#!/bin/zsh # vren - rename files with vim # usage: vren [dir] dir="${1:-.}" cd "$dir" || exit 1 # git init if no .git has_git=0 if [ -d .git ]; then has_git=1 else git init -q git add -A git commit -q -m "before rename" fi # list files old=$(mktemp) new=$(mktemp) ls -1 > "$old" cp "$old" "$new" # edit vim "$new" # check old_count=$(wc -l < "$old") new_count=$(wc -l < "$new") if [ "$old_count" -ne "$new_count" ]; then echo "error: line count mismatch ($old_count -> $new_count)" rm "$old" "$new" exit 1 fi # diff changes=0 paste "$old" "$new" | while IFS=$'\t' read -r o n; do [ "$o" != "$n" ] && echo " $o -> $n" && changes=$((changes+1)) done if ! diff -q "$old" "$new" >/dev/null 2>&1; then echo "" printf "apply? (y/n): " read -q ans echo "" if [ "$ans" = "y" ]; then paste "$old" "$new" | while IFS=$'\t' read -r o n; do [ "$o" != "$n" ] && mv -- "$o" "$n" done echo "done" # restore point if [ "$has_git" -eq 0 ]; then git add -A git commit -q -m "after rename" echo "restore: git log / git checkout HEAD~1 -- ." fi else echo "cancelled" fi else echo "no changes" fi rm "$old" "$new"