注释多行Vimrc的功能 [英] Function to Comment Multiple Lines Vimrc
问题描述
因此,我目前喜欢这种在vim中注释多行的解决方案:
So I currently like this solution to commenting multiple lines in vim:
- 按
CTRL-v
(进入可视块模式) - 选择要注释的行
- 按
Shift -i
(进入插入模式) - 键入您的语言使用的注释字符
- 按
ESC ESC
(按两次退出键可使结果显示更快)
- 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)
但是我想一些帮助将这些步骤映射到我的vimrc文件中。
我目前使用以下注释行:
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:
您基本上在Visual模式下键入; /
即可使用此功能(Visual,Visual Line和Visual Block模式自< C-v>
部分将您强制进入Visual Block模式,这是正确的)。
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).
0I
部分将使您在行首进入插入模式。
The 0I
part will put you in Insert mode at the beginning of the line.
//< ESC>
部分将插入注释字符 //
,然后让您回到普通模式。
The // <ESC>
part will insert the comment characters //
and put you back into Normal mode.
我需要帮助的部分是取消注释。如何在vimrc中编写一个基本上可以切换 //
字符的函数?
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
推荐答案
将此内容放入您的 .vimrc
文件:
Put this in your .vimrc
file:
vnoremap <silent> ;/ :call ToggleComment()<cr>
function! ToggleComment()
if matchstr(getline(line(".")),'^\s*\/\/.*$') == ''
:execute "s:^://:"
else
:execute "s:^\s*//::"
endif
endfunction
这篇关于注释多行Vimrc的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!