infoclogged
infoclogged

Reputation: 4007

Comparing files with itself in VIM or any other method

I have a file that I want to compare with itself. The first file is the original file and the second file is the one being developed. One method would be git diff, but wont work, if the original itself has been changed and not staged yet. Another method would be to make a copy of it on the file system and use the diff and watch command. This isnt efficient because of creation and deletion of temp files.

I would like to hence try the dodiff command in VIM. VIM can store files as buffer in memory and hence my question is how should I do it?

Please note that the files are not changed from within the vim environment, but from an external editor.

Example:

  1. file1.txt - original
  2. This contents are now changed in the file1.txt
  3. load file1.txt in vim
  4. The contents of the file are again changed
  5. compare 3 wih 4.
  6. The contents of the file are again changed
  7. compare 3 with 6 automatically, maybe something like automatic refresh.

Upvotes: 2

Views: 785

Answers (3)

infoclogged
infoclogged

Reputation: 4007

This is to complete Edwin's answer, that would not fit in a comment.

Another solution will be to load test.yml vim test.yml and then :windo diffthis. Then create a new buffer (a temp file) :vert diffsplit test_original.yml and then read the content in this new buffer :read test.yml.

At this point you should have two windows - one with test_original.yml and the other with test.yml. You have to delete the first line ( dd ) in the test_original.yml, because read appends to the file and hence there is an extra line. The problem here is that vim marks all the lines as changed.

enter image description here

In order to fix this, run :diffoff and again :windo diffthis. Now you would only see one line.

Finally, make the changes externally to test.yml and if you want to see the changes then run :edit, Now you should only see the changes between the original and the changed file.

enter image description here

Once over, just quit with qa!. Since test_original.yml has not been written ( w ), no copy on the filesystem has been made.

Subsequent changes externally, must be reloaded into vim by using the :edit or :checktime command

Upvotes: 2

Edwin
Edwin

Reputation: 2278

Another solution will be to start :windo diffthis then create a new buffer (a temp file) :vert diffsplit file1.txt~ (when you have file1.txt) and then read the content in this new buffer :read file1.txt

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172688

Inside Vim, edited files are read into memory buffers. To create a "snapshot" of the file contents at a certain point in time, you can create a new buffer, and copy the contents from the original buffer to it, using either :%yank | new %.snapshot | put | 1delete _ (clobbers the default register), or :let contents = getline(1, '$') | new %.snapshot | call setline(1, contents) (longer), or more comfortably through my clone plugin via :SCloneAs %.snapshot.

With :set autoread, the original buffer will automatically update to any changes done outside of Vim. You can then diff both windows via :windo diffthis, and update the diff (if necessary) with :diffupdate.

Of course, you can also create new snapshots via :SCloneAs %.snapshot2; remove a snapshot via :bdelete.

Upvotes: 2

Related Questions