如何在vi/Vim中的多行选择开始处插入文本?

时间:2021-09-14 22:30:01

In Vim, how would I go about inserting characters at the beginning of each line in a selection?

在Vim中,如何在选择的每一行的开头插入字符?

For instance, let's say 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?

例如,假设我想在每一行的开头加上'/ '来注释一段代码(假设我的语言的注释系统不允许像/* /这样的块注释)。我该怎么做呢?

11 个解决方案

#1


556  

  • Press Esc to enter 'command mode'
  • 按Esc进入“命令模式”
  • Use Ctrl+V to enter visual block mode
  • 使用Ctrl+V进入visual block模式
  • Move Up/Downto select the columns of text in the lines you want to comment.
  • 向上/向下移动以选择要注释的行中的文本列。
  • Then hit Shift+i and type the text you want to insert.
  • 然后点击Shift+i,输入要插入的文本。
  • Then hit Esc, wait 1 second and the inserted text will appear on every line.
  • 然后点击Esc,等待1秒,插入的文本将出现在每一行。

#2


151  

This replaces the beginning of each line with "//":

用“//”替换每行的开头:

:%s!^!//!

This replaces the beginning of each selected line (use visual mode to select) with "//":

这将用“//”替换每一行的开头(使用可视模式选择):

:'<,'>s!^!//!

Note that gv (in normal mode) restores the last visual selection, this comes in handy from time to time.

请注意,gv(在正常模式下)会恢复最后的视觉选择,这有时很方便。

#3


65  

The general pattern for search and replace is:

搜索和替换的一般模式是:

:s/search/replace/

Replaces the first occurrence of 'search' with 'replace' for current line

将第一个出现的“search”替换为当前行的“replace”

:s/search/replace/g

Replaces all occurrences of 'search' with 'replace' for current line, 'g' is short for 'global'

将所有出现的“search”替换为当前行中的“replace”,“g”是“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:

此命令将只对当前行使用“replace”替换“search”的每次出现。%用于搜索整个文件。为了确认每一个替换项,在后面加上“c”以确认:

:%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

在第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

在第14-20行开头插入一个'//'字符序列

Or you can always just escape the // characters like:

或者你可以转义//字符,比如:

:14,20s/^/\/\//

Inserts a '//' character sequence at the start of lines 14-20

在第14-20行开头插入一个'//'字符序列

If you are not seeing line numbers in your editor, simply type the following

如果在编辑器中没有看到行号,只需输入以下内容

:set nu

#4


31  

Another way that might be easier for newcomers:

另一种对新来者来说可能更容易的方法是:

 some█
 code
 here

Place the cursor on the first line, e.g. by

把光标放在第一行,例如:by

: 1 Enter

:1进入

and type the following to get into insert mode and add your text:

输入以下内容进入插入模式并添加文本:

I / / Space

我/ /空间

 // █some
 code
 here

Press Esc to get back to command mode and use the digraph:

按Esc回到命令模式,使用有向图:

j . j .

j。j。

 // some
 // code
 //█here

j is a motion command to go down one line and . repeats the last editing command you made.

j是一个运动命令,沿着一条直线。重复最后一次编辑命令。

#5


21  

And yet another way:

另一种方法:

  • Move to the beginning of a line
  • 移动到一行的开头
  • enter Visual Block mode (CTRL-v)
  • 输入Visual Block模式(CTRL-v)
  • select the lines you want (moving up/down with j/k, or jumping to a line with [line]G)
  • 选择你想要的线条(用j/k上下移动,或者用[line]G调到一行)
  • press I (that's capital i)
  • 按I(即大写I)
  • type the comment character(s)
  • 注释字符型(s)
  • press ESC
  • 按ESC

#6


16  

This adds # at the beginning of every line:

这在每行开头都加了#:

:%s/^/#/

And people will stop complaining about your lack of properly commenting scripts.

人们也不会再抱怨你没有正确的注释脚本。

#7


14  

If you want to get super fancy about it, put this in your .vimrc:

如果你想特别喜欢它,把它写在你的。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.

然后,无论何时在可视模式下,您都可以按下\c对块进行注释,然后按下\u取消注释。当然,您可以将这些快捷键击更改为任何类型。

#8


8  

Yet another way:

另一种方法:

:'<,'>g/^/norm I//

/^/ 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).

/ ^ /只是一个虚拟的模式匹配的每一行。norm让您运行正常模式命令。我//说进入插入模式,同时将光标移到行的开头,然后插入下面的文本(两个斜杠)。

: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.

:g通常用于在多行上执行复杂的操作,您可能希望在多个模式之间跳转、删除或添加行、移动光标、运行一系列宏,等等。您可以告诉它只在与模式匹配的行上操作。

#9


3  

For commenting blocks of code, I like the NERD Commenter plugin.

对于代码块的注释,我喜欢NERD Commenter插件。

Select some text:

选择一些文本:

Shift-V
...select the lines of text you want to comment....

Comment:

备注:

,cc

Uncomment:

取消:

,cu

Or just toggle the comment state of a line or block:

或者只是切换行或块的注释状态:

,c<space>

#10


2  

I can recommend the EnhCommentify plugin.

我可以推荐增强插件。

eg. put this to your vimrc:

如。把这个给你的vimrc:

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'

然后,您可以使用',c'对(选定的)行进行注释/取消注释

#11


1  

Mark the area to be comment as a visual block (<C-V)

将要注释的区域标记为可视块( )

and do c#<ESC>p

c# < ESC > p

  1. change it to "#"
  2. 将其更改为“#”
  3. put it back
  4. 把它放回去

If you do it often, define a short cut (example \q) in your .vimrc

如果你经常这样做,在你的。vimrc中定义一个捷径(例如q)

:vmap \q c#<ESC>p

#1


556  

  • Press Esc to enter 'command mode'
  • 按Esc进入“命令模式”
  • Use Ctrl+V to enter visual block mode
  • 使用Ctrl+V进入visual block模式
  • Move Up/Downto select the columns of text in the lines you want to comment.
  • 向上/向下移动以选择要注释的行中的文本列。
  • Then hit Shift+i and type the text you want to insert.
  • 然后点击Shift+i,输入要插入的文本。
  • Then hit Esc, wait 1 second and the inserted text will appear on every line.
  • 然后点击Esc,等待1秒,插入的文本将出现在每一行。

#2


151  

This replaces the beginning of each line with "//":

用“//”替换每行的开头:

:%s!^!//!

This replaces the beginning of each selected line (use visual mode to select) with "//":

这将用“//”替换每一行的开头(使用可视模式选择):

:'<,'>s!^!//!

Note that gv (in normal mode) restores the last visual selection, this comes in handy from time to time.

请注意,gv(在正常模式下)会恢复最后的视觉选择,这有时很方便。

#3


65  

The general pattern for search and replace is:

搜索和替换的一般模式是:

:s/search/replace/

Replaces the first occurrence of 'search' with 'replace' for current line

将第一个出现的“search”替换为当前行的“replace”

:s/search/replace/g

Replaces all occurrences of 'search' with 'replace' for current line, 'g' is short for 'global'

将所有出现的“search”替换为当前行中的“replace”,“g”是“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:

此命令将只对当前行使用“replace”替换“search”的每次出现。%用于搜索整个文件。为了确认每一个替换项,在后面加上“c”以确认:

:%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

在第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

在第14-20行开头插入一个'//'字符序列

Or you can always just escape the // characters like:

或者你可以转义//字符,比如:

:14,20s/^/\/\//

Inserts a '//' character sequence at the start of lines 14-20

在第14-20行开头插入一个'//'字符序列

If you are not seeing line numbers in your editor, simply type the following

如果在编辑器中没有看到行号,只需输入以下内容

:set nu

#4


31  

Another way that might be easier for newcomers:

另一种对新来者来说可能更容易的方法是:

 some█
 code
 here

Place the cursor on the first line, e.g. by

把光标放在第一行,例如:by

: 1 Enter

:1进入

and type the following to get into insert mode and add your text:

输入以下内容进入插入模式并添加文本:

I / / Space

我/ /空间

 // █some
 code
 here

Press Esc to get back to command mode and use the digraph:

按Esc回到命令模式,使用有向图:

j . j .

j。j。

 // some
 // code
 //█here

j is a motion command to go down one line and . repeats the last editing command you made.

j是一个运动命令,沿着一条直线。重复最后一次编辑命令。

#5


21  

And yet another way:

另一种方法:

  • Move to the beginning of a line
  • 移动到一行的开头
  • enter Visual Block mode (CTRL-v)
  • 输入Visual Block模式(CTRL-v)
  • select the lines you want (moving up/down with j/k, or jumping to a line with [line]G)
  • 选择你想要的线条(用j/k上下移动,或者用[line]G调到一行)
  • press I (that's capital i)
  • 按I(即大写I)
  • type the comment character(s)
  • 注释字符型(s)
  • press ESC
  • 按ESC

#6


16  

This adds # at the beginning of every line:

这在每行开头都加了#:

:%s/^/#/

And people will stop complaining about your lack of properly commenting scripts.

人们也不会再抱怨你没有正确的注释脚本。

#7


14  

If you want to get super fancy about it, put this in your .vimrc:

如果你想特别喜欢它,把它写在你的。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.

然后,无论何时在可视模式下,您都可以按下\c对块进行注释,然后按下\u取消注释。当然,您可以将这些快捷键击更改为任何类型。

#8


8  

Yet another way:

另一种方法:

:'<,'>g/^/norm I//

/^/ 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).

/ ^ /只是一个虚拟的模式匹配的每一行。norm让您运行正常模式命令。我//说进入插入模式,同时将光标移到行的开头,然后插入下面的文本(两个斜杠)。

: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.

:g通常用于在多行上执行复杂的操作,您可能希望在多个模式之间跳转、删除或添加行、移动光标、运行一系列宏,等等。您可以告诉它只在与模式匹配的行上操作。

#9


3  

For commenting blocks of code, I like the NERD Commenter plugin.

对于代码块的注释,我喜欢NERD Commenter插件。

Select some text:

选择一些文本:

Shift-V
...select the lines of text you want to comment....

Comment:

备注:

,cc

Uncomment:

取消:

,cu

Or just toggle the comment state of a line or block:

或者只是切换行或块的注释状态:

,c<space>

#10


2  

I can recommend the EnhCommentify plugin.

我可以推荐增强插件。

eg. put this to your vimrc:

如。把这个给你的vimrc:

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'

然后,您可以使用',c'对(选定的)行进行注释/取消注释

#11


1  

Mark the area to be comment as a visual block (<C-V)

将要注释的区域标记为可视块( )

and do c#<ESC>p

c# < ESC > p

  1. change it to "#"
  2. 将其更改为“#”
  3. put it back
  4. 把它放回去

If you do it often, define a short cut (example \q) in your .vimrc

如果你经常这样做,在你的。vimrc中定义一个捷径(例如q)

:vmap \q c#<ESC>p