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.
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”.
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.
Use Esc to switch to normal mode in Vim. Once you press Enter, the specified lines are removed, as in the image below.
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.
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:
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:
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.
You don't need a global command for this. The substitute command in by itself will suffice by
example
%s/.*text.*\n//gc
You can mix :help global
and :help substitute
:
:g/text/s/.*\n//c
This will ask for confirmation before deleting every line containing text
:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With