I've read this question, but I want to know how can I delete the line when the pattern DOESN'T occur.
To delete a line in Vi or Vim, switch to normal mode first. If you're into command mode or insert mode, you can switch back to normal mode by pressing Escape. Highlight the line that you want to delete, then hit dd or D on the keyboard.
In normal mode, press / to start a search, then type the pattern ( \<i\> ), then press Enter. If you have an example of the word you want to find on screen, you do not need to enter a search pattern. Simply move the cursor anywhere within the word, then press * to search for the next occurrence of that whole word.
Deleting the rest of the line when a pattern does not occur is ... hard for me to understand.
Did you mean, delete the whole line if it doesn't have a pattern?
:v/pattern/d
If you meant, preserve the line but clobber the characters:
:v/pattern/s/.*//
Equivalently:
:g!/pattern/d
Easier to remember in my opinion, because!
is ingrained as "not" in my brain.
:v/pattern/s/.*//
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