Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and replace with asterisk (*) in vim

Tags:

replace

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?

like image 653
ouranos Avatar asked May 18 '26 05:05

ouranos


1 Answers

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.

like image 185
Sabrina Jewson Avatar answered May 21 '26 22:05

Sabrina Jewson