Skip to content

Instantly share code, notes, and snippets.

@taikedz
Created April 23, 2018 17:02
Show Gist options
  • Save taikedz/461a811208b231bab0145060ac6e082f to your computer and use it in GitHub Desktop.
Save taikedz/461a811208b231bab0145060ac6e082f to your computer and use it in GitHub Desktop.
Find mid-edit vim files after crash (yes it happens - especially if in a VM and the host is flaky)
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
find . -name '.*.swp' -exec bash "$0" reconcile {} \;
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment