A Collection of Vim Tips

Vim 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.

Pasting From GUI

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

Now press i to switch to insert mode, and paste your code. Once done, you may disable this by using:
:set nopaste

Visual Blocks

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 #.

Multiple Buffers

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

You may also open another file in the same window. To do so issue the command:
: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

Map Commonly-Used Tasks to Keys

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
This maps the command 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.

Code Folding

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.

Multiple Clipboards

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.

Convert Tabs to Keys

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

Must-Have Plugins For Development

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. To install a plugin, search for it in your package manager (Linux/Unix users) or refer to :help add-plugin.

  • SuperTab: Makes all insert-mode completion done with tab. To use, simply press TAB while in edit mode.
  • minibufexpl: Adds a buffer explorer to the top of Vim's window, simplifies working with buffers.
  • taglist: A source code browser that works with many languages, including C/C++, Java, Python, Perl, PHP, ...
  • vcscommand: SVN/CVS integration.

Quick Tips

  • To quickly get out of command-line, press 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 a piece of code, highlight it in visual mode, and press =. To auto-indent the current line, press ==.
  • Use gq to wrap the highlighted peice of text.
  • Use :set wrap and :set nowrap to toggle long line wrapping.
  • To visually check the difference between two files, use vimdiff file1 file2. It provides a nice color-highlighted view with code folding, much better than plain diff.
  • Type :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.
Tags:
Submitted by Ayman on Thu, 2006/09/07 - 7:37pm

Jos (not verified) | Very nice article | Sat, 2006/09/30 - 7:04pm

I like this article on Vim. Came across your article on del.icio.us.

I also like your webdesign. Just one question...

At the end of the article I see the number of people who have read the article - now it shows "994 reads". Asking because of curiosity. Which module do you use to enable it in drupal? Or have you written the php code yourselves ?

I know that there is a plugin available for wordpress which gives the same functionality.

Cheers

Ayman | Glad that you enjoyed the | Sat, 2006/09/30 - 8:01pm

Glad that you enjoyed the article :)

Regarding the view count, I'm using the statistics module, it's part of Drupal core, to use, enable it in the module list, then go to admin > settings > statistics.

Hope this helps.

Jos (not verified) | Thanks for the pointer :-) | Sun, 2006/10/01 - 3:23pm

I wonder how I missed that one :-) . But what about the del.icio.us tag and others ? Are they also modules ?

Btw, I am testing drupal (4.7) on my personal machine that is why I ask these questions and I am literally blown away by the sheer number of features this CMS has. It is awesome.

I have tried joomla, wordpress and a couple of others but in my opinion, nothing beats drupal - period.

Anonymous (not verified) | thanks, i'd been looking to | Mon, 2006/10/02 - 5:18pm

thanks, i'd been looking to solve that pesky crazy tabs paste problem.
VIM 7 is truly beautiful!

Jalal Araidah (not verified) | nice article | Sat, 2006/10/14 - 8:21am

Nice article Ayman :)

one of my most used tip is to encrypt a file:

vim -x foo.txt, or just :X inside a file

cheers!

mysurface (not verified) | More vim tips to share! | Fri, 2006/11/24 - 1:29am

Nice! vim have a lots more function and tips, it is so discoverable, I always post up my tips when I find one, I am here to share it and hope to get more tips.

http://lne.blogdns.com/lbe/archives/category/text-manipulation/vim/

Have fun.

Alexis Bellido (not verified) | Visual blocks | Fri, 2008/10/17 - 3:53am

Hi Ayman, good tips, many of them have been on my arsenal for a long time, I'm also a vim addict :)

One small observation regarding visual blocks, this is how it works for me:

  1. Select a block of text with ctrl+v
  2. Enter insert mode to start writing on the beginning of the line by pressing shift+i
  3. Write what I need, for example // to comment on PHP or # on Python. At this point what I write appears just on the first line of the block
  4. Finally press ESC and the text I wrote appears on all other lines of the block I had selected. You mention ctrl+c at this point but it didn't work for me. I'm running vim 7 on Ubuntu

Slightly off topic: I read your Django book last week. I really enjoyed it and learnt a lot from it. Thanks!

catalin.me (not verified) | Great tips! | Mon, 2009/01/19 - 8:24pm

Great tips!

Anonymous (not verified) | Great tips you got here, | Fri, 2009/02/13 - 3:28am

Great tips you got here, Aymanh.

Alex (not verified) | Question | Sun, 2009/04/26 - 8:17am

Wow, nice tips there. I`ve bookmarked your page.
Is there any way to fold blocks with id`s? I do learn web design and only use vim now. Notepad++ have this function by default, i used it times ago in Win. Now i work on my loved Linux Debian Lenny and installed Notepad++ only for this function, but would love to use mighty vim for everything.

Would thank for tips via eMail, and i have the rss comment feed of this posting.

Thanks and regards
Alexander
qsettemix@googlemail.com

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <strike> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <sup> <sub> <h1> <h2> <h3> <b> <i> <u>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

About

Ayman Hourieh

Computer Science graduate, Open Source enthusiast and Software engineer (Site reliability) at Google.

I'm 25 years old, and live in Dublin, Ireland.

This is my personal blog. The views expressed on these pages are mine alone and not those of my employer.

More

Books

Learning Website Development with Django

Learning Website Development with Django
A beginner's tutorial to building web applications, quickly and cleanly, with the Django application framework.

My first book. Published by Packt Publishing in April 2008.