| 1 | #!/usr/bin/env bash |
| 2 | set -eu |
| 3 | |
| 4 | # Lists the current directory's files in Vim, so you can edit it and save to rename them |
| 5 | # USAGE: vimv [file1 file2] |
| 6 | # https://github.com/thameera/vimv |
| 7 | |
| 8 | declare -r FILENAMES_FILE=$(mktemp "${TMPDIR:-/tmp}/vimv.XXX") |
| 9 | |
| 10 | trap '{ rm -f "${FILENAMES_FILE}" ; }' EXIT |
| 11 | |
| 12 | if [ $# -ne 0 ]; then |
| 13 | src=( "$@" ) |
| 14 | else |
| 15 | # in this line where is says src=($(ls)), you can change the "ls" to "exa" |
| 16 | # if you want the proper number sorting. |
| 17 | IFS=$'\r\n' GLOBIGNORE='*' command eval 'src=($(ls))' |
| 18 | fi |
| 19 | |
| 20 | for ((i=0;i<${#src[@]};++i)); do |
| 21 | echo "${src[i]}" >> "${FILENAMES_FILE}" |
| 22 | done |
| 23 | |
| 24 | ${EDITOR:-vi} "${FILENAMES_FILE}" |
| 25 | |
| 26 | IFS=$'\r\n' GLOBIGNORE='*' command eval 'dest=($(cat "${FILENAMES_FILE}"))' |
| 27 | |
| 28 | if (( ${#src[@]} != ${#dest[@]} )); then |
| 29 | echo "WARN: Number of files changed. Did you delete a line by accident? Aborting.." >&2 |
| 30 | exit 1 |
| 31 | fi |
| 32 | |
| 33 | declare -i count=0 |
| 34 | for ((i=0;i<${#src[@]};++i)); do |
| 35 | if [ "${src[i]}" != "${dest[i]}" ]; then |
| 36 | mkdir -p "$(dirname "${dest[i]}")" |
| 37 | if git ls-files --error-unmatch "${src[i]}" > /dev/null 2>&1; then |
| 38 | git mv "${src[i]}" "${dest[i]}" |
| 39 | else |
| 40 | mv "${src[i]}" "${dest[i]}" |
| 41 | fi |
| 42 | ((++count)) |
| 43 | fi |
| 44 | done |
| 45 | |
| 46 | echo "$count" files renamed. |