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

发布于 2024-07-08 21:11:12 字数 225 浏览 10 评论 0 原文

Vim 中,如何在所选内容的每行开头插入字符?

例如,我想通过在每行开头添加 // 来注释掉一段代码,假设我的语言的注释系统不允许像 /* */. 我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

赠我空喜 2024-07-15 21:11:13

还有另一种方式:

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

/^/ 只是一个匹配每一行的虚拟模式。 norm 允许您运行后面的正常模式命令。 I// 表示进入插入模式,同时将光标跳到行首,然后插入以下文本(两个斜杠)。

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

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

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

青巷忧颜 2024-07-15 21:11:13

要在每行的开头插入“ABC”:

  1. 转到命令模式

  2. %norm I ABC

To insert "ABC" at the begining of each line:

  1. Go to command mode

  2. % norm I ABC

回心转意 2024-07-15 21:11:13

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

选择一些文本:

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

注释:

,cc

取消注释:

,cu

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

,c<space>

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

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>
灯下孤影 2024-07-15 21:11:13

我可以推荐 EnhCommentify 插件。

例如。 将其放入您的 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'

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

I can recommend the EnhCommentify plugin.

eg. put this to your 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'

雄赳赳气昂昂 2024-07-15 21:11:13

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

并执行 c#p

  1. c 将其更改为“#”
  2. 如果你经常这样做,请

在 .vimrc 中定义一个快捷方式(例如 \q

:vmap \q c#<ESC>p

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

and do c#<ESC>p

  1. change it to "#"
  2. put it back

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

:vmap \q c#<ESC>p
临走之时 2024-07-15 21:11:13

如果某人的多行选择实际上是一个段落,则无需手动选择行。 vim 可以为您做到这一点:

  1. vip:选择并标记整个段落
  2. shift-i:在行开头插入文本
  3. escape:离开插入模式/进入正常模式[行开头仍处于选中状态]
  4. 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:

  1. vip: select and mark the whole paragraph
  2. shift-i: insert text at line beginning
  3. escape: leave insert mode/enter normal mode [line beginnings still selected]
  4. escape: unselect line beginnings
空名 2024-07-15 21:11:13

投票最多答案的映射:

第一个视觉选择所需的行,然后执行 zzz,其值:

vnoremap <leader>zzz <C-V>^I-<Space><Esc>
  • 进入视觉模式
  • ^ 转到行首(或使用“0”到第一个非空白)
  • I 以块模式插入
  • - 插入“- ”(例如,根据需要进行编辑)
  • 将相同的插入应用于所有视觉块行

或正常模式下的最后一个视觉选择:

nnoremap <leader>zzz gv<C-V>^I-<Space><Esc>

Mapping of most voted answer:

1st visual select the desired lines, then execute <leader>zzz, which values:

vnoremap <leader>zzz <C-V>^I-<Space><Esc>
  • <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 lines

Or of last visual selection from normal mode:

nnoremap <leader>zzz gv<C-V>^I-<Space><Esc>
你丑哭了我 2024-07-15 21:11:12
  1. Esc 进入“命令模式”
  2. 使用 Ctrl+V 进入视觉块模式
  3. 移动 向上/向下 选择您想要的行中的文本列
    评论。
  4. 然后按 Shift+i 并键入要插入的文本。
  5. 然后按Esc,等待1秒,插入的文本将出现在每一行上。

有关更多信息和阅读,请查看 Vim Tips Wiki 中的“在多行中插入文本”。

  1. Press Esc to enter 'command mode'
  2. Use Ctrl+V to enter visual block mode
  3. Move Up/Down to select the columns of text in the lines you want to
    comment.
  4. Then hit Shift+i and type the text you want to insert.
  5. Then hit Esc, wait 1 second and the inserted text will appear on every line.

For further information and reading, check out "Inserting text in multiple lines" in the Vim Tips Wiki.

尸血腥色 2024-07-15 21:11:12

这会将每行的开头替换为“//”:

:%s!^!//!

这会将每个选定行的开头(使用可视模式进行选择)替换为“//”:

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

注意gv(在正常模式下)恢复最后的视觉选择,这有时会派上用场。

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.

叶落知秋 2024-07-15 21:11:12

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

:s/search/replace/

将当前行的第一次出现的“搜索”替换为“替换”

:s/search/replace/g

将当前行的所有出现的“搜索”替换为“替换”, 'g' 是 'global' 的缩写

此命令将仅在当前行将每次出现的 'search' 替换为 'replace'。 % 用于搜索整个文件。 要以交互方式确认每个替换,请附加“c”进行确认:

:%s/search/replace/c

交互式确认将整个文件的“搜索”替换为“替换”

您可以使用行号范围来代替 % 字符 (请注意,“^”字符是行开头的特殊搜索字符)

:14,20s/^/#/

在第 14-20 行开头插入“#”字符

如果您想使用另一个注释字符(例如 //),然后更改命令分隔符:

:14,20s!^!//!

在第 14-20 行开头插入“//”字符序列

或者您始终可以转义 // 字符,例如:

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

< em>在第 14-20 行的开头插入“//”字符序列

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

:set nu

The general pattern for search and replace is:

:s/search/replace/

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:

:set nu
浪漫之都 2024-07-15 21:11:12

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

 some█
 code
 here

将光标放在第一行,例如通过

gg

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

< kbd>I / / 空格

 // █some
 code
 here

Esc 返回命令模式并使用二合字母:

j . j .

 // some
 // code
 //█here

j 是向下一行的运动命令和 . 重复您执行的最后一个编辑命令。

Another way that might be easier for newcomers:

 some█
 code
 here

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

 // █some
 code
 here

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

j . j .

 // some
 // code
 //█here

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

断桥再见 2024-07-15 21:11:12

还有另一种方法:

  • 移至行首
  • 进入可视块模式 (CTRL-v)
  • 选择所需的行(使用 j 向上/向下移动/k,或使用 [line]G 跳转到一行)
  • I(即大写 i)
  • 键入注释字符(s)
  • ESC

And yet another way:

  • Move to the beginning of a line
  • enter Visual Block mode (CTRL-v)
  • select the lines you want (moving up/down with j/k, or jumping to a line with [line]G)
  • press I (that's capital i)
  • type the comment character(s)
  • press ESC
挽你眉间 2024-07-15 21:11:12

这会在每行的开头添加 #

:%s/^/#/

人们将不再抱怨您缺乏正确的注释脚本。

This adds # at the beginning of every line:

:%s/^/#/

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

旧竹 2024-07-15 21:11:12

如果你想要超级喜欢它,请将其放入你的 .vimrc 中:

vmap \c :s!^!//!<CR>
vmap \u :s!^//!!<CR>

然后,无论何时在可视模式下,你都可以点击 \c注释块并 < code>\u 来u对其进行注释。 当然,您可以将这些快捷键更改为任何内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文