To get started with Vim, first read Basic Text Editing with Vim

Advanced Text Editing with Vim

Wherever this text says gvim you can also read mvim.

Subjects in this page

joining lines

Suppose you have several
lines that you want to
merge into one long line. A bit
like these four lines.

Put the cursor on the first line and type J for every line you want to merge. This transforms the above lines into the following:

Suppose you have several lines that you want to merge into one long line. A bit like these four lines.

If you don't want Vim to insert a space, use g J.


The other way around is also possible. When you have a piece of text containing no line breaks, e.g. an SQL dump or an e-mail with very long lines which you want to re-format into lines with a maximum width of 40 characters, use

:set textwidth=40
Then select the text you want to format and type g q. Don't forget to put the textwidth back to a reasonable width afterwards.

dot dot dot...

One of the most powerful features of Vim is the . command, also known as the dot command.

The key ., when entered in command mode, will repeat the last performed action.

Example: add a line and then multiply it by 5 is easy. Let's add a line: A <ENTER>This is a line ESC
Now repeat that action 4 times so we end up with 5 identical lines 4 .

Another approach for this specific case would be to put the number first and then what you want to do, e.g. for creating a line with 60 asterisks: 60 A * ESC will give you

Moving around with confidence

Mark a line as a : m a, then move to another line in the file and jump back to line a: ' a

Mark a line as b : m b
Go up a couple of lines and type d ' b to delete up to line b or y ' b yank up to line b, then put the deleted or yanked lines somewhere else in the file: p

Window splitting


gvim -p file [file...]  open all files in one window,
  where each file gets its own tab

gvim -o file [file...]  open all files in multiple split-windows

Advanced search and replace

^ - start of line
$ - end of line


The % is a shortcut that tells vi to search all lines of the file for search_string and change it to replacement_string.

The global (g) flag at

the end of the command tells vi to continue searching for other occurrences of search_string.

To confirm each replacement, add the confirm (c) flag after the global flag

Press * to display all words matching the word under the cursor. Jump to next word using 'n' and backwards using 'N'.

Press [I to display all lines that contain the keyword under the cursor.

Run commands on lines

Delete all empty lines:

Delete all lines matching a keyword:

Delete all lines NOT matching a keyword:

Dope for programmers

(({Matching {brackets}} and (parentheses)))

Matching brackets { } and parentheses ( ) is quite easy with these two techniques that Vim offers:

What goes for brackets applies to parentheses as well, so this functionality is great for C, C++, Java and a gift from the gods for Scheme.

Word completion

If you use the same word several times in one document, you don't have to type it again and again, just type its first letters and then CTRL n for auto-completion.

Indenting level

Make sure these two lines are in your .vimrc:
vnoremap < <gv
vnoremap > >gv
then you can select a piece of code and use > to move it to the right and < to move it to the left


Try what happens when you do this: ! G sort and be ready to UNDO with u
I'll explain later...

Regular expressions

replace multiple spaces by single space:  %s/ \+/=/g
remove everything from @ to end of line: %s/@.*//g

remove whitespace at start of line
:%s/^[ ]\+//g

remove whitespace at end of line
:%s/[ ]\+$//g

Convert M$ Windows style line-ends (CRLF) to UNIX-style (LF):
replace ^M by newline: %s/^M/\r/\r/g

VIM folding

Folding task lists in Vim