Comment & Uncomment multiple lines in Vim
Baba Gyani Triviani said:
"When someone said 'No Comments' I just had to intervene. No comments? Have multi-line comments my friend!"
Commenting and uncommenting multiple lines in Vim can be a little tricky. Here is a small snippet of code to show how you can do this. I have done this for just a few of languages. But you can extend this to any number as per your choice.
Save the following into a file and call it vcomments.vim
Once this is done, open your ~/.vimrc file if it exists, or create a new one. There, put the following code.
:source ~/vcomments.vim
:map <C-a> :call Comment() <Enter>
:map <C-b> :call Uncomment() <Enter>
Where you can replace a and b of and by whatever is convenient to you. This just calls the functions when the Ctrl + <key> combination is hit.
To use it, just select the lines to comment by using Shift + v and moving cursor up or down to select lines. Then, hit the Ctrl + <key> combination to comment. Similarly, you can uncomment them
Note - This is my first attempt at Vimscript - So it may not really be all that graceful in case of issues :-) But do let me know when you hit problems so that I can put in a bunch of fixes.
***
You may also like to read
Common Vim Mappings to make your life easier (For Beginners)
Vim Editor - Some cool features (Relatively Advanced)
"When someone said 'No Comments' I just had to intervene. No comments? Have multi-line comments my friend!"
Commenting and uncommenting multiple lines in Vim can be a little tricky. Here is a small snippet of code to show how you can do this. I have done this for just a few of languages. But you can extend this to any number as per your choice.
Save the following into a file and call it vcomments.vim
Once this is done, open your ~/.vimrc file if it exists, or create a new one. There, put the following code.
:source ~/vcomments.vim
:map <C-a>
:map <C-b> :call Uncomment() <Enter>
Where you can replace a and b of
To use it, just select the lines to comment by using Shift + v and moving cursor up or down to select lines. Then, hit the Ctrl + <key> combination to comment. Similarly, you can uncomment them
Note - This is my first attempt at Vimscript - So it may not really be all that graceful in case of issues :-) But do let me know when you hit problems so that I can put in a bunch of fixes.
***
You may also like to read
Common Vim Mappings to make your life easier (For Beginners)
Vim Editor - Some cool features (Relatively Advanced)
Comments
>> go to the beginning of the first line where you want to start commenting.
>> hit ctrl + v, Then press down arrow repeatedly until you have reached the beginning of the last line you want to comment.
>> then hit shift+i. This will take the cursor back to the first line. Then, simply type in the symbols for comment like // or # or whatever, and then hit escape.
Sounds complex, but really easy.
Suppose you had 3 groups of 10 lines in different parts of the file, and you commented the first set using the method above, you can then comment the other lines by just going to the starting line of each of those groups and simply pressing "."
"." will redo the most recent operation, whatever that was
Two advantages - One, I don't need to go to the beginning of the line. I can start selection anywhere - the comments are added in the beginning.
Secondly, only key combination is simpler than bunch of key strokes and I don't have to think what's the comment character once I have set it up. :-) At least for me, these two are bigger advantages. Thanks for the comment though.