Skip to content

Instantly share code, notes, and snippets.

@taikedz
Created April 23, 2019 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taikedz/5b63850b9e39e3367b7dad6039cee8d0 to your computer and use it in GitHub Desktop.
Save taikedz/5b63850b9e39e3367b7dad6039cee8d0 to your computer and use it in GitHub Desktop.
Reconcile vim swap files

Vim creates temporary files with the *.swp extension when editing files; if the session is terminated or the computer reboots/crashes etc without cleanly exiting, these stick around.

This script seqarches for such files, and allows the user to use vim to check that the file actually has the latest changes from the swap, then offers to delete the swap file.

The script uses bash builder syntax and inclusions, and can be run directly in bbrun. It can also be be built to a standalone script.

#!/usr/bin/env bbrun
### reconcile-swp.sh SEARCHDIR Usage:help
#
# Find temporary 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.
#
###/doc
#%include std/out.sh
#%include std/colours.sh
#%include std/autohelp.sh
#%include std/syntax-extensions.sh
#%include std/askuser.sh
$%function rswp:real_name(*p_realfile swapfile) {
p_realfile="$(dirname "$swapfile")/$(basename "$swapfile"|sed -r 's/^\.(.+)\.swp$/\1/' )"
}
$%function rswp:do_reconcile(swapfile) {
local realfile
rswp:real_name realfile "$swapfile"
if askuser:confirm "${CBPUR}Swap found for '$realfile'. Edit?"; then
vim "$realfile"
fi
if askuser:confirm "${CBRED}Delete$CDEF [${CBBLU}$swapfile${CDEF}]?"; then
out:warn "Removing $swapfile"
rm "$swapfile"
else
out:info "Keeping $swapfile"
fi
echo
}
rswp:search() {
out:info "Searching for swap files ..."
find . -name '.*.swp' -exec bash "$0" reconcile {} \;
out:info ".... done."
}
main() {
autohelp:check "$@"
if [[ "$1" = reconcile ]]; then
rswp:do_reconcile "$2"
else
rswp:search
fi
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment