135

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?

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

6 Answers6

234

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?

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 1
    Actually, vim has great mouse support if your terminal supports it. For example using the `gpm` daemon in a console, or running `xterm` or `konsole` in a graphical environment, you can tell vim `set mouse=a` (or add it to your .vimrc) and the mouse can be used for selection, resizing splits, etc. – Ben Voigt Jun 25 '10 at 01:34
  • @Ben You're right, I don't use the mouse at all but this doesn't mean VIM can't deal with it (in certain circumstances). I was actually removing this inaccurate part from my answer while you were typing. – Pascal Thivent Jun 25 '10 at 01:36
  • Also, it's really hard to tell when your V is capital or not... I'd suggest saying [Shift] + [V] for line-mode selection. – Ben Voigt Jun 25 '10 at 01:40
  • 1
    I know it's been a while, but why using `h`, `j`, `k` and `l` would be **much** more efficient than using the arrows? I see no problem by using the arrows and the final result looks the same. Also, the arrows were first designed for that purpose, so their arrangement feels much more intuitive (at least for me) – Frederico Pantuzza Aug 04 '17 at 08:54
  • 1
    @FredericoPantuzza It's a matter of arm movement at that point - the goal is to keep your fingers on the home row keys at all times. Having to pick up your right arm and slide it over to the arrow keys to use them adds up over time. – AndrewF Sep 06 '17 at 15:27
  • TLDR; type d to delete section – Suhayb Apr 22 '19 at 13:36
  • On the arrow key vs h,j,k,l, occasionally you may be on a bad network and trying to edit something remotely (i.e., something messed up the network and you are the hero to fix it remotely), arrow keys sends a sequence of keys and if the network packet was broken into two, then you will see weird characters. In old dial-up network time this was a major concern. – fchen Jan 21 '22 at 19:24
62

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)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
36

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mister Verleg
  • 4,053
  • 5
  • 43
  • 68
24

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!

heymatthew
  • 6,716
  • 5
  • 26
  • 22
  • This is the way. The accepted answers are nonsense. How is the users supposed to count lines if they may not even see them all on one screen?.. – wvxvw Feb 25 '22 at 12:37
  • If you're using Vim then one way to do what you're after is to set relative number in your `.vimrc`. This makes it pretty quick to count lines that you want deleted as you see a the count away from your cursor in the margin. See this SO answer for more details https://stackoverflow.com/questions/27151491 It's been 12 years and the way I use my editor has completely changed, but I'm happy that you found this answer useful – heymatthew Feb 26 '22 at 23:06
  • Yes, I know about relative line numbers, but it doesn't help if all the lines you want to select aren't visible at once. – wvxvw Mar 03 '22 at 22:19
6

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
3

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

shas
  • 703
  • 2
  • 8
  • 31
Syed
  • 31
  • 1