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 command 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

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.

Comments

Jos
Jos's gravatar

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


Posted at 6:04 p.m. on September 30, 2006

Ayman Hourieh
Ayman Hourieh's gravatar

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.


Posted at 7:01 p.m. on September 30, 2006

Jos
Jos's gravatar

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.


Posted at 2:23 p.m. on October 1, 2006

Anonymous
Anonymous's gravatar

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


Posted at 4:18 p.m. on October 2, 2006

Jalal Araidah
Jalal Araidah's gravatar

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!


Posted at 7:21 a.m. on October 14, 2006

mysurface
mysurface's gravatar

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.


Posted at 12:29 a.m. on November 24, 2006

Alexis Bellido
Alexis Bellido's gravatar

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:

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


Posted at 2:53 a.m. on October 17, 2008

Anonymous
Anonymous's gravatar

Great tips you got here, Aymanh.


Posted at 2:28 a.m. on February 13, 2009

Alex
Alex's gravatar

Wow, nice tips there. I've bookmarked your page. Is there any way to fold blocks with <div> 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


Posted at 7:17 a.m. on April 26, 2009

majestrooo
majestrooo's gravatar

Omg thank you for your wrapping tip!

It suddenly made vim an excellent editor for documents, it was very annoying when vim didn't wrap lines when writing reports in latex.


Posted at 9:39 a.m. on September 13, 2009

sumeet
sumeet's gravatar

A lot of tips were very useful.But seems you have not edited visual block tip corrected given by ubuntu user.


Posted at 11:50 a.m. on December 3, 2009

ixti
ixti's gravatar

Hello,

First of all, thank you for the tips. In fact I was looking for code folding but met lots of other useful things :)) And about pasting from GUI. I use another way: :r!xsel or :r!xsel -b

Depending on what buffer I want to use :)) :r! simply insert command's output, so it's not tabbed :)) And xsel is a command to manipulate the X selections :))

So, of course you can use the same to insert current date: :r!date


Posted at 2:08 p.m. on January 25, 2010

ContabilulManole
ContabilulManole's gravatar

I found your website by mistake, looking for Drupal web content management tutorials, but I'm glad I did. Very interesting and easy. I'm going to give this a try. Thank you.


Posted at 8:12 p.m. on February 15, 2010

Anonymous
Anonymous's gravatar

If you use X, you can get around the whole paste issue by just doing

:r !xclip -o

or

:r !xclip -o -selection c

Bind those to keys if you wish; no need for :set paste and the like.


Posted at 9:24 p.m. on March 20, 2010

vagubunt
vagubunt's gravatar

the vim-scripts page for the minibuffexplorer plugin is outdated, the actual version is available at https://github.com/fholgado/minibufexpl.vim ...

greets


Posted at 1:31 a.m. on July 4, 2011

Post a comment

HTML is not allowed. You can use markdown syntax to format your comment.