Created
September 11, 2018 14:48
-
-
Save taikedz/3a92daffe76509a20f395a6f55095dbb to your computer and use it in GitHub Desktop.
Find temporary vim swp files, load in new session; on exiting vim, prompt for deletion of swap file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
cred="$(echo -e '\033[31;1m')" | |
cgrn="$(echo -e '\033[32;1m')" | |
cyel="$(echo -e '\033[33;1m')" | |
cblu="$(echo -e '\033[34;1m')" | |
cdef="$(echo -e '\033[0;m')" | |
do_reconcile() { | |
swapfile="$1"; shift | |
realfile="$(dirname "$swapfile")/$(basename "$swapfile"|sed -r 's/^\.(.+)\.swp$/\1/' )" | |
echo "${cblu}$realfile$cdef" | |
sleep 0.5 | |
vim "$realfile" | |
read -p "${cred}Delete$cdef [${cblu}$swapfile${cdef}]? y/N> " | |
if [[ "$REPLY" =~ ^(y|Y|yes|YES)$ ]]; then | |
echo "${cred}Removing $swapfile${cdef}" | |
rm "$swapfile" | |
else | |
echo "${cgrn}Keeping $swapfile${cdef}" | |
fi | |
echo | |
} | |
printhelp() { | |
cat <<'EOF' | |
Find temproary vim "swap" files, try to load them in a new session. | |
After the user exits vim, they are asked whether to delete the swap file. | |
EOF | |
} | |
main() { | |
if [[ "$*" =~ --help ]]; then | |
printhelp | |
exit 0 | |
elif [[ "$1" = reconcile ]]; then | |
do_reconcile "$2" | |
else | |
echo "${cgrn}Searching for swap files ...${cdef}" | |
find . -name '.*.swp' -exec bash "$0" reconcile {} \; | |
echo "${cblu}.... done.${cdef}" | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment