How is an agile way to add and remove code comments in VIM?

Asked

Viewed 3,344 times

10

I don’t want to remove the entire line of code in VIM, what I want is to work with the comment codes. I have 3 purpose cases:

Comments with which 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 would like to know an agile way to add and remove these types of comments in VIM, so that it applies only to selective lines of code and not to the entire file.

6 answers

5

In the case of single line comments I use the following method

Place comment symbol on Start of line

  1. key 0 takes it to the beginning of the line (optional)
  2. keystrokes Ctrl+v (is in visual block)
  3. with the arrow keys selects the lines to comment on
  4. Shift+i (I capital)
  5. A writing cursor will appear on the first selected line- write the comment symbol, i.e. '#' or '//'
  6. select ESC and the lines will be all commented

To remove comments from line start

  1. key 0 takes it to the beginning of the line (optional)
  2. keystrokes Ctrl+v (is in visual block)
  3. with the arrow keys selects the lines to uncomment. In case there are two symbols like in '//' you can use the directional keys to select an Nxm matrix
  4. Select x erases everything inside the matrix

alternatively for two symbols ('/') you can do steps 1, 2 and 3 but select only the beginning of the lines, then do x(deletes the first column), f+g (selects the same visual block area) and then x(deletes the second column)

To make multi-line comments a lá C (/* coment */) I don’t have an immediate way, but with the above method I rarely need to use multi-line comments

  • didn’t know, so +1 for learning! :)

  • +1 I really liked the answer, I’ll give a little more time to see if someone comes up with a multi-line pro solution too, otherwise I will take for granted your solution, because it already solves 90% of my cases :)

  • Yeah, vertical selection helps to comment blocks with line comments. When it’s just a line, you tbm can use I, that enters insertion mode with the cursor at the beginning of the current line. I used these methods for some time, but today I prefer tcomment.

4


I suggest using the plugin tcomment, providing unique shortcuts to comment/uncomment in multiple languages.

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

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

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

  • +1 I will test this plugin not yet known, seems very promising!

4

I use a plugin called Nerdcommenter he’s pretty cool and he’s got easy-to-use predefined shortcuts, check it out:

  • Comment on the current line: \cc
  • Comment multiple lines: \cm
  • Undo the comments: \cu
  • Comment and copy the current line: \cy

The key \ is my key Leader which comes set by default but, you can change using the command

let mapleader = "_"
rather than use \cc you would use _cc, to comment on the current line.

1

To remove comments starting at #:

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

To remove comments starting at //:

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

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

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

    Cara is a good solution use substitution, but I think I expressed myself badly, I do not want to remove all comments from the code, I want to be selective a few lines, to easily make a toggle, I will edit the question, but I gave you +1

0

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

(I) Delete the content of a comment:

  1. look for the start of comment: /\/\*
  2. remove: d%
  3. look for next occurrence of start of comment: n
  4. remove next .

(II) To comment on an area there C (/* coment */):

  1. we define a generic command to comment on a visual area (i.e. \q)
  2. visually mark the area

  3. comment the area with \q

To define the visual area commentator, we set a shortcut on .vimrc

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

which in the background erases the area (d), inserts (i) /**/, and put the blank back in the middle (P).

0

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

The Global command provides us with an elegant solution to add a character to 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 to the two portions of the interval and the replacement is:

s/^%//

Via plugins I would recommend the vim-Commentary of Tim Pope, it’s very efficient.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.