1

I am looking for a unix command-line program for renaming file (or files) visually, in an editor or inline (in the same manner as you rename a file on your desktop).

Example. I often need to rename a file somewhere deep. Imagine a file with a wrong .js.txt extension (_ is a cursor):

$ mv deep/inside/there/is/a/file.js.txt _

It is confusing to repeat the whole path as second argument to mv:

$ mv deep/inside/there/is/a/file.js.txt deep/inside/there/is/a/file.js

(I know I can use the mouse, but it is still error-prone). I'd better press Home and change mv to a visual renamer (imagine it's called vmv). E.g.:

$ vmv somewhere/deep/inside/there/is/a/file.js.txt
EDIT FILE NAME: somewhere/deep/inside/there/is/a/file.js.txt_
user31494
  • 3,853
  • 3
  • 16
  • 6
  • I don't know what remane files in an editor means.. do you mean while open? I don't know if it's possible to rename a file while open in an editor. If you mean writing the copy commands in an editor, how about writing a script, you write your mv lines in there , copy/paste for any repetition of a long directory, and execute it? – barlop Jul 14 '11 at 18:54
  • 3
    `mv deep/inside/there/is/a/file.js{.txt,}` – Ignacio Vazquez-Abrams Jul 14 '11 at 18:58
  • possible duplicate of [Reuse text on a bash command](http://superuser.com/questions/294685/reuse-text-on-a-bash-command) – Nifle Jul 14 '11 at 19:02
  • 1
    Also [in-bash-how-can-i-rename-a-file-without-repeating-the-path](http://superuser.com/questions/298081/in-bash-how-can-i-rename-a-file-without-repeating-the-path) – Nifle Jul 14 '11 at 19:02
  • @Nifle, thanks for pointers! I wasn't looking for bash-specific solution. I use [fish](http://en.wikipedia.org/wiki/Friendly_interactive_shell) as my shell. That's why I didn't mention bash in the question. – user31494 Jul 15 '11 at 10:38
  • @barlop, I was thinking about `vidir` command, but didn't know its name ;-) Thanks @grawity for a great answer below! – user31494 Jul 15 '11 at 10:39
  • @Ignacio, thank you! Your solution is bash-specific, but luckily [fish](http://en.wikipedia.org/wiki/Friendly_interactive_shell) (shell of my choice) supports brace expansions too. – user31494 Jul 15 '11 at 10:43

1 Answers1

3

Write your own. Put this in a file called vmv, make it executable, put it in ~/bin or wherever you'd like:

#!/bin/bash
for oldname; do
    read -rep "Edit: " -i "$oldname" newname
    if [[ $oldname != $newname ]]; then
        mv -v "$oldname" "$newname" || exit $?
    fi
fi

Does exactly what you want.


In bash, you can edit the current command line in an editor:

mv deep/inside/there/is/a/file.js.txt Ctrl-XCtrl-E

moreutils has:

vidir

Debian comes with a Perl script that accepts regexps:

prename 's/\.txt$//' file.js.txt
u1686_grawity
  • 426,297
  • 64
  • 894
  • 966
  • +1 for two extra solutions I never even knew about (bash and vidir) –  Jul 14 '11 at 19:45