Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Can you delete a specific line number from another line?

Tags:

vim

I was on line 93 and realized I wanted to delete line 89. I typed :d89 in hopes that line 89 wold be deleted. It didn't work.

Does anyone know a good way to accomplish this type of interaction? I am a comfortable Vim user but have not (yet) taken the leap to writing plugins...

Thanks.

like image 571
Ethan Avatar asked Dec 13 '11 19:12

Ethan


People also ask

How do I delete a specific number of lines in vi?

Press the Esc key to go to normal mode. Place the cursor on the first line you want to delete. Type 5dd and hit Enter to delete the next five lines.

How do I delete a specific line number in Linux?

To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete.

How do I delete a specific part of Vim?

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.

How do you delete a number in Vim?

Make the vi/vim text editor show or hide line numbers Press ESC key. At the : prompt type the following command to run on line numbers: set number. To turn off line numbering, type the following command at the : prompt set nonumber.


2 Answers

The address of a colon-command (eg: the line number) comes first.

:89d 

Note that this will also cause you to navigate to the location of the change. You can use `` to jump back.

If you'd prefer to have this be a single command you can define a custom command. eg:

command! -range -nargs=0 Delete <line1>,<line2>d|norm `` 

This defines a command called Delete that deletes the addressed range (<line1>,<line2>d) and then navigates back (norm ``).

You can call it like:

:89Delete 

You can actually invoke it with any unique prefix, so you may be able to get it down to:

:89D 
like image 116
Laurence Gonsalves Avatar answered Sep 27 '22 03:09

Laurence Gonsalves


:89d will delete the 89th line.

:89,91d will delete the 89th 90th and 91st line.

These functions also work in the Vrapper plugin for Eclipse, and the Vim Extension for Visual Studio Code.

like image 37
ZhaoGang Avatar answered Sep 26 '22 03:09

ZhaoGang