Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM : Deleting all characters before particular word for all the lines in a file

Tags:

I need a vim command for deleting all characters before a particular word for all the lines in a file

Ex: Input:

asdfasdfasdfscccHello

qwerqwerHello

24351243vsfgertHello

Output:

Hello

Hello

Hello
like image 523
Anup Buchke Avatar asked Jun 16 '13 19:06

Anup Buchke


People also ask

How do you select all and delete all Vim?

Immediately after opening a file, type “gg” to move the cursor to the first line of the file, assuming it is not already there. Then type dG to delete all the lines or text in it.

How do I delete a previous character in Vim?

Shift + x is instead the appropriate command because was meant for that: Shift + x delete the previous character like Shift + p paste in the previous column.


1 Answers

If you want to delete all characters before "Hello", you can do

:%s/.*Hello/Hello/

Note that .* is greedy, i.e. it will eat all occurrences of "Hello" till it finds the last one. If you have a line:

abcHellodefHelloghi

it will become

Helloghi

If you want a non-greedy solution, try

:%s/.\{-}Hello/Hello
like image 113
pfnuesel Avatar answered Oct 15 '22 20:10

pfnuesel