sponsor Vim development Vim logo Vim Book Ad

basic Tip #1316: Easy visual commenting - Navigating visual selections with '< and '>

 tip karma   Rating 5/2, Viewed by 1369 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   August 24, 2006 19:44      complexity:   basic
author:   Max Cantor      as of Vim:   5.7

By using '< and '>, you can jump to the start and end, respectively, of the most recent visual selection.  This is a great and simple way to comment out a block of visually selected code:

vmap ,/ '><ESC>o*/<ESC>'<O/*<ESC>

This works because even after you <ESC> out of visual mode, '< and '> (presumably) remember where the most recent visual selection began and ended.  Actually, this doesn't always work.  Curly braces seem to confuse Vim when I do it that way, so here's an alternate way with marks (note: this will overwrite mark ')

vmap ,/ '<m''><ESC>o*/<ESC>''O/*<ESC>

Now, uncommenting is as easy as deleting the two lines that the /* */s are on.  Or you can use this: (note: this will produce unexpected results if your /* */s aren't on their own lines!)

vmap ,u ?\/*dd/*\/dd:nohl<CR>

 rate this tip  Life Changing Helpful Unfulfilling 

<< netrw x command | Open a buffer in its own tabpage >>

Additional Notes

Anonymous, August 25, 2006 1:07
Or you could use the nerd commenter http://www.vim.org/scripts/script.php?script_id=1218
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge.net Logo