[Assorted-commits] SF.net SVN: assorted:[1538] shell-tools/trunk/src/recover-vim.bash
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-12-31 01:00:01
|
Revision: 1538 http://assorted.svn.sourceforge.net/assorted/?rev=1538&view=rev Author: yangzhang Date: 2009-12-31 00:59:53 +0000 (Thu, 31 Dec 2009) Log Message: ----------- added recover-vim Added Paths: ----------- shell-tools/trunk/src/recover-vim.bash Added: shell-tools/trunk/src/recover-vim.bash =================================================================== --- shell-tools/trunk/src/recover-vim.bash (rev 0) +++ shell-tools/trunk/src/recover-vim.bash 2009-12-31 00:59:53 UTC (rev 1538) @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +# Finds vim swap files and tries to recover from them. Note: make sure you're +# not working on actual in-use swap files. + +. common.bash || exit 1 + +set -o errexit -o nounset + +# This function is useful if you don't have `set directory=<something other +# than .>`. + +old() { + local a= + while read -r i ; do + a=( "${a[@]}" "$i" ) + done < <( echo "$@" | sed 's/ /\n/g' | # find . -name '*.swp' | + sed 's/\(.*\/\)\.\(.\+\)\.swp/\1\2/' ) + for i in "${a[@]}" ; do + if [[ "$i" == '' ]] ; then continue ; fi + rm -f "$i.recovered" + if [[ ! -f "$i" ]] ; then + echo -e "${fg_bright_red}$i not found${normal_color}" ; read + else + local del=false + vim -r "$i" -X -c "sav $i.recovered | q" || true + if is-empty-file "$i.recovered" ; then + echo -e "${fg_yellow}$i.recovered is empty; deleting swp${normal_color}" + del=true + fi + if diff -q "$i" "$i.recovered" ; then + echo -e "${fg_bright_green}$i.recovered no diff than $i; deleting swp${normal_color}" + del=true + fi + if $del + then rm "$(dirname "$i")/.$(basename "$i").swp" "$i.recovered" + else echo -e "${fg_bright_red}NOT DELETING${normal_color}" ; read + fi + fi + done +} + +old "$@" +exit + +# <http://stackoverflow.com/questions/63104/smarter-vim-recovery> +TMPDIR=$(mktemp -d) || exit 1 +RECTXT="$TMPDIR/vim.recovery.$USER.txt" +RECFN="$TMPDIR/vim.recovery.$USER.fn" +trap 'rm -f "$RECTXT" "$RECFN"; rmdir "$TMPDIR"' 0 1 2 3 15 +for q in ~/.vim/swap/.*sw? ~/.vim/swap/*; do + [[ -f $q ]] || continue + rm -f "$RECTXT" "$RECFN" + vim -X -r "$q" \ + -c "w! $RECTXT" \ + -c "let fn=expand('%')" \ + -c "new $RECFN" \ + -c "exec setline( 1, fn )" \ + -c w\! \ + -c "qa" + if [[ ! -f $RECFN ]]; then + echo "nothing to recover from $q" + rm -f "$q" + continue + fi + CRNT="$(cat $RECFN)" + if diff --strip-trailing-cr --brief "$CRNT" "$RECTXT"; then + echo "removing redundant $q" + echo " for $CRNT" + rm -f "$q" + else + echo $q contains changes + vim -n -d "$CRNT" "$RECTXT" + rm -i "$q" || exit + fi +done Property changes on: shell-tools/trunk/src/recover-vim.bash ___________________________________________________________________ Added: svn:executable + * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |