VIM – Comment blocks of code

by

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.

17 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 = 'http://www.linkwithin.com/widget.js?rand=' + 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:

    […] https://notfaq.wordpress.com/2006/07/28/vim-comment-blocks-of-code/ […]

  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 […]

  11. What are the dark corners of Vim your mom never told you about? | Ngoding Says:

    […] I just found this one today via NSFAQ: […]

  12. Solution: What are the dark corners of Vim your mom never told you about? #dev #it #computers | Technical information for you Says:

    […] I just found this one today via NSFAQ: […]

  13. Fixed What are the dark corners of Vim your mom never told you about? #dev #it #asnwer | Good Answer Says:

    […] I just found this one today via NSFAQ: […]

  14. Darren Says:

    Reblogged this on Darren Goossens and commented:
    I need to remember this and I never do.

  15. What are the dark corners of Vim your mom never told you about? [closed] | ASK AND ANSWER Says:

    […] I just found this one today via NSFAQ: […]

Leave a comment