Silly stuff   vi   cheatsheet

Just try them! :)

:set rightleft
:helpuganda
:help!
:help42
:helpquotes
:help holy-grail

registers

When you copy and cut stuff, it gets saved to registers. You can pull stuff from those registers at a later time.

:reg- show named registers and what's in them
"5p- paste what's in register "5

You can also record a whole series of edits to a register, and then apply them over and over.

qk- records edits into register k
   (q again to stop recording)
@k- execute recorded edits (macro)
@@ - repeat last one
5@@ - repeat 5 times
"kp - print macro k
   (e.g., to edit or add to .vimrc)
"kd- replace register k with what cursor is on

editing in a stream

You can take the output of any command and send it into a vim session. From there you could format it, change stuff, and then save it to a file.

find . | vim -

folding

Use folds to collapse selected blocks. Useful if you've finished a subroutine and want to save window space, or maybe want to fold all blocks of comments.

select block, then :fold
zo - open
zc - close
See :help foldmethod for options, and use :mkview and :loadview to save and restore the current window.

markers

Use markers to set places you want to quickly get back to, or to specify a block of text you want to copy or cut.

mk- mark current position (can use a-z)
'k- move to mark k
d'k - delete from current position to mark k
'a-z - same file
'A-Z - beteween files

text selection

If you want to do the same thing to a collection of lines, like cut, copy, sort, or format, you first need to select the text. Get out of insert mode, hit one of the options below, and then move up or down a few lines. You should see the selected text highlighted.

V - selects entire lines
v - selects range of text
ctrl-v - selects columns
gv - reselect block
After selecting the text, try d to delete, or y to copy, or :s/match/replace/, or :center, or !sort, or...

searching

For basic searching:

/pattern - search forward for pattern
?pattern - search backward
n - repeat forward search
N - repeat backward
Some variables you might want to set:
:set ignorecase - case insensitive
:set smartcase - use case if any caps used
:set incsearch - show match as search proceeds
:set hlsearch - search highlighting

More cool searching tricks:

* - search for word currently under cursor
g* - search for partial word under cursor
   (repeat with n)
ctrl-o, ctrl-i - go through jump locations
[I - show lines with matching word under cursor

word & line completion

Typing is a pain! In insert mode, try:


ctrl-n, ctrl-p - next/previous word completion
   (similar word in current file)
ctrl-x ctrl-l
(ctrl-n/p)
- line completion
:set   dictionary=/usr/share/dict/words
ctrl-x ctrl-k - dictionary completion
also
ctrl-w - erases word (insert mode...
ctrl-u - erases line ...or on command line)

modelines

modelines allow you to set variables specific to a file. By default, the first and last five lines are read by vim for variable settings. For example, if you put the following in the last line of a C program, you would get a textwidth of 60 chars when editing that file:


/* vim: tw=60 ts=2: */

modelines variable sets the number of lines (at the beginning and of each file) vim checks for initializations.

variables

Here's how to show, set, and reset vim's variables:

:set - shows vars different from defaults
:set all - shows all values
:set foo? - shows the value of foo
:set foo+=opt - add opt to the value w/o changing others
:set foo-=opt - remove opt from value
:set foo& - reset foo to default value
:setlocal foo - only the current buffer
:verbose set foo? - tells you where it was last set!

indenting

Some variables you might want to set:

:set tabstop 8 - tabs are at proper location
:set expandtab - don't use actual tab character (ctrl-v)
:set shiftwidth=4 - indenting is 4 spaces
:set autoindent - turns it on
:set smartindent - does the right thing (mostly) in programs
:set cindent - stricter rules for C programs

I like having auto on, but smart does funny things based on keywords.

To indent the current line, or a visual block:

ctrl-t, ctrl-d - indent current line forward, backwards
(insert mode)
visual > or < - indent block by sw (repeat with . )

To stop indenting when pasting with the mouse, add this to your .vimrc:

:set pastetoggle=

then try hitting the F5 key while in insert mode (or just :set paste).

reformatting

These are useful to reformat text paragraphs or chunks of code:

V=- select text, then reformat with =
=    will correct alignment of code
== - one line;
gq - reformat paragraph

Options to change how automatic formatting is done:

:set formatoptions (default "tcq")
t - textwidth
c - comments (plus leader -- see :help comments)
q - allogw 'gq' to work
n - numbered lists
2 - keep second line indent
1 - single letter words on next line
r - (in mail) comment leader after

Other related options:

:set wrapmargin
:set textwidth

multiple windows

If you want, you can probably do everything from one vim session! :) Here are some commands to turn one vim session (inside one xterm) into multiple windows.

:e filename- edit another file
:split filename - split window and load another file
ctrl-w up arrow - move cursor up a window
ctrl-w ctrl-w- move cursor to another window (cycle)
ctrl-w_- maximize current window
ctrl-w=- make all equal size
10 ctrl-w+- increase window size by 10 lines
:vsplit file- vertical split
:sview file- same as split, but readonly
:hide- close current window
:only- keep only this window open
:ls- show current buffers
:b 2- open buffer #2 in this window