Linux 如何在vi编辑器中删除选定的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3114936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:10:20  来源:igfitidea点击:

How to delete selected text in the vi editor

linuxtext-editorvi

提问by

I am using PuTTY and the vi editor. If I select five lines using my mouse and I want to delete those lines, how can I do that?

我正在使用 PuTTY 和 vi 编辑器。如果我使用鼠标选择了五行并且我想删除这些行,我该怎么做?

Also, how can I select the lines using my keyboard as I can in Windows where I press Shiftand move the arrows to select the text? How can I do that in vi?

另外,如何使用键盘选择行,就像在 Windows 中一样,我可以按下Shift并移动箭头来选择文本?我怎么能在vi中做到这一点?

采纳答案by Pascal Thivent

I am using PuTTY and the vi editor. If I select five lines using my mouse and I want to delete those lines, how can I do that?

我正在使用 PuTTY 和 vi 编辑器。如果我使用鼠标选择了五行并且我想删除这些行,我该怎么做?

Forget the mouse. To remove 5 lines, either:

忘记鼠标。要删除 5 行,请执行以下任一操作:

  • Go to the first line and type d5d(dddeletes one line, d5ddeletes 5 lines) ~or~
  • Type Shift-vto enter linewise selection mode, then move the cursor down using j(yes, use h, j, kand lto move left, down, up, rightrespectively, that's much more efficient than using the arrows) and type dto delete the selection.
  • 转到第一行并输入d5ddd删除一行,d5d删除5行)~或~
  • 键入Shift-v进入面向行的选择模式,然后向下移动光标使用j(是,使用hjkl以移动向上向右分别,这比使用箭头更有效),然后键入d删除选区。

Also, how can I select the lines using my keyboard as I can in Windows where I press Shiftand move the arrows to select the text? How can I do that in vi?

另外,如何使用键盘选择行,就像在 Windows 中一样,我可以按下Shift并移动箭头来选择文本?我怎么能在vi中做到这一点?

As I said, either use Shift-vto enter linewise selection mode or vto enter characterwise selection mode or Ctrl-vto enter blockwise selection mode. Then move with h, j, kand l.

正如我所说,要么使用Shift-v进入行选择模式,要么使用v字符选择模式,要么使用块Ctrl-v选择模式。然后用hjk和移动l

I suggest spending some time with the Vim Tutor (run vimtutor) to get more familiar with Vim in a very didactic way.

我建议花一些时间与 Vim Tutor (run vimtutor) 一起,以一种非常讲授的方式更熟悉 Vim。

See also

也可以看看

回答by heymatthew

Highlighting with your mouse only highlights characters on the terminal. VI doesn't really get this information, so you have to highlight differently.

用鼠标突出显示仅突出显示终端上的字符。VI 并没有真正获得这些信息,因此您必须以不同的方式突出显示。

Press 'v' to enter a select mode, and use arrow keys to move that around. To delete, press x. To select lines at a time, press shift+v. To select blocks, try ctrl+v. That's good for, say, inserting lots of comment lines in front of your code :).

按“v”进入选择模式,然后使用箭头键来移动它。要删除,请按 x。要一次选择行,请按 shift+v。要选择块,请尝试 ctrl+v。这对于在代码前插入大量注释行很有好处:)。

I'm OK with VI, but it took me a while to improve. My work mates recommended me this cheat sheet. I keep a printout on the wall for those odd moments when I forget something.

我对 VI 没问题,但我花了一段时间来改进。我的同事向我推荐了这份备忘单。当我忘记某事时,我会在墙上保留打印件,以备不时之需。

Happy hacking!

快乐黑客!

回答by Greg Hewgill

When using a terminal like PuTTY, usually mouse clicks and selections are nottransmitted to the remote system. So, vi has no idea that you just selected some text. (There are exceptions to this, but in general mouse actions aren't transmitted.)

当使用像PuTTY这样的终端时,通常鼠标点击和选择不会传输到远程系统。因此,vi 不知道您刚刚选择了一些文本。(对此也有例外,但通常不会传输鼠标操作。)

To delete multiple lines in vi, use something like 5ddto delete 5 lines.

要在 vi 中删除多行,请使用类似5dd删除 5 行的方法。

If you're not using Vim, I would stronglyrecommend doing so. You can use visual selection, where you press Vto start a visual block, move the cursor to the other end, and press dto delete (or any other editing command, such as yto copy).

如果您不使用Vim,我强烈建议您这样做。您可以使用视觉选择,在此处按下V以启动视觉块,将光标移动到另一端,然后按下d以删除(或任何其他编辑命令,例如y复制)。

回答by OscarRyz

Do it the vi way.

按照 vi 的方式进行。

To delete 5 lines press: 5dd( 5 delete )

要删除 5 行,请按:(5dd5 delete )

To select ( actually copy them to the clipboard ) you type: 10yy

要选择(实际上是将它们复制到剪贴板),请键入: 10yy

It is a bit hard to grasp, but very handy to learn when using those remote terminals

有点难掌握,但在使用那些远程终端时学习起来非常方便

Be aware of the learning curves for some editors:

请注意某些编辑器的学习曲线:


(source: calver at unix.rulez.org)


(来源:来自 unix.rulez.org 的 calver

回答by Syed

If you want to remove all lines in a file from your current line number, use dG, it will delete all lines (shift g)mean end of file

如果要从当前行号中dG删除(shift g)文件中的所有行,请使用,它将删除所有行,表示文件结束

回答by Vladimir verleg

If you want to delete using line numbers you can use:

如果要使用行号删除,可以使用:

:startingline, last line d

Example:

例子:

:7,20 d

This example will delete line 7 to 20.

本示例将删除第 7 到 20 行。