summaryrefslogtreecommitdiff
path: root/vimv
blob: 3b3a7bb462a41ad604e731bfa39b364dfcd3ad18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env bash
set -eu

# Lists the current directory's files in Vim, so you can edit it and save to rename them
# USAGE: vimv [file1 file2]
# https://github.com/thameera/vimv

declare -r FILENAMES_FILE=$(mktemp "${TMPDIR:-/tmp}/vimv.XXXXXX")

trap '{ rm -f "${FILENAMES_FILE}" ; }' EXIT

if [ $# -ne 0 ]; then
    src=( "$@" )
else
    IFS=$'\r\n' GLOBIGNORE='*' command eval 'src=($(ls))'
fi

for ((i=0;i<${#src[@]};++i)); do
    echo "${src[i]}" >> "${FILENAMES_FILE}"
done

${EDITOR:-vi} "${FILENAMES_FILE}"

IFS=$'\r\n' GLOBIGNORE='*' command eval 'dest=($(cat "${FILENAMES_FILE}"))'

if (( ${#src[@]} != ${#dest[@]} )); then
    echo "WARN: Number of files changed. Did you delete a line by accident? Aborting.." >&2
    exit 1
fi

declare -i count=0
for ((i=0;i<${#src[@]};++i)); do
    if [ "${src[i]}" != "${dest[i]}" ]; then
        mkdir -p "$(dirname "${dest[i]}")"
        if git ls-files --error-unmatch "${src[i]}" > /dev/null 2>&1; then
            git mv "${src[i]}" "${dest[i]}"
        else
            mv "${src[i]}" "${dest[i]}"
        fi
        ((++count))
    fi
done

echo "$count" files renamed.