I am about to write a shell script to edit some files.
I'm doing it like this:
/usr/bin/vim $file
Now i want to ask the exit status of vim. If the user closes with saved changes do something (:wq or :x), or if closed without changes (:q, :q!) do something else
if [ vi-exit-status=wq ] ; then
# do this
else # vi didn't save anything
# do that
fi
How can I do that? When I call vi to create a new file and exit it without saving (:q!) and then I check the status via
echo $?
the answer is 0. If I save something or create a file via vi/vim, it's also 0.
Kind regards
The exit status doesn't tell you what the user did in vim. Perhaps you could compare the timestamp on the file before and after to see if it was written to?
This works for me:
echo Edit me > editme.txt
time1=`stat -c%y editme.txt`
vim editme.txt
time2=`stat -c%y editme.txt`
if [ "$time1" != "$time2" ]; then
echo You saved it.
else
echo No change.
fi