In Vim, how do I insert characters at the beginning of each line in a selection?
For instance, I want to comment out a block of code by prepending // at the beginning of each line assuming my language's comment system doesn't allow block commenting like /* */. How would I do this?
/^/ is just a dummy pattern to match every line. norm lets you run the normal-mode commands that follow. I// says to enter insert-mode while jumping the cursor to the beginning of the line, then insert the following text (two slashes).
:g is often handy for doing something complex on multiple lines, where you may want to jump between multiple modes, delete or add lines, move the cursor around, run a bunch of macros, etc. And you can tell it to operate only on lines that match a pattern.
let maplocalleader=','
vmap <silent> <LocalLeader>c <Plug>VisualTraditional
nmap <silent> <LocalLeader>c <Plug>Traditional
let g:EnhCommentifyBindInInsert = 'No'
let g:EnhCommentifyMultiPartBlocks = 'Yes'
let g:EnhCommentifyPretty = 'Yes'
let g:EnhCommentifyRespectIndent = 'Yes'
let g:EnhCommentifyUseBlockIndent = 'Yes'
let maplocalleader=','
vmap <silent> <LocalLeader>c <Plug>VisualTraditional
nmap <silent> <LocalLeader>c <Plug>Traditional
let g:EnhCommentifyBindInInsert = 'No'
let g:EnhCommentifyMultiPartBlocks = 'Yes'
let g:EnhCommentifyPretty = 'Yes'
let g:EnhCommentifyRespectIndent = 'Yes'
let g:EnhCommentifyUseBlockIndent = 'Yes'
you can then comment/uncomment the (selected) lines with ',c'
Replaces the first occurrence of 'search' with 'replace' for current line
:s/search/replace/g
Replaces all occurrences of 'search' with 'replace' for the current line, 'g' is short for 'global'
This command will replace each occurrence of 'search' with 'replace' for the current line only. The % is used to search over the whole file. To confirm each replacement interactively, append a 'c' for confirm:
:%s/search/replace/c
Interactive confirm replacing 'search' with 'replace' for the entire file
Instead of the % character you can use a line number range (note that the '^' character is a special search character for the start of line):
:14,20s/^/#/
Inserts a '#' character at the start of lines 14-20
If you want to use another comment character (like //), then change your command delimiter:
:14,20s!^!//!
Inserts a '//' character sequence at the start of lines 14-20
Or you can always escape the // characters like:
:14,20s/^/\/\//
Inserts a '//' character sequence at the start of lines 14-20
If you are not seeing line numbers in your editor, simply type the following:
If you want to get super fancy about it, put this in your .vimrc:
vmap \c :s!^!//!<CR>
vmap \u :s!^//!!<CR>
Then, whenever in visual mode, you can hit \c to comment the block and \u to uncomment it. Of course, you can change those shortcut keystrokes to whatever.
发布评论
评论(14)
还有另一种方式:
/^/
只是一个匹配每一行的虚拟模式。norm
允许您运行后面的正常模式命令。I//
表示进入插入模式,同时将光标跳到行首,然后插入以下文本(两个斜杠)。:g
通常可以方便地在多行上执行复杂的操作,您可能希望在多种模式之间跳转、删除或添加行、移动光标、运行一堆宏等。可以告诉它只对与模式匹配的行进行操作。Yet another way:
/^/
is just a dummy pattern to match every line.norm
lets you run the normal-mode commands that follow.I//
says to enter insert-mode while jumping the cursor to the beginning of the line, then insert the following text (two slashes).:g
is often handy for doing something complex on multiple lines, where you may want to jump between multiple modes, delete or add lines, move the cursor around, run a bunch of macros, etc. And you can tell it to operate only on lines that match a pattern.要在每行的开头插入“ABC”:
转到命令模式
%norm I ABC
To insert "ABC" at the begining of each line:
Go to command mode
% norm I ABC
对于注释代码块,我喜欢 NERD Commenter 插件。
选择一些文本:
注释:
取消注释:
或者只是切换行或块的注释状态:
For commenting blocks of code, I like the NERD Commenter plugin.
Select some text:
Comment:
Uncomment:
Or just toggle the comment state of a line or block:
我可以推荐 EnhCommentify 插件。
例如。 将其放入您的 vimrc:
然后您可以使用 ',c' 注释/取消注释(选定的)行
I can recommend the EnhCommentify plugin.
eg. put this to your vimrc:
you can then comment/uncomment the (selected) lines with ',c'
将要注释的区域标记为可视块 ()
并执行
c#p
c
将其更改为“#”在 .vimrc 中定义一个快捷方式(例如
\q
)Mark the area to be comment as a visual block (
<C-V
)and do
c#<ESC>p
c
hange it to "#"If you do it often, define a short cut (example
\q
) in your .vimrc如果某人的多行选择实际上是一个段落,则无需手动选择行。 vim 可以为您做到这一点:
vip
:选择并标记整个段落shift-i
:在行开头插入文本escape
:离开插入模式/进入正常模式[行开头仍处于选中状态]escape
:取消选择行开头In case someone's multi-line-selection is actually a paragraph, there is no need to manually select the lines. vim can do that for you:
vip
: select and mark the whole paragraphshift-i
: insert text at line beginningescape
: leave insert mode/enter normal mode [line beginnings still selected]escape
: unselect line beginnings投票最多答案的映射:
第一个视觉选择所需的行,然后执行
zzz
,其值:
进入视觉模式^
转到行首(或使用“0
”到第一个非空白)I
以块模式插入- 插入“- ”(例如,根据需要进行编辑)
将相同的插入应用于所有视觉块行或正常模式下的最后一个视觉选择:
Mapping of most voted answer:
1st visual select the desired lines, then execute
<leader>zzz
, which values:<C-V>
to enter visual mode^
goes to start of line ( or use '0
' to 1st non blank)I
to insert in block mode-<Space>
to insert '- ' (for example, edit as you need)<Esc>
to apply same insert to all visual block linesOr of last visual selection from normal mode:
评论。
有关更多信息和阅读,请查看 Vim Tips Wiki 中的“在多行中插入文本”。
comment.
For further information and reading, check out "Inserting text in multiple lines" in the Vim Tips Wiki.
这会将每行的开头替换为“//”:
这会将每个选定行的开头(使用可视模式进行选择)替换为“//”:
注意,
gv
(在正常模式下)恢复最后的视觉选择,这有时会派上用场。This replaces the beginning of each line with "//":
This replaces the beginning of each selected line (use visual mode to select) with "//":
Note that
gv
(in normal mode) restores the last visual selection, this comes in handy from time to time.搜索和替换的一般模式是:
将当前行的第一次出现的“搜索”替换为“替换”
将当前行的所有出现的“搜索”替换为“替换”, 'g' 是 'global' 的缩写
此命令将仅在当前行将每次出现的 'search' 替换为 'replace'。 % 用于搜索整个文件。 要以交互方式确认每个替换,请附加“c”进行确认:
交互式确认将整个文件的“搜索”替换为“替换”
您可以使用行号范围来代替 % 字符 (请注意,“^”字符是行开头的特殊搜索字符):
在第 14-20 行开头插入“#”字符
如果您想使用另一个注释字符(例如 //),然后更改命令分隔符:
在第 14-20 行开头插入“//”字符序列
或者您始终可以转义 // 字符,例如:
< em>在第 14-20 行的开头插入“//”字符序列
如果您在编辑器中没有看到行号,只需键入以下内容:
The general pattern for search and replace is:
Replaces the first occurrence of 'search' with 'replace' for current line
Replaces all occurrences of 'search' with 'replace' for the current line, 'g' is short for 'global'
This command will replace each occurrence of 'search' with 'replace' for the current line only. The % is used to search over the whole file. To confirm each replacement interactively, append a 'c' for confirm:
Interactive confirm replacing 'search' with 'replace' for the entire file
Instead of the % character you can use a line number range (note that the '^' character is a special search character for the start of line):
Inserts a '#' character at the start of lines 14-20
If you want to use another comment character (like //), then change your command delimiter:
Inserts a '//' character sequence at the start of lines 14-20
Or you can always escape the // characters like:
Inserts a '//' character sequence at the start of lines 14-20
If you are not seeing line numbers in your editor, simply type the following:
对于新手来说可能更容易的另一种方法:
将光标放在第一行,例如通过
gg
并键入以下内容进入插入模式并添加文本:
< kbd>I / / 空格
按 Esc 返回命令模式并使用二合字母:
j . j .
j 是向下一行的运动命令和 . 重复您执行的最后一个编辑命令。
Another way that might be easier for newcomers:
Place the cursor on the first line, e.g. by
gg
and type the following to get into insert mode and add your text:
I / / Space
Press Esc to get back to command mode and use the digraph:
j . j .
j is a motion command to go down one line and . repeats the last editing command you made.
还有另一种方法:
And yet another way:
这会在每行的开头添加
#
:人们将不再抱怨您缺乏正确的注释脚本。
This adds
#
at the beginning of every line:And people will stop complaining about your lack of properly commenting scripts.
如果你想要超级喜欢它,请将其放入你的 .vimrc 中:
然后,无论何时在可视模式下,你都可以点击
\c
来注释块并 < code>\u 来u对其进行注释。 当然,您可以将这些快捷键更改为任何内容。If you want to get super fancy about it, put this in your .vimrc:
Then, whenever in visual mode, you can hit
\c
to comment the block and\u
to uncomment it. Of course, you can change those shortcut keystrokes to whatever.