LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Vim recover + vim scripts when files have .swp file (https://www.linuxquestions.org/questions/linux-general-1/vim-recover-vim-scripts-when-files-have-swp-file-4175603039/)

dedec0 04-02-2017 12:10 PM

Vim recover + vim scripts when files have .swp file
 
I am thinking about a script to automatically recover vim files, substituting the vim command with it:

Code:

# 1. Check if the file (if possible, more than one argument?) has a .swp
# file.

# 2. If it does not, just open the file with vim. End.

# 3. If it does, do:

# 3.2. Recover the file, save it to a temporary folder (no change to
# actual file!). Use /dev/shm to save disk a bit.


-> Vim has the -r option to recover the file as it is opened. But I want
  to save the recovered file somewhere. Never done it, how should I?


# 3.3. Compare the MD5 sums of both files.


-> How should I do it? sed? awk? May you do it to me?


# 3.4. If the sum is the same: delete the .swp file, the temporary file
# and open the file normally. End.

# 3.5. If the sum is not the same, open both files with 'vimdiff -o'

# Unrelated note, do not hijack this thread with answers to it: I have no
# clue why this code tag has so many newlines in its end. I have reported
# this before. Do you see it too? The last 2 lines of the tag are:
this (followed by one other line with the closing code tag)

I will put the full script here when it is ready. It may be useful for others too! (:

dedec0 04-02-2017 03:28 PM

Script: first try
 
I am trying to make most of it, and I reached a point where I don't know exactly where the problem is.

Current script:

Code:

#!/bin/bash
# Automatically erase Vim swap files, when they are not necessary (no information lost)
# Calls `vimdiff -o` if there are unsaved changes

COPIA=$(mktemp /dev/shm/arq.XXX)
echo Copy = $COPIA
VIM_SCRIPT=$(mktemp /dev/shm/vim_script.XXX)
echo Vim script = $VIM_SCRIPT
MD5_SUM=$(mktemp /dev/shm/md5_sum.XXX)
echo MD5 file = $MD5_SUM

# #############
# If no arguments are given, just open vim
if (( $# < 1)); then
    vim
    exit
else
    sleep 1 # should be an "elif more than 1 argument, give all to vim
fi

# #############
# 1. Check if the file (if possible, more than one argument?) has a .swp
# file.

# File should exist and be readable, or we do not proceed
if [[ !( (-r $1) && !(-d $1) ) ]]; then
    echo Cannot read file or it is a directory
    # Swap file exists and is readable?
    if [[ !( ( -r .$1.swp ) && !( -f .$1.swp ) ) ]]; then
        echo Also cannot read swap file
    fi
    exit
else
# #############
# 2. If swap does not exist, just open the file. End.
    # Swap file not there?
    if [[ !( -r .$1.swp ) ]]; then
        echo "Just open"
        vim $1
        exit
    fi

fi


# #############
# 3. If it does, do:

# #############
# 3.2. Recover the file, save it to a temporary folder (no change to
# actual file!). Use /dev/shm to save disk a bit.

# Make a vim script file to save the temporary fily
echo ":sav $COPIA" > $VIM_SCRIPT
echo ":q!" >> $VIM_SCRIPT

# This recovers the file with the swap file, executes the vim script to
# save it somewhere else and quits, making no change to the original file
echo chamando: vim -r $1 -s $VIM_SCRIPT
vim -r $1 -s $VIM_SCRIPT

# #############
# 3.3. Compare the MD5 sums of both files.

echo "line: md5sum $COPIA"
recovered_md5=$( md5sum $COPIA | sed -e 's/\s.*$//' )
echo Rec: $recovered_md5
echo Arqmd5: $MD5_SUM
echo -e "$recovered_md5        $1" > $MD5_SUM

echo Checking
if md5sum --status -c $MD5_SUM; then
    echo samesum=1
else
    echo samesum=0
fi


# #############
# 3.4. If the sum is the same: delete the .swp file, the temporary file
# and open the file normally. End.
declare -i x=0
((x)) && echo "x zero" $x

x=1
((x)) && echo "x um" $x

# #############
# 3.5. If the sum is not the same, open both files with 'vimdiff -o'

Steps to test:

Code:

cd /dev/shm
 echo something > teste.txt
 vim teste.txt

 # In another terminal, kill the vim we just started:
 kill -9 $that_vim

 # Wherever:
 cd /dev/shm
 /path/to/script.sh teste.txt

The output for script.sh is:

Code:

$/var/tmp/rec.sh teste.txt
Copy = /dev/shm/arq.9Zh
Vim script = /dev/shm/vim_script.CYZ
MD5 file = /dev/shm/md5_sum.C3s
calling: vim -r teste.txt -s /dev/shm/vim_script.CYZ
line: md5sum /dev/shm/arq.9Zh
^[[>1;2305;0cRec: d41d8cd98f00b204e9800998ecf8427e
Arqmd5: /dev/shm/md5_sum.C3s
Checking
md5sum:      teste.txt: File or directory not found
samesum=0
x um 1

I do not understand why md5sum cannot find the file. It is in the current directory! I also do not understand why some garbage is written in the line below "line: md5sum /dev/shm/arq.9Zh".

Does this thread stop being in the email messages "Zero reply threads" when I reply to myself? It should not, right? At least while I cannot solve the problem.

dedec0 04-08-2017 03:09 PM

Still no comment? Maybe the questions I have made inside the script were not imagined by who read this thread? Or am I hasty and should wait a few more days?


All times are GMT -5. The time now is 09:52 PM.