summaryrefslogtreecommitdiff
path: root/.local/bin/vimv
diff options
context:
space:
mode:
authorSaumit Dinesan <justsaumit@protonmail.com>2023-03-15 11:35:51 +0530
committerSaumit Dinesan <justsaumit@protonmail.com>2023-03-15 11:35:51 +0530
commitfa7a8015feb5ff3e4d158e2d7f1b82fe94091555 (patch)
tree6e111a5ec2f5709898a2d43e637b137cbaa8a595 /.local/bin/vimv
parent46f86978d0f8c7538b615ba60e1332cc280f5cd0 (diff)
.local: adding some envscripts+udev rules for brillo
Diffstat (limited to '.local/bin/vimv')
-rwxr-xr-x.local/bin/vimv44
1 files changed, 44 insertions, 0 deletions
diff --git a/.local/bin/vimv b/.local/bin/vimv
new file mode 100755
index 0000000..74844fc
--- /dev/null
+++ b/.local/bin/vimv
@@ -0,0 +1,44 @@
+#!/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.XXX")
+
+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.