Assorted vim tips.
Additional contributors: Emil Mikulic, Saied Tahaghoghi.
Note: commands in this document are typed as they would appear in your .vimrc file. To enter them as commands in an active vim session prefix them with a colon. For example:
:set cindentWhen the cursor is over one of a pair of parentheses, square brackets or braces, press % to move quickly to the other. You can also use the % key as a destination argument to many vi commands (e.g. d% will delete everything between a pair of braces, including the braces).
set showmatch will briefly highlight a matching brace/parenthesis/square bracket when you type it's partner.
You can turn "dumb" automatic indenting on with set ai (or autoindent) and off with set noai (or noautoindent). This just starts a new line at an indent equal to the current one.
You can enable smart indenting with set si - amongst other things this will automatically adjust indenting for braces and push hash comments to the first column. This second "feature" can be annoying but is easily disabled using :inoremap # X^H# (use ctrl-v ctrl-h to enter the ^H).
Vim also has a "cindent" mode that is very useful for c-like code and is highly configurable - see the help file indent.txt for details. You can try it out with set cindent.
Automatic indenting makes X copy-and-paste untenable (try it and see!) so it's useful to have a key which will enable/disable it for pasting. You can set this with pastetoggle - set pastetoggle=<F11>.
Alternatively, enter the command :a (ex-style append), paste your text and press ctrl-C. This will enter text unmodified by any indenting abbreviation or similar commands. Thanks to Emil for this tip.
If you want to reindent a chunk of text (e.g. you've just pasted in some badly formatted code) by selecting it in insert mode and pressing =. Thanks to Saied for this tip.
Use ab [abbreviation] [expansion] to define an abbreviation, where [abbreviation] is a single word (it can expand to many words). Typing in the abbreviation followed by a space will make vim automatically expand the text. For example:
ab uns unsigned short It's useful to highlight "trailing" whitespace - spaces and tabs at the end of the line. These don't display anything but can mess up automatic wrapping, version control software (like RCS, CVS and Subversion), diffs... The following commands, thanks to Emil, will display trailing whitespace in dark grey:
set list listchars=trail:.
highlight SpecialKey ctermfg=DarkGray
set list listchars=tab:\|_,trail:.
You can easily set up a key to automatically pipe the editor contents through a command:
map x :%!somecommand -arg1 -arg2<CR>The <CR> simulates a carriage return keypress (otherwise the mapping would just print the command on the : line). This is useful for rot13 scripts.
A modeline is a line close to the beginning or end of a file which contains vim settings. When the file is opened for editing vim will read the modeline and use the given settings. Modelines take one of two forms:
vim: shiftwidth=3 tabstop=3 expandtab
vim: set shiftwidth=3 tabstop=3 expandtab:
You can place the modelines inside a comment but they need whitespace before and after or they will not function. Modelines at the beginning of a line will be ignored in some circumstances - try indenting them slightly.
The simple way to do this is to use an autocommand with the file extension:
au BufRead,BufNewFile *.c set tabstop=3 | set shiftwidth=3
Another way is using the builtin Vim filetype detection. This requires the filetype plugin installed, but has the advantage that it can detect files based on their contents as well as the extension - and doesn't require you to write your own detection rules. Setting the same values as above:
au BufRead,BufNewFile * if &ft == 'c' | set ts=3 | set sw=3 | endif
The 'c' filetype is autodetected by the filetype plugin which sets the filetype/ft variable to 'c'. Check "filetype.vim" on your system to see which filetypes are available.