Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to remove all the words that ending with abc

Example:

111abc 222def 333abc 444def 555abc 666def

From here I want to delete all the words that ending with abc.

so I tried this

:s/abc//g

But this will replace only abc

How to delete that all words ending with abc?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

If you want to remove the whole word, and if your sample is complete, then you want to use this regexp:

:s/[0-9]\{3\}abc//g

It removes every bit consisting of 3 digits followed by abc.

share|improve this answer
    
It's showing E486: Pattern not found: [0-9]{3}abc –  max Sep 15 '12 at 10:34
    
My bad, I forgot to escape the curly braces. Edited. –  m4573r Sep 15 '12 at 10:41
    
Thanks a lot it's working... –  max Sep 15 '12 at 10:48

If all the words you want to delete consist of three digits followed by the character sequence "abc" you can go with @m4673r's suggestion. For a more general approach I'd suggest this instead:

:s/\<\S*abc\>//g

This will remove any word (regardless of characters/length) that ends with the character sequence "abc" (111abc as well as FOOBARabc or just abc).

share|improve this answer

Ansgar's answer is plain right.
I'll explain it's parts to make it a methodology more than a recipe because I think that regular expressions are somehow cryptic.

:s/\<\S*abc\>//g

is a vim substitution command.
It means "in the current line (use %s for all lines) substitute the matches of the regular expression \<\S*abc\> (delimited by the first two "/"s by the text contained between the last two "/"s, i.e., nothing. The ending "g" is for global and means to replace all the matches instead of only the first one.

The regexp is enclosed in escaped angle brackets \< and \>. These are word boundaries, meaning that the regexp will match text only within single words. Think that vim will split the text in words before attempting to match each one.
The escaped uppercase S is for non-witespace. \s (lowercase) matches whitespace (spaces and tabs) while \S (uppercase) matches all other.
Hence, \S* means "non-whitespace characters zero or more times".
The abc part simply matches that exact characters sequence.

Putting it all together, the regexp matches whole words made up of any sequence of characters followed by abc, as max wanted.

Because of the way vim thinks of words, the any sequence of characters will not contain spaces and other characters that are word boundaries. The other characters list depends on the file type being edited and can be seen typing :set iskeyword enter. This is the information used to split the text in words.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.