VIM – Comment blocks of code


Mark the area which is to be commented using the *blockwise* visual mode (CTRL-V, in Windows this is CTRL-Q).

Press I (capital i) and write the text you want to prepend to each line of the selected block, e.g. %.

Then press ESC and the text will be inserted to the left of each line of the selected block.

About these ads

12 Responses to “VIM – Comment blocks of code”

  1. Linux T&T : VIM - Comment blocks of code Says:

    [...] VIM – Comment blocks of code « Not So Frequently Asked Questions [...]

  2. ivanatora Says:

    Capital “I” puts me simply into an insert mode.

    • michalmolhanec Says:

      That’s OK. Just type something, don’t worry that it will display just on the beginning of one line, when you press Esc, it will be added to all of the selected lines (some version of vim needs to press e.g. move key after Esc to display it).

  3. gvim – comentar mĂșltiplas linhas : Wilson Blogue Says:

    [...] Via var linkwithin_site_id = 38479; (function () { var elem = document.createElement('script'); elem.type = 'text/javascript'; elem.src = '' + Math.random(); document.getElementsByTagName('head')[0].appendChild(elem); })(); [...]

  4. James Kubecki Says:

    So… What’s an easy way to UNcomment it? Or should I go the script route? (Found several.)

    • nietaki Says:

      James: it’s even easier than commenting stuff out: You use CTRL-v to block mark just the comment characters (which is easy, they are in equal amounts at line beginnings), than just press d to delete the block of text. And it is as if you never commented anything out ;)

  5. staticip Says:

    Nice tip.

  6. Comments block | Rapidrefillwas Says:

    [...] VIM – Comment blocks of code « Not So Frequently Asked QuestionsA special comment block is a C or C++ style comment block with some additional markings, so doxygen knows it is a piece of structured text that needs to end up … [...]

  7. rafiks Says:

    good one!

  8. How to comment a block of code in vim | Stack Trace Says:

    [...] [...]

  9. svenlogan Says:

    Reblogged this on Sven Logan's Blog and commented:
    Proved to be quite useful when editing system configuration files (Apache2, etc.). Thanks!

  10. Vault»Blog Archive » I am not a link bot… I hope Says:

    […] Vim copy and past commands – Setting blocks, yank, paste, cut, etc.. in vim […]

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: