So I currently like this solution to commenting multiple lines in vim:
- Press
CTRL-v
(to go into Visual Block mode) - Select the lines you want to comment
- Press
Shift-i
(to go into Insert mode) - Type whatever comment characters your language uses
- Press
ESC ESC
(pressing the escape key twice makes the results appear faster)
But I would like some help mapping these steps into my vimrc file. I currently use the following to comment lines out:
vnoremap ;/ <C-v>0I// <ESC>
For those who want an explanation of what the command does:
You basically type ;/
when you're in Visual mode to use this (Visual, Visual Line, and Visual Block mode all work since the <C-v>
part forces you into Visual Block mode, which is correct).
The 0I
part will put you in Insert mode at the beginning of the line.
The // <ESC>
part will insert the comment characters //
and put you back into Normal mode.
The part I need help with is uncommenting the lines. How do I write a function in my vimrc that will basically let me toggle the //
characters?
Ideally, the solution would involve the following:
- Selecting the lines
- Pressing
;/
- If there are NO
//
characters then it will insert them - If there ARE
//
characters then it will remove them