3

I have two files in two different buffers and I want to get the difference between these two files in another file

How can I do this with Vim?

Example:

BUFFER1

rtl.de
sat1.de
pro7.de
spiegel.de

BUFFER2

rtl.de
sat1.de
heise.de
pro7.de
spiegel.de

buffer3

heise.de
5

Vim offers a tool to see the difference between two buffers and put contents from one to another: diffthis (see :h :diffthis).

To get this diff mode you can either start it from the command line with:

$ vimdiff file1 file2

Or from within Vim:

  • Open both of the buffers.
  • create a split window to get both of the buffers on your screen. (with :split and :e for example)
  • Start the diff mode with :windo diffthis
  • Use :diffput and :diffget to modify the content of the buffers (:h :diffput and :h :diffget).
  • Use :diffoff to stop the diff mode.

Now to put the diff of the files in a third one I would recommend to use the diff from the (shell) command line with something like (See man diff):

$diff file1 file2 > file3

From within Vim you can get the result of this command in a buffer with:

:read !diff file1 file2

read allows to get the result of a command in the current buffer and ! allows to specify a string to be executed as an external command (here diff file1 file2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.