February 23, 2011 · General ·

A utility to help rename various files at once, using a text editor. This script will open the list of files in a directory in a text editor (by default, vi). Then any changes made in the editor will be reflected in the filesystem (the files in the directory will be renamed to the names saved in the editor).

Installation

Unix/Linux with Python only: copy to /usr/local/bin or elsewhere on $PATH, rename to vimv and make executable.

Usage

On the command-line:

1. type vimv to open the list of files in the current directory in a text file (the list returned by ls);
2. make the changes, save and quit.

The filenames are updated when you quit. If you delete a name (leave a blank line where a filename used to be), the file will be deleted (this will not work with directories). A log of the changes is written to ~/.vimv_history.

Advanced

Multiple directory listings can be updated in sequence, e.g., like this:

$vimv . ./dir1 ./dir2

Files can be moved between directories, relative names can be given in the file; double quotes should not be used in the file.

For more details execute vimv --help.

Download Vimv from here

Sponsored Link

4 Comments to “Vi move (vimv) – rename the files in a directory using the vi text editor”

  1. Ras says:

    This is a beautiful script that I have been waiting a long time to find. I get an error however. I’m not a python programmer, but I guess I dont have the right version:
    > vimv .
    File “/usr/local/bin/vimv”, line 87
    except OSError as e:
    ^
    SyntaxError: invalid syntax

    This is on a Mac Leopard. Could you possibly post some requirements somewhere?

  2. wataya says:

    Hi,
    a simple but very useful script. Great work! Thanks for sharing. As I prefer the entries to be ordered, I’ve changed line 82 to:
    ls = sorted(os.listdir(dir))

  3. Charles says:

    Ras: The script as written requires Python 2.6.

    I started to port it to 2.5, but finally gave up. (I’m not a python programmer, but I have this friend named Google…)

    For that particular line (and another like it) you can simply replace ” as” with “,”

    However, then you run into the “with” statements, e.g. line 94, where I think you need to replace this:
    with open(temp_file.name, ‘rb’) as f:
    file_contents = f.read().split(‘\n’)[:-1]
    with this:
    f = open(temp_file.name, ‘rb’)
    try:
    file_contents = f.read().split(‘\n’)[:-1]
    finally:
    f.close()

    But THEN I found that NamedTemporaryFile doesn’t support the “delete” parameter, so apparently you need to mess with the whole block of code that uses that object.

    I don’t think it would take *that* much work to finish the job — esp. for someone who actually knows Python — but I can’t spend any more time on it right now. 🙁 I’ll just use the 2.6 script from another machine using NFS.

  4. Ras says:

    I’m not a python programmer. I actually thought I had the latest version, so I guess I can just upgrade.

Leave a Reply

  • Recent comments