Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to delete line containing certain text in vim with prompt

Tags:

At present i can search for text

/text 

and then delete line using dd and if i don't want to delete i can go for next match with n.

But is there any more fast way to do that!

This command below deletes all the lines containing text, but the problem is that it deletes all lines at once, sometimes that text is in some line that is exception.

:g/text/d 

But i want something simple like like

:%s/text/some_other_text/gc 

because this gives the option to substitute or not to.

like image 277
Amritpal Nagra Avatar asked Oct 17 '17 03:10

Amritpal Nagra


People also ask

How do I delete a specific line in Vim?

Deleting a single line in Vim editor: First, bring your cursor to the line you want to delete. Press the “Esc” key to change the mode. Now type, “:d”, and press “Enter” to delete the line or quickly press “dd”.

How do I delete a chunk of text in Vim?

Step 1: Move your cursor to the top line of the block of text/code to remove. Step 2: Press V (Shift + v) to enter the Visual mode in Vim/Vi. Step 3: Now move your cursor down to the bottom of the block of text/code to remove. Step 4: Press d to delete the block selected.

How do you select multiple lines to delete in Vim?

Use Esc to switch to normal mode in Vim. Once you press Enter, the specified lines are removed, as in the image below.

How do I delete a specific line in Vim?

Delete Lines Matching Specific Pattern in a File Using VIM. In order to delete lines matching specific pattern in a file using vim editor, you can use ex command, g in combination with d command. To remove lines that contains the string amos, in vim command mode, type the command below and press Enter. :g/amos/d.

How to change the default range of lines in Vim?

1. Use <strong>Esc</strong> to switch to normal mode in Vim. 2. Use the following command and replace <strong> [from]</strong> with the number of the line where you want the range to start and <strong> [to]</strong> where you want it to end: For instance, if you wanted to remove lines 4, 5, 6, and 7, you would use the command:

How do I remove the pattern George Bush in Vim?

That’s all you need to do. This vim search/delete command will find and then delete all lines containing the pattern “George Bush”. Here’s a brief explanation of how this vi/vim search and delete command works:

How to delete lines that contain a specific string in Linux?

So far, we’ve seen how to delete lines that contain a specific string with the grep and awk commands. In addition, we can also solve the problem using the sed command. The sed command has the -i option, which allows editing files in-place.


2 Answers

You don't need a global command for this. The substitute command in by itself will suffice by

  • adding a wildcard
  • and adding an end-of-line.

example

%s/.*text.*\n//gc 
like image 84
Lieven Keersmaekers Avatar answered Sep 18 '22 07:09

Lieven Keersmaekers


You can mix :help global and :help substitute:

:g/text/s/.*\n//c 

This will ask for confirmation before deleting every line containing text:

enter image description here

like image 28
romainl Avatar answered Sep 18 '22 07:09

romainl