It is possible to use netrw (the default explorer when you open directories for editing) to execute bulk rename operations.
You can do that by first marking the files, and then using a s///
substitution when prompted for a replacement in the rename operation.
Beware: This gets really heavy in its use of regexps. It's also quite strict in how you get all the details correctly. You also don't have "undo" available, if you make any mistakes. And if you happen to get some details wrong and there are name clashes, you may end up overwriting existing files, with possibly no good way to recover them other than restoring from a backup.
In your particular case, replacing _
with -
at the start of the filenames, you can do that with this sequence of operations:
- Use the
mr
command to mark a series of files, selecting them based on a pattern.
- You'll be prompted with
Enter regexp:
but note this command actually wants a glob instead! So enter _*
to mark all files with a name starting with underscore. (You would use something like *.txt
to mark all files with a "txt" extension.)
- Use the
R
command to rename files. netrw will prompt you about the first file, so it will ask you about renaming _file1
to what? But you can actually use a s///
expression here, which will then apply to all marked files.
- Press Control-U to delete the current replacement (or backspace as many times as needed), and enter
s/_\(.*\)/-\1/
in its place. Once you press ENTER, all your files will be renamed as you expect.
Note that:
- You need to have the final slash in the
s///
expression for this to work (that's usually not the case with the :s
command, but it's the case here.)
- You need the pattern to match the whole filename. You can't just use
s/_/-/
here unfortunately.
- Which means you typically need at least one capture group, such as
\(.*\)
, which you then refer to using \1
on the replacement side.
The last couple of constraints mean it's really hard to use this feature to, for example, replace all underscores with hyphens...
But it can be used to change a prefix in files, or to change file extension. Even in those cases, having to deal with capture groups makes this feature pretty clunky, so you're probably better off using a different tool for your bulk rename operations.
See also: