What is your favorite way to comment several lines in Vim?
It happens more often than not that I have to comment several lines at once in Vim. Methods that I know are not as fast as say TextMate way to comment lines out.
What is your favorite way to do it?
I currently use:
Method 1:
#
for Ruby) Method 2:
:
which gives you a :'<,'>
prompt which you can extend to :'<,'>s/^/#/
Method 3:
mm
:'m,.s/^/#/
I like method 1 the most but I still hope there is a better way.
I think you described the most popular ways to comment code, but if you are open to use Vim Scripts, give a look to those:
I use a keymap for the regex part, but I do the same visual selection first. Usually using:
vip
to get the visual block (paragraph visual selection)
then using
cc
co
for comment add/remove (cc,co chosen for muscle memory reasons)
with the mappings defined in .vimrc as:
vmap <leader>cc :s/^/#/<cr>
vmap <leader>co :s/^#//<cr>
Though this is rather old I just wanted to add my solution which is pretty similar to everyone elses but adds the unhighlighting function. In my .vimrc file I have the following maps:
:vmap `c :s/^//*/<cr>gv:s/$/*//<cr>:noh<cr>i
:vmap `r :s/^/*//<cr>gv:s/*/$/<cr>:noh<cr>i
Note: I use /*line of code*/ style of commenting to be compatible with old c code. In vim I simply highlight the lines and push `c to comment and `r to remove comments.
链接地址: http://www.djcxy.com/p/8694.html下一篇: 您最喜欢在Vim中评论多行的方式是什么?