Nov 092011
 

There are a number of those around the interwebs, but this one is mine.

Working with files

:q Quit Vim. 
:q! Quit now, no saving
:wq Save the current file and exit.
:w {file} Write to {file}
:e {file} Edit file. Can use tab completion. Without argument will reopen the current file discarding changes.
:x Write current file, if modified, and exit.
ZZ Write current file, if modified, and exit.
ZQ Quit current file and exit (same as “:q!”).

Inserting/changing Text

a Append text after the cursor [count] times.
A Append text at the end of the line [count] times.
i Insert text before the cursor [count] times.
I Insert text before the first non-blank in the line
o Begin a new line below the cursor and insert text, repeat [count] times.
O Begin a new line above the cursor and insert text, repeat [count] times.
R Start replace/overwrite mode
:r[ead] [name] Insert file [name] below the cursor
:r[ead] !{cmd} Insert the result of {cmd} below the cursor
~ Change case of char under cursor

To comment a block of text enter visual mode with “CTRL+v”, select the beginning of the lines to comment, type capital “I”, type the comment (like “#[space]”), hit ESC. To uncomment, “CTRL+v”, select, “d”.

Deleting Text

<Del> or x Delete [count] characters under and after the cursor
X Delete [count] characters before the cursor
d{motion} Delete text that {motion} moves over
dd Delete [count] lines. Ex.: 5dd will delete 5 lines
D Delete the characters under the cursor until the end of the line
:[range]d[elete] Delete [range] lines (default: current line)
:[range]d[elete] {count} Delete {count} lines, starting with [range]

To uncomment a block enter visual block mode with “CTRL+v”, select, “d”.

Substituting/replacing

:s/{pattern}/{string}/[arguments] For each line in [range] replace a match of {pattern} with {string}.
To search the whole file, [range] should be %,
:%s/{pattern}/{string}/gc will replace {patern} with {string} in the whole file, asking for confirmation

Arguments:

  • [c] – confirm each substitution
  • [g] Replace all occurrences in the line. Without this argument, replacement occurs only for the first occurrence in each line.
  • [i] Ignore case for the pattern.
  • [I] Don’t ignore case for the pattern.
  • [p] Print the line containing the last substitute.

Working with blocks

v Enter visual mode
V Enter visual mode with line selection
CTRL+v Enter visual block mode. You can select blocks of text, not the whole line
y Yank the selected text. (CTRL+c)
p Put the yanked text after the cursor (CTRL+v)
P Put the yanked text before the cursor
d Delete the selected text
> Shift right (indent)
< Shift left (unindent)
~ Switch case of selected text
“{a-z} Use register {a-z} for the next yank/put. Using {A-Z} will append to the register.
To use, select the text, then “ay would yank it in register a. “ap would then paste from register a

Undo/Redo/Repeat

u Undo
CTRL-R Redo changes which were undone.
U Undo all latest changes on one line
. Repeat last change

Moving Around

h or <left arrow> left
l or <right arrow>  or [Space] right
k or <up arrow> up
j or <down arrow> down
0 or <Home> Start of line
^ First non-blank character of the line
$ or <End> End of line
g0 or g<Home> First char on screen on current line
g^ First non-blank char on screen on current line
g$ or g<End> Last char on screen on current line
– <minus> First non-blank char on line above
+ or <CR> First non-blank char on line below
<C-End> or G First non-blank char of the last line
<C-Home> or gg First non-blank char of the first line
e Forward to the end of word
E Forward to the end of whitespace-delimited word
b Back to the beginning of word
B Back to the beginning of whitespace-delimited word

Marks

m{a-zA-Z} Set mark {a-zA-Z} at cursor position,
{a-z} Jump to the mark {a-z}
{A-Z0-9} To the mark {A-Z0-9} in the correct file
:marks List all the current marks (not a motion command).
:marks {arg} List the marks that are mentioned in {arg} (not a motion command)

Searching

/{pattern} Search forward for {pattern}
n Repeat last search
N Repeat in opposite direction.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

(required)

(required)