LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-02-2017, 12:10 PM   #1
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Rep: Reputation: 51
Question 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! (:
 
Old 04-02-2017, 03:28 PM   #2
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Unhappy 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.

Last edited by dedec0; 04-03-2017 at 04:11 PM. Reason: A few script comments and other details rewritten in English
 
Old 04-08-2017, 03:09 PM   #3
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
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?
 
  


Reply

Tags
vim



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Fedora shows 'man vim' but when execute 'vim' got "bash: vim: command not found..." ? flash_os Linux - Newbie 19 01-03-2015 11:56 PM
[SOLVED] How to make Vim (scripts) write to stdout? (or, how to use Vim as a filter program?) dedec0 Linux - Software 13 02-04-2013 05:45 AM
VIM wont close, keeps leaveing swp files skinney Linux - Newbie 5 10-13-2012 04:58 PM
Switching from vim to vim -g from inside vim iDragoon Linux - Software 4 05-15-2009 11:46 AM
Editor comparison: vim VS vim-lite, Cleaning vim Ruler2112 *BSD 4 04-13-2009 04:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration