Vim tips and tricks

Introduction

This a short list of some of the greatest features in vim. Here is my
vimrc file and webfriendly vesion: vimrc.html.

Visual block editing

Pressing Ctrl-v in normal mode will put you in visual block mode. This mode makes it easy to edit for example columns in file with multiple columns.

This can also be used to out comment a lot of lines at the same time. Select the first character of the line with Ctrl-v and press I and for example #. When esc is pressed the change will propagate down to the other lines.

Word completion

Pressing Ctrl-n insert mode will complete the word currently under the marker based on the words found in the document. Great for variables.

Reformat paragraphs to a fixed line length

In visual mode marking a paragraph and pressing gq will reformat the paragraph to a certain amount of characters. A more powerful version of this is to use par, see Using par to reformat paragraphs in vim [tjansson.dk].

Indent code

Mark a visual block with Ctrl-v in normal mode. Then press “=”. This will re-indent the code such that loops are nested and such. To reindent the whole file use “1G=G” – This command say goto line 1 (1G), reindent (=), until the last line (G), see
http://arioch.unomaha.edu/~jclark/vim.html

Editing LaTeX documents in vim

As described in Using latexmk, vim spell checking and vim latex-suite [tjansson.dk] there is a great plugin for vim called latex-suite. Used in connection with latexmk magic will happen.

Spell checking

Also described in As described in Using latexmk, vim spell checking and vim latex-suite [tjansson.dk] is how to setup spell checking in vim.

Syntax highlighting and syntax highlighted printing

Vim has great syntax highlighting and with a little hack this can even be carried on to the print, see Syntax highlighted printing with vim [tjansson.dk].

Export a file to HTML with syntax highlighting

In normal mode writing :TOhtml will save the file with syntax highlighting as a html file.

Navigating between windows in Vim

The vim session use several windows for example by using :vertical. Navigating between these windows is down by the following commands:
Ctrl-W Ctrl-W cycle through the windows.
Ctrl-W= make all the windows equally sized.
:e . opens a file manager to select a file to open.
:vsp Vertical split view.
:sp Horizontal split view.

View a man-page from within vim

By typing K in normal mode vim will open the man page for program under the cursor.

Using vimdiff to see changes

I often use vimdiff to merge to slightly different version of a document, but I always forget the commands:
do – Get changes from other window into the current window.
dp – Put the changes from current window into the other window.
]c – Jump to the next change.
[c – Jump to the previous change.
Ctrl W + Ctrl W – Switch to the other split window.

From http://amjith.blogspot.com/2008/08/quick-and-dirty-vimdiff-tutorial.html

Links

http://blog.interlinked.org/tutorials/vim_tutorial.html