Vim [1] is one of the most popular text editors for Linux and Unix systems. Its text-based interface may look intimidating for newcomers, but underneath it there is a wealth of functionality to be learned. Experienced Vim users often feel much more productive using Vim than GUI-based text editors.
I've been using Vim on a daily basis for years to do a wide range of tasks, from casual editing of short text files, to managing large programming projects, and every now and then I come across a new feature that considerably helps in some aspect of text editing. For this reason, I decided to compile a list of such Vim tips in this post.
When pasting code from a GUI application (a web browser for example), Vim mysteriously inserts a lot of extra white spaces into code. This happens because Vim thinks that you are actually typing and not pasting, so it indents the code again, resulting in more white spaces. To solve this problem, run the following in normal mode before pasting:
:set paste
i to switch to insert mode, and paste your code. Once done, you may disable this by using::set nopaste
Visual blocks are one of those features that you won't find in a GUI text editor. They let you mark blocks of text and do editing operations on them. To enter visual blocks mode, press ctrl+v, then use HJKL or arrow keys to highlight a block of text. Now operate on the first highlighted line as if you were in normal mode, and operations will be reflected on other highlighted lines. For example, to indent a block of text with > characters (to make it look like a text being replied to), highlight the first column of characters in visual blocks mode, then press shift+i to switch to insert mode at the beginning of the line. Insert a > in the first line and press ctrl+c. A > will be prepended to all other highlighted lines.
This feature is also useful for commenting blocks of code, by prepending lines with // or #.
You may work with several view ports of the same file in the same Vim window. This is very useful for moving pieces of text inside a document. To open another viewport of the same file, use:
:sp
:sp filename
To switch between buffers, use ctrl+w twice, and to open a vertical buffer use :vsp filename.
To jump to a specific buffer, enter its number before pressing ctrl+w ctrl+w. For example, the following jumps to the 3rd buffer:
3 ctrl+w ctrl+w
In addition to filenames, sp and vsp take any command that opens a buffer. For example, to open help in a new vertical buffer, use:
:vsp :help
If you find yourself re-typing the same command over and over, why not map it to one of the function keys? To do so use the map set of commands:
:map <Fx> cmd
cmd to function key Fx.
nmap, imap and vmap can be used to limit key mapping to normal, insert, and visual modes respectively. For example, to map F9 to the command :!gcc -c % (compiles the current file) in normal mode:
:nmap <F9> :!gcc -c % <CR>
To cancel a key mapping, use unmap (Or one of its friends nunmap, iunmap, and vunmap), example:
nunmap <F9>
A mapping command is available for the current Vim session only. To make it available whenever you launch Vim, add it to your .vimrc.
Vim can fold blocks of code like how Ecplise (and other IDEs) does. To fold a number of lines (5 for example) press zf5j. To fold a code block marked by braces { }, move the cursor into the block and press zfa}. Vim replaces folded code with text like:
+----- 6 lines: }-----
To open the folded text, press zo while the cursor is at the above line. To close it back, press zc.
You may use registers to simulate multiple clipboards for copy and paste operations. Register names consist of one letter or number. To yank the current selection in visual mode to the register a, use "ay. To paste the contents of register a, use "ap. The same applies to other registers ("iy and "ip, for registeri). To view contents of all registers, use: :reg.
Placing the following in your .vimrc will replace tabs with 2 spaces when tab key is pressed, when indenting, and auto-indenting. this can be very useful while working with Python code for example, as it stops tabs from sneaking into files.
set et set sw=2 set sts=2 set smarttab
Vim can be enhanced by plugins; the following is a list of plugins that I believe would improve the Vim experience while working with code. You may find more at Vim Scripts [12]. To install a plugin, search for it in your package manager (Linux/Unix users) or refer to :help add-plugin.
TAB while in edit mode.ctrl+c. This is faster than hitting ESC multiple times.~ changes case of current letter.zb, zt, and zz scroll the screen to make the current line at the top, bottom, or middle.
=. To auto-indent the current line, press ==.
gq to wrap the highlighted peice of text.:set wrap and :set nowrap to toggle long line wrapping.
vimdiff file1 file2. It provides a nice color-highlighted view with code folding, much better than plain diff.
:help to access Vim's help, and :help cmd for information on the command cmd.vimtutor is Vim newcomer's friend. it provides a course on Vim usage.Links:
[1] http://en.wikipedia.org/wiki/Vim_(text_editor)
[2] http://aymanh.com/a-collection-of-vim-tips#
[3] http://aymanh.com/a-collection-of-vim-tips#PastingFromGUI
[4] http://aymanh.com/a-collection-of-vim-tips#VisualBlocks
[5] http://aymanh.com/a-collection-of-vim-tips#MultipleBuffers
[6] http://aymanh.com/a-collection-of-vim-tips#MapCommonlyUsedTaskstoKeys
[7] http://aymanh.com/a-collection-of-vim-tips#CodeFolding
[8] http://aymanh.com/a-collection-of-vim-tips#MultipleClipboards
[9] http://aymanh.com/a-collection-of-vim-tips#ConvertTabstoKeys
[10] http://aymanh.com/a-collection-of-vim-tips#MustHavePluginsForDevelopment
[11] http://aymanh.com/a-collection-of-vim-tips#QuickTips
[12] http://www.vim.org/scripts/index.php
[13] http://www.vim.org/scripts/script.php?script_id=182
[14] http://vim.sourceforge.net/scripts/script.php?script_id=159
[15] http://www.vim.org/scripts/script.php?script_id=273
[16] http://www.vim.org/scripts/script.php?script_id=90