ouranos
ouranos

Reputation: 331

Find and replace with asterisk (*) in vim

I have a text file, which lists file names in a directory (excerpt below). The item names are the f followed 3-digit numbers.

 771M Jan 22 02:35 f186
 1.2G Jan 22 02:35 f172
 771M Jan 22 02:36 f206
 771M Jan 22 02:37 f151
 771M Jan 22 02:37 f029
 1.2G Jan 22 02:38 f162
 771M Jan 22 02:40 f168
 1.2G Jan 22 02:42 f244
...

I would like to have a list of only the 3-digit numbers. Therefore, I need to repalce the previous columns by "nothing". Since the content of the previous columns is different for each line, I would use an asterisk, and the following approach seemed logic for me in VIM:

:%s/*f/

where I replace everything followed by an f by nothing.

Why doesn't this work? How do I do this in VIM?

Upvotes: 3

Views: 3228

Answers (1)

Sabrina Jewson
Sabrina Jewson

Reputation: 1518

Vim uses regex and, in regex, an asterisk is actually a quantifier.

What you want is this:

:%s/.*f/

the . character means any character, and the * means any number of .s. So, the combination .* matches essentially anything, which is what you were looking for.

Regexes are simultaneously the most useful and annoying things I have ever learned, so I would recommend getting familiar with them.

Upvotes: 8

Related Questions