简体   繁体   English

如何在VI编辑器中删除所选文本

[英]How to delete selected text in VI editor

I am using putty and vi editor. 我正在使用putty和vi编辑器。 If I select 5 lines using my mouse and I want to delete those lines, how can I do that? 如果我使用鼠标选择5行并且我想删除这些行,我该怎么做?

Also, how can I select the lines using my keyboard as I can in Windows where I press shift and move the arrows to select the text? 另外,我如何使用我的键盘选择线条,因为我可以在Windows中按shift并移动箭头以选择文本? How can I do that in vi? 我怎么能在vi中做到这一点?

I am using putty and vi editor if select 5 lines using mouse and i want to delete those lines how can i do that 我使用putty和vi编辑器,如果使用鼠标选择5行,我想删除这些行,我该怎么做

Forget the mouse. 忘记鼠标。 To remove 5 lines, either: 删除5行,要么:

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

Also how can i select the lines from keyboard like in windows i pres shift and move the arrows to select the text. 另外我如何从键盘中选择线条,如在窗口中移动并移动箭头以选择文本。 how can i do that in vi 我怎么能在vi中这样做

As I said, either use Shift-v to enter linewise selection mode or v to enter characterwise selection mode or Ctrl-v to enter blockwise selection mode. 正如我所说,使用Shift-v进入行选择模式或使用v进入字符选择模式或使用Ctrl-v进入按块选择模式。 Then move with h , j , k and l . 然后用hjkl移动

I suggest spending some time with the VIM Tutor (run vimtutor ) to get more familiar with VIM in a very didactic way. 我建议花一些时间与VIM Tutor(运行vimtutor )以非常教学的方式更熟悉VIM。

See also 也可以看看

Do it the vi way. 这是vi方式。

To delete 5 lines press: 5dd ( 5 delete ) 要删除5行,请按: 5dd (5删除)

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

If you want to delete using line numbers u can use 如果您想使用行号删除,您可以使用

:startingline, last line d

example: 例:

:7,20 d

This example will delete line 7 to 20. . 此示例将删除第7行到第20行。

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. 按'v'进入选择模式,然后使用箭头键移动它。 To delete, press x. 要删除,请按x。 To select lines at a time, press shift+v. 要一次选择行,请按shift + v。 To select blocks, try ctrl+v. 要选择块,请尝试ctrl + v。 That's good for, say, inserting lots of comment lines in front of your code :). 例如,在代码前面插入大量注释行是很好的。

I'm OK with VI, but it took me a while to improve. 我很喜欢VI,但我花了一些时间来改进。 My work mates recommended me this cheat sheet . 我的工作伙伴向我推荐了这张备忘单 I keep a printout on the wall for those odd moments when I forget something. 当我忘记某些事情时,我会在墙上打印出那些奇怪的时刻。

Happy hacking! 快乐的黑客!

When using a terminal like Putty, usually mouse clicks and selections are not transmitted to the remote system. 当使用像Putty这样的终端时,通常鼠标点击和选择不会传输到远程系统。 So, vi has no idea that you just selected some text. 所以,vi不知道你刚刚选择了一些文字。 (There are exceptions to this, but in general mouse actions aren't transmitted.) (对此有例外,但通常不会传输鼠标操作。)

To delete multiple lines in vi, use something like 5dd to delete 5 lines. 要删除vi中的多行,请使用5dd类的5dd删除5行。

If you're not using Vim , I would strongly recommend doing so. 如果您不使用Vim ,我强烈建议您这样做。 You can use visual selection, where you press V to start a visual block, move the cursor to the other end, and press d to delete (or any other editing command, such as y to copy). 您可以使用视觉选择,按V键启动可视块,将光标移动到另一端,然后按d删除(或任何其他编辑命令,例如y复制)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM