5

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
statox
  • 49,782
  • 19
  • 148
  • 225
HendrikVIM
  • 73
  • 5

1 Answers1

8

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)

statox
  • 49,782
  • 19
  • 148
  • 225
  • this is so amazing! thanks so much for including the :diffthis command because rarely do i want to exit vim to run vimdiff if i already have the files/buffers opened! – John Huynh Jan 09 '24 at 23:59