On Mon, Sep 02, 2002 at 03:02:03PM +0200, Dirk Zimmermann wrote:
> substitude all occurencies of track7 (i.e. track7, track7.log and
> track7.dat) with track49050 and if the file name is "track49061.job" I
> want to substituede with track49061. I would prefer to do this
expand('%:r') will give you the 'root' filename of the current file.
That is the filename without the extension. So:
x.txt => x
That gives you what you want to replace your matches with. I.e. track49061
if the filename is "track49061.job".
You probably know how to use substitutes but with a normal substitute, you
can't insert variables or use code. But you can use the notation \= in the
replace part to indicate you want to use a special replacement:
> So, the question is how to get a part of the current file/buffer name
> and use this in an substitution command?
To bring it all together in one command for you:
:%s/track7/\= expand('%:r') /g
Obviously you might be able to make this more generic with
In which case you might be able to fix all of your files at once with:
vim -c":bufdo %s/track\d\+/\= expand('%:r')/g|wn" *
Which breaks down into:
%s/track\d\+/\= expand('%:r') /g
- Search for all track[0-9] and replace with the filename, as above
- Save and move onto the 'next' file in the argument list
- Perform the following commands on all files in the buffer list
- Execute the following command once vim has loaded.
Hope that helps - oh and if you're going to try the bufdo, *test* first :)
There's nothing worse than finding out you typoed and screwed up hundreds
of live files. (Not that I'd admit to having ever done that :)