How to delete selected text in the vi editor
Solution 1
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
- This answer to What is your most productive shortcut with Vim? (one of my favorite answers on SO).
- Efficient Editing With vim
Solution 2
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)
Solution 3
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.
Solution 4
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!
Solution 5
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
Experienced application developer. Software Engineer. M.Sc.E.E. C++ (10 years), software engineering, .NET/C#/VB.NET (12 years), usability testing, Perl, scientific computing, Python, Windows/Macintosh/Linux, Z80 assembly, CAN bus/CANopen. Contact I can be contacted through this reCAPTCHA (requires JavaScript to be allowed from google.com and possibly other(s)). Make sure to make the subject specific (I said: specific. Repeat: specific subject required). I can not stress this enough - 90% of you can not compose a specific subject, but instead use some generic subject. Use a specific subject, damn it! You still don't get it. It can't be that difficult to provide a specific subject to an email instead of a generic one. For example, including meta content like "quick question" is unhelpful. Concentrate on the actual subject. Did I say specific? I think I did. Let me repeat it just in case: use a specific subject in your email (otherwise it will no be opened at all). Selected questions, etc.: End-of-line identifier in VB.NET? How can I determine if a .NET assembly was built for x86 or x64? C++ reference - sample memmove The difference between + and & for joining strings in VB.NET Some of my other accounts: Careers. [/]. Super User (SU). [/]. Other My 15 minutes of fame on Super User My 15 minutes of fame in Denmark Blog. Sample: Jump the shark. LinkedIn @PeterMortensen (Twitter) Quora GitHub Full jump page (Last updated 2021-11-25)
Updated on September 03, 2022Comments
-
Peter Mortensen over 1 year
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?
-
Admin almost 14 yearsi have the printed man list but could not find how to delete the selected text
-
Mawg says reinstate Monica almost 14 yearsx Delete character to the right of cursor X Delete character to the left of cursor D Delete to the end of the line dd Delete current line :d Delete current line from lagmonster.org/docs/vi.html hope that helps a little
-
Devanshu Mevada almost 14 yearsVIM questions on superuser? LOL!
-
Marcel Korpel almost 14 years@Pascal: hmm, yeah, you're right. Nicely formatted answer, by the way. Oh, and his/her question was not about VIM, but about VI. ;)
-
-
Ben Voigt almost 14 yearsActually, vim has great mouse support if your terminal supports it. For example using the
gpm
daemon in a console, or runningxterm
orkonsole
in a graphical environment, you can tell vimset mouse=a
(or add it to your .vimrc) and the mouse can be used for selection, resizing splits, etc. -
Devanshu Mevada almost 14 years@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.
-
Ben Voigt almost 14 yearsAlso, it's really hard to tell when your V is capital or not... I'd suggest saying [Shift] + [V] for line-mode selection.
-
Greg Hewgill almost 14 years@Justin L.: Use
<kbd>a</kbd>
. -
Justin L. almost 14 yearsThanks! :D Are there any more of these hidden around somewhere?
-
congusbongus over 10 yearsPutty is capable of transmitting mouse commands; the first sentence may be outdated.
-
Frederico Pantuzza almost 7 yearsI know it's been a while, but why using
h
,j
,k
andl
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) -
AndrewF over 6 years@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.
-
Suhayb about 5 yearsTLDR; type d to delete section
-
Tung almost 4 yearsI haven't been aware of this tip so far. It works like a charm! Thanks!
-
fchen over 2 yearsOn 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.
-
wvxvw about 2 yearsThis 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?..
-
Hounaida about 2 yearsIf 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 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 -
wvxvw about 2 yearsYes, I know about relative line numbers, but it doesn't help if all the lines you want to select aren't visible at once.