can someone please edit %:s/\([0-9]*\)_\(*\)/\2
so that i can rename files. for example, if file name is 5555_word_word.jpg
, then I want the file name to be word_word.jpg
. i feel like I am so close!
views:
175answers:
3Try this:
:%s/\([0-9]*\)_\(.*\)/\2
The .
will match any character (part of the second grouping) and the *
will greedily match any amount of them. Your original regex was missing that directive. This will also rename files of the form _word_word.txt
to word_word.txt
. If you want to require digits to match (probably a good idea), use:
:%s/\([0-9]\+\)_\(.*\)/\2
The \+
directive means to match 1 or more instances.
You may want to simplify and have it just delete leading numbers and the underscore:
s/^[0-9]+_//
Your version is fine but you forgot a period and you should probably anchor it to the beginning of a line or to a word boundary using either ^
or \<
.
:%s/^\([0-9]*\)_\(.*\)/\2/
You can use \v
to clean up some of those slashes.
:%s/\v^([0-9]*)_(.*)/\2/
You can use \ze
to avoid capture groups.
:%s/^[0-9]*_\ze.*//
But the trailing .*
is superfluous, because it matches anything. So use Seth's version, it's the simplest.