How is an agile way of adding and removing code comments in VIM?

10

I do not want to remove the entire line of code in VIM, what I want is to work with the comment codes. I have 3 cases:

Comments that open and close (usually multiple lines):

/* doSomething(); */

Comments with 2 characters in the line of code (one per line):

// doSomething();

And comments with only 1 character at the beginning (one per line):

# doSomething();

I'd like to know an agile way to add and remove these types of comments in VIM, so that it only applies to selective lines of code and not to the entire file.

    
asked by anonymous 24.02.2014 / 15:33

6 answers

4

I suggest using the tcomment plugin, which provides unique shortcuts to comment / uncomment across multiple languages.

With tcomment installed, you can do, by the Normal mode:

  • gcc to comment / uncomment current line
  • V to select rows visually, and gc to comment / uncomment selected rows
  • gc{movimento} to uncomment in the direction of motion. Examples:
    • gc3j comments on current line and next 3
    • gcG comments to the end of the file.

It has more shortcuts, but the first two are the ones I use predominantly.

    
25.02.2014 / 13:37
5

In the case of comments from a single line use the following method

Place comment symbol in Start of line

  • 0 key takes you to the beginning of the line (optional)
  • ctrl + v (stays in visual block)
  • Use the arrow keys to select lines to comment
  • Shift + i (Uppercase I)
  • A writing cursor will appear on the first line selected - write the comment symbol, i.e. '#' or '//'
  • select ESC and the lines will all be commented out
  • To remove comments from the beginning of the line

  • 0 key takes you to the beginning of the line (optional)
  • ctrl + v (stays in visual block)
  • Use the arrow keys to select lines to uncomment. If there are two symbols like '//' you can use the arrow keys to select an NxM array
  • Selecting x deletes everything within the array
  • Alternatively for two symbols ('//') you can do steps 1, 2 and 3 but select only the beginning of the lines, then do x (delete the first column),

    24.02.2014 / 18:24
    4

    I use a plugin called NERDCommenter it is pretty cool and has pre-defined shortcuts that are very easy to use , look at:

    • Comment the current line: \ c c
    • Comment several lines: \ c m
    • Undo the comments: \ c u
    • Comment and copy the current line: \ and
    The \ key is the leader key that is set by default but you can change using the

    let mapleader = "_"
    so instead of using \ c c you would use c to comment on the current line.     
    14.09.2014 / 19:21
    1

    To remove comments beginning with # :

    :%s/\s*#.*//g
    

    To remove comments beginning with // :

    :%s/\s*\/\/.*//g
    

    To remove comments blocks surrounded by /* and */ :

    :%s/\/\*.*\*\///g
    
        
    24.02.2014 / 15:54
    0

    Reply with 1 year delay ... For multi-line comments to there C (/ * comment * /)

    (I) Delete the content of a comment:

  • search the start of the comment : /\/\*
  • remove: d%
  • look for next occurrence of start of comment : n
  • remove next .
  • (II) To comment on an area there C (/ * comment * /):

  • We define a generic command to comment on a visual area (be \q )
  • Visually mark the area

  • comment the area with \q

  • To define the visual area commenter, we define a shortcut in .vimrc

    :vmap \q di/**/<ESC>hP
    

    In the background, it deletes the area ( d ), inserts ( i ) /**/ , and puts the delete again in the middle ( P ).

        
    24.09.2015 / 13:06
    0

    The most complicated in my opinion are the multiline comments, if the file has many lines stay making visual selection goes against the principles of vim.

    The Global command provides us with an elegant solution to add a character in these snippets (in this case I am putting % )

     :g/\/\*\*/,/\*\// s/^/%
    
     :g .................. comando global
     /\/\*\*/ ............ localiza /**
     , ................... indica intervalo
     /\*\// .............. localiza */
     s/^/%  .............. substitua o começo de linha "^" por "%"
    

    Doing the reverse

     g/%\/\*\*/,/%\*\// s/^%//
    

    "%" is added in the two portions of the range and the substitution is:

    s/^%//
    

    Via plugins I would recommend Tim Pope's vim-commentary , is very eficient.

        
    30.08.2017 / 15:36