toraritte.github.io

How to use Vim to merge files

The pre-requisite to all operations detailed below is to make sure that all participating buffers are in diff mode (see :h start-vimdiff). I use tpope/vim-unimpaired that has the normal mode alias yod to toggle :diffthis on and off easily for a buffer.

Useful normal mode commands (and an extra)

Quirks to watch for

Visual mode and finer grained control

When selecting lines of text in Visual mode, you must use the normal commands:

For example:

  1. Enter Visual mode and mark some text/lines.

  2. Then type :diffput to push the selected lines to the other file or :diffget to get the selected lines from the other file.

To belabor the point: This means that if there is a block of changes consisting of multiple lines, then selecting a subset of lines and issueing :diffput will only apply those changes in the other buffer.

(:diffget and :diffput also accept ranges, see :h copy-diffs for more.)

Compare two buffers inside Vim

If you load up two files in splits (:vs or :sp), you can do :diffthis on each window and achieve a diff of files that were already loaded in buffers.

:diffoff can be used to turn off the diff mode.

This Vimcasts post and video show this in practice.

How to apply all changes between buffers

Get all changes from a buffer to the current one:
:%diffget <buffer-number>

Put all changes from current buffer into another:
:%diffput <buffer-number>

(:% is a range to select the entire file; see :h :%. :ls will show currently opened buffers.)