有时我想跨多行编辑某个可视文本块。

例如,我会取一个看起来像这样的文本:

name
comment
phone
email

让它看起来像这样

vendor_name
vendor_comment
vendor_phone
vendor_email

现在我要做的是…

Select all 4 row lines of a block by pressing V and then j four times. Indent with >. Go back one letter with h. Go to block visual mode with Ctrlv. Select down four rows by pressing j four times. At this point you have selected a 4x1 visual blocks of whitespace (four rows and one column). Press C. Notice this pretty much indented to the left by one column. Type out a " vendor_" without the quote. Notice the extra space we had to put back. Press Esc. This is one of the very few times I use Esc to get out of insert mode. Ctrlc would only edit the first line. Repeat step 1. Indent the other way with <.

如果单词前至少有一列空白,我就不需要缩进。如果我不需要用c清除可视块,我就不需要空白。

但是如果我必须清除,那么是否有一种方法来做我上面所做的而不创建所需的缩进空白?

还有为什么一次编辑多行只能通过退出插入模式与Esc在Ctrlc?


这里有一个更复杂的例子:

name    = models.CharField( max_length = 135 )
comment = models.TextField( blank = True )
phone   = models.CharField( max_length = 135, blank = True )
email   = models.EmailField( blank = True )

to

name    = models.whatever.CharField( max_length = 135 )
comment = models.whatever.TextField( blank = True )
phone   = models.whatever.CharField( max_length = 135, blank = True )
email   = models.whatever.EmailField( blank = True )

在这个例子中,我将在.上执行垂直可视块,然后在插入模式下重新插入它,即输入.whatever..希望现在你能看到这种方法的缺点。我只能选择一列在垂直位置相同的文本。


当前回答

我想在只有vi(没有nano)的服务器上注释掉一些配置文件中的很多行,所以可视化方法也很麻烦 我是这样做的。

打开vi文件 显示行号:设置行号!或:设置数字 然后使用行号将行首替换为“#”,如何?

: 35, 77年代/ ^ / # /

注:数字包含在内,行数从35行到77行,都包含将被修改。

要取消注释/撤销,只需使用:35,77s/^#//

如果你想在每一行代码后添加一个文本单词作为注释,你也可以使用:

:35,77s/$/#test/(适用于Python等语言)

:35,77s/;$/;\/\/test/(适用于Java等语言)

积分/参考资料:

https://unix.stackexchange.com/questions/84929/uncommenting-multiple-lines-of-code-specified-by-line-numbers-using-vi-or-vim https://unix.stackexchange.com/questions/120615/how-to-comment-multiple-lines-at-once

其他回答

我将使用宏来记录我的操作,然后重复它。

把光标放在名字的第一个字母上。 点击qq开始记录到q缓冲区。 按i进入插入模式,输入vector_,然后按Esc退出插入模式。 现在按0返回到行开头。 现在按下j。 现在再次按q停止录制。

现在你有了一个漂亮的宏。

输入3@q来执行宏三次以完成其余的行。

将光标移动到name的n处。 进入可视块模式(Ctrlv)。 按j三次(或3j)向下跳转3行;G(大写G)跳到最后一行 按I(大写I)。 输入vendor_。注意:它只会更新屏幕的第一行——直到按下Esc键(6.),这时所有的行都将被更新。 按Esc。

必须使用大写的I而不是小写的I,因为小写的I被解释为文本对象的开始,这本身非常有用,例如用于在标记块(it)中选择:

我想在只有vi(没有nano)的服务器上注释掉一些配置文件中的很多行,所以可视化方法也很麻烦 我是这样做的。

打开vi文件 显示行号:设置行号!或:设置数字 然后使用行号将行首替换为“#”,如何?

: 35, 77年代/ ^ / # /

注:数字包含在内,行数从35行到77行,都包含将被修改。

要取消注释/撤销,只需使用:35,77s/^#//

如果你想在每一行代码后添加一个文本单词作为注释,你也可以使用:

:35,77s/$/#test/(适用于Python等语言)

:35,77s/;$/;\/\/test/(适用于Java等语言)

积分/参考资料:

https://unix.stackexchange.com/questions/84929/uncommenting-multiple-lines-of-code-specified-by-line-numbers-using-vi-or-vim https://unix.stackexchange.com/questions/120615/how-to-comment-multiple-lines-at-once

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

Ctrl + v进入可视块模式 使用向上和向下箭头选择线条 输入小写的3i(按小写的I三次) I(按大写I,进入插入模式。) 写下你想要添加的文本 Esc 按下箭头