From 19969f79632e4936073dcd91fbd9dfbe917e9438 Mon Sep 17 00:00:00 2001 From: 53hornet Date: Sat, 2 Oct 2021 17:22:43 -0400 Subject: published file renaming with vim --- ...-scripting-and-bulk-renaming-files-with-vim.php | 6 ---- drafts/2021-reading-list.php | 1 + ...-scripting-and-bulk-renaming-files-with-vim.php | 41 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) delete mode 100644 drafts/2021-09-26-useful-shell-scripting-and-bulk-renaming-files-with-vim.php create mode 100644 posts/2021-09-26-useful-shell-scripting-and-bulk-renaming-files-with-vim.php diff --git a/drafts/2021-09-26-useful-shell-scripting-and-bulk-renaming-files-with-vim.php b/drafts/2021-09-26-useful-shell-scripting-and-bulk-renaming-files-with-vim.php deleted file mode 100644 index 6f0adfe..0000000 --- a/drafts/2021-09-26-useful-shell-scripting-and-bulk-renaming-files-with-vim.php +++ /dev/null @@ -1,6 +0,0 @@ -Fitzgerald, F. Scott. The Great Gatsby
  • Wells, H.G. War of the Worlds
  • Tolkien , J. R. R. The Fellowship of the Ring
  • +
  • Tolkien , J. R. R. The Two Towers
  • diff --git a/posts/2021-09-26-useful-shell-scripting-and-bulk-renaming-files-with-vim.php b/posts/2021-09-26-useful-shell-scripting-and-bulk-renaming-files-with-vim.php new file mode 100644 index 0000000..bc88caa --- /dev/null +++ b/posts/2021-09-26-useful-shell-scripting-and-bulk-renaming-files-with-vim.php @@ -0,0 +1,41 @@ + + +

    + I used to use vifm, a terminal file manager, on FreeBSD and Linux. I realized the only feature I was really using was bulk interactive file renaming with vim. So I ditched the file manager altogether and implemented the bulk rename feature in a few lines of shell. +

    + +

    + Lots of terminal file managers have this feature (lf, vifm, ranger), and I tried a bunch but always ended up going back to just cd-ing around in the terminal and opening files most of the time. The only feature that did keep me using file managers/browsers/explorers was the bulk rename. For example, in vifm, you can select a bunch of files and open all of their names in a Vim buffer, one per line. You make a bunch of bulk changes or one-offs, and then save and quit. All of the files get renamed to whatever their line changed to. Since this was the only feature I was using I decided to implement it myself with just a little useful shell scripting. +

    + +

    +

    +
    +rename() {
    +	src="$(mktemp -t rename)" # Make a temp file for source filenames
    +	dst="$(mktemp -t rename)" # Make a temp file for destination filenames
    +	printf '%s\n' "$@" | tee "$src" > "$dst" # Write args (filenames) into temp files
    +	$EDITOR "$dst" # Edit the destination filenames
    +	diff -s "$src" "$dst" > /dev/null || paste "$src" "$dst" | sed -e "s/\t/' '/" -e "s/^/mv -i '/" -e "s/$/'/" | sh 
    +		# Change the source filenames into destination filenames by rewriting as mv commands
    +}
    +
    +
    + +

    + Here's the function! That's all there is to it. It reads in a list of filenames from the args and writes them all to two temporary files. It then opens an editor and lets you interactively rename files (by hand, with regexes, macros, etc.). Then you save and quit your editor and (as long as the old and new names are different), generates mv commands to rename everything. It pipes those commands to a shell. +

    + +

    + I could have alternatively read files from STDIN but I chose to use args so that I could more easily make use of shell globbing and wildcards. It makes more sense to make my rename tool take arguments the way that cp and mv do. +

    + +

    + I've been using this renaming extensively, especially for tagging TV show DVD rips. It's so handy I just put it in my shell's startup aliases. I also uninstalled vifm, the file manager I had been using for just this purpose. +

    -- cgit v1.2.3