简体   繁体   中英

How to delete selected text in VI editor

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

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? How can I do that in 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

Forget the mouse. To remove 5 lines, either:

  • Go to the first line and type d5d ( dd deletes one line, d5d deletes 5 lines) ~or~
  • 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.

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

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. Then move with h , j , k and l .

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

See also

Do it the vi way.

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

To select ( actually copy them to the clipboard ) you type: 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 )

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

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

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

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.

Happy hacking!

When using a terminal like Putty, usually mouse clicks and selections are not transmitted 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.)

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

If you're not using Vim , I would strongly recommend doing so. 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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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