Krolyxon
Commit 01eec64

Branches

Refs →

dotfiles-x11.localbinvimv

46 lines · 1.2 KB · Bourne Again Shell (bash)

1#!/usr/bin/env bash
2set -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
8declare -r FILENAMES_FILE=$(mktemp "${TMPDIR:-/tmp}/vimv.XXX")
9
10trap '{ rm -f "${FILENAMES_FILE}" ; }' EXIT
11
12if [ $# -ne 0 ]; then
13 src=( "$@" )
14else
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))'
18fi
19
20for ((i=0;i<${#src[@]};++i)); do
21 echo "${src[i]}" >> "${FILENAMES_FILE}"
22done
23
24${EDITOR:-vi} "${FILENAMES_FILE}"
25
26IFS=$'\r\n' GLOBIGNORE='*' command eval 'dest=($(cat "${FILENAMES_FILE}"))'
27
28if (( ${#src[@]} != ${#dest[@]} )); then
29 echo "WARN: Number of files changed. Did you delete a line by accident? Aborting.." >&2
30 exit 1
31fi
32
33declare -i count=0
34for ((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
44done
45
46echo "$count" files renamed.