(vim) How to remove word that has a specified character?

so I tried to remove any word that has a letter "a" in it with vim

I want to change this string

hotel echo lima lima oscar whiskey oscar romeo lima delta

to

hotel echo whiskey romeo

I hope I can do this on the vim, but no problem if I have to do this on paste, sed, etc. As long as I can do this on linux.

Thanks

2 Answers

Use the following:

s/\<\w*a\w*\> *//g

Explanation:

s/ # substitute, delimiter \< # word boundary \w* # 0 or more word character a # letter a \w* # 0 or more word character \> # word boundary * # 0 or more spaces
/ # delimiter
/g # delimiter, global
2

One way is to put the cursor at the start of the line, search for /a, thendaw ('delete a word'). You can then jump to the next match with n and press the . to repeat the deletion (and just keep spamming n + .). If the line is very long, you can record a macro with (put the cursor at the start of the line) with qqndaw, and just repeat the macro 100 times (or some sufficiently large number) with 100@q.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like