Vim Tips Wiki

Remove up to the nth slash to clean file lists

1,610pages on
this wiki

Redirected from VimTip1206

Tip 1206 Printable Monobook Previous Next

created 2006 · complexity intermediate · author Ken Holm · version 5.7

Lets say you find a list of files:

:r !find / -name blahblah -ls

and get the following:

3139934 4 -rw-r--r-- 1 dude dude 600 Feb 3 13:25 /var/www/
3010144 4 -rw-r--r-- 1 mang mang 1848 Mar 31 09:35 /var/www/
1964295 4 -rw-r--r-- 1 apache apache 597 Mar 8 11:48 /var/www/
2339286 4 -rw-r--r-- 1 mang mang 1851 Mar 31 09:29 /var/www/
3010157 4 -rw-r--r-- 1 mang mang 1940 Mar 31 10:44 /var/www/
294691 4 -rw-r--r-- 1 mang mang 1848 Apr 10 16:02 /tmp/PMT/
1880561 4 -rw-r--r-- 1 mang mang 2432 Feb 22 13:49 /usr/local/scripts/prod/
1880565 4 -rw-r--r-- 1 mang mang 1700 Mar 30 14:44 /usr/local/scripts/dev/
899894 4 -rw-r--r-- 1 mang mang 1848 Mar 31 09:48 /usr/local/scripts/dev/Gold/base/
1880626 4 -rw-r--r-- 1 mang mang 1699 Mar 30 14:46 /usr/local/scripts/test/

Now, you'd like to just have a list of files without the rest of the info.

Issue the global replace as follows:


That leaves you with a nice list:


If you want two levels deep you can wrap the atom being grabbed in escaped parens and quantify it with braces:


Note: If you want two levels deep, you need to grab three atom's worth of stuff, all the junk to the left of the first slash, then the first two levels of dirs.


Why not use:

:r !find /path/to/search -name pattern_to_find

and skip the '-ls' parameter completely? That will do away with the extraneous listing fields in the first place. Or am I missing something really subtle?

The really subtle part was how to delete all the stuff before the first slash.

One could ask "Why not use :r !locate filename?"

That would have given us the data we needed with no manipulation necessary. However that is beyond the scope of this tip.

Why not the shorter:


Advertisement | Your ad here

Around Wikia's network

Random Wiki