How To Comment Out Multiple Lines At Once In Vim Editor

Comment Out Multiple Lines At Once In Vim Editor

4 Responses

  1. anand nande says:

    Method 4:

    – Press Ctrl+V to enter into ‘Visual block’ mode
    – Press down arrow to select all the lines in your file
    – Press Shift+i to enter INSERT mode (this will place your cursor on the first line)
    – Press Shift+3 (which will insert ‘#’ without the quotes) before your first line
    – Press ESC key now, and you can see all lines are commented out.

  2. nuny3 says:

    If you want to be productive in Vim you need to talk with Vim with *language* Vim is using. Every solution that gets out of “normal
    mode” is most probably not the most effective.

    METHOD 1
    Using “normal mode”. For example comment first three lines with: I#j.j.
    This is strange isn’t it, but:
    I –> capital I jumps to the beginning of row and gets into insert mode
    # –> type actual comment character
    –> exit insert mode and gets back to normal mode
    j –> move down a line
    . –> repeat last command. Last command was: I#
    j –> move down a line
    . –> repeat last command. Last command was: I#
    You get it: After you execute a command, you just repeat j. cobination for the lines you would like to comment out.

    METHOD 2
    There is “command line mode” command to execute “normal mode” command.
    Example: :%norm I#
    Explanation:
    % –> whole file (you can also use range if you like: 1,3 to do only for first three lines).
    norm –> (short for normal)
    I –> is normal command I that is, jump to the first character in line and execute insert
    # –> insert actual character
    You get it, for each range you select, for each of the line normal mode command is executed

    METHOD 3
    This is the method I love the most, because it uses Vim in the “I am talking to Vim” with Vim language principle.
    This is by using extension (plug-in, add-in): https://github.com/tomtom/tcomment_vim extension.
    How to use it? In NORMAL MODE of course to be efficient. Use: gc+action.

    Examples:
    gcap –> comment a paragraph
    gcj –> comment current line and line bellow
    gc3j –> comment current line and 3 lines bellow
    gcgg –> comment current line and all the lines including first line in file
    gcG –> comment current line and all the lines including last line in file
    gcc –> shortcut for comment a current line

    You name it it has all sort of combinations. Remember, you have to talk with Vim, to properly efficially use it.
    Yes sure it also works with “visual mode”, so you use it like: V select the lines you would like to mark and execute: gc

    You see if I want to impress a friend I am using gc+action combination. Because I always get: What? How did you do it? My answer it is Vim, you need to talk with the text editor, not using dummy mouse and repeat actions.

    NOTE: Please stop telling people to use DOWN arrow key. Start using h, j, k and l keys to move around. This keys are on home row of typist. DOWN, UP, LEFT and RIGHT key are bed habit used by beginners. It is very inefficient. You have to move your hand from home row to arrow keys.

    VERY IMPORTANT: Do you want to get one million dollar tip for using Vim? Start using Vim like it was designed for use normal mode. Use its language: verbs, nouns, adverbs and adjectives. Interested what I am talking about? You should be, if you are serious about using Vim. Read this one million dollar answer on forum: https://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim/1220118#1220118

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.