Check the changes made to file in vi

Hi,

I use vi editor in Unix.

Is there any way if we can know that what change was made to the file opened in vi before quitting?
As i opened a huge file made some changes yesterday and didnt save it.
Today when i was quitting the vi , i came to know that some changes are made(as i got warning with q option).I need to know what changes were made , so i can save it or discard it.

Is there any way of doing this?

Thanks,
Kailash

If your file is called "edit-this", try

more .edit-this.swp
:w /tmp/current
:!diff -c /tmp/current % | less

---------- Post updated at 23:53 ---------- Previous update was at 23:52 ----------

This is vim specific.

Thanks fo reply.
But no success , i dont have that swap file.

---------- Post updated at 04:57 PM ---------- Previous update was at 04:54 PM ----------

Well , yes there's option to save the file with diff name and compare :slight_smile:

Thanks.

---------- Post updated at 04:59 PM ---------- Previous update was at 04:57 PM ----------

Scrutinizer,

I dont have vim , if its vim specific ;thats why it didn't work.
I will try to check with vim.
Thanks

kailash19, what scrutinizers and jiliagres suggestions have in common is this:

As vi opens a file it creates a backup copy somewhere. Depending on the specific flavour of vi or vim you use it might be in different locations: the vi under AIX creates this file in /var/tmp, vim uses .<filename>.swp in the same directory as the file, etc.. Real vi's delete this backup copy upon leaving the editor, but some vi-clones (like vim) retain these files until explicitly configured to do otherwise.

By using the "diff" utility you can compare the text you have right now in your editing buffer (the file in the open editing session) and the contents of this backup copy of the original file and find out the differences. You can even write these differences to a file and open this in another editor window as vi is multi-windowed (see the ":e", ":n" and ":rew" commands in the man pages).

I hope this helps.

bakunin

To clarify, I wasn't suggesting to use any backup file that vi itself possibly creates by itself but to save the current buffer in a temporary file and compare it to the original one. This can be done simply with the two commands I already posted:

:w /tmp/current
:!diff -c /tmp/current % | less

(replace less by more if less is missing).
The backup files created by the traditional vi in /var/tmp aren't usable for that task as not being a plain copy of the current file but a structured binary designed to allow file recovery should the editor crashed (vi -r). This applies also to vim .swp files which aren't either editable text files.

Thanks all for your replies.

I used diff for the rescue :slight_smile: .

Discussion was knowledgeable.