Home Explore Blog CI



neovim

1st chunk of `runtime/doc/diff.txt`
d66210e62db3c23b768c8b26160bccf7b7dd0eeb3eed93340000000100000fa8
*diff.txt*      Nvim


		  VIM REFERENCE MANUAL    by Bram Moolenaar


							*diff* *diff-mode*
This file describes the diff feature: Showing differences between two to
eight versions of the same file.

The basics are explained in section |08.7| of the user manual.

                                      Type |gO| to see the table of contents.

==============================================================================
1. Starting diff mode					*start-vimdiff*

To start editing in diff mode, run "nvim -d".  This starts Nvim as usual, and
additionally sets up for viewing the differences between the arguments. >

	nvim -d file1 file2 [file3 [file4]]

In addition to the |-d| argument, |-R| may be used for readonly mode.

The second and following arguments may also be a directory name.  Vim will
then append the file name of the first argument to the directory name to find
the file.

By default an internal diff library will be used.  When 'diffopt' or
'diffexpr' has been set an external "diff" command will be used.  This only
works when such a diff program is available.

Diffs are local to the current tab page |tab-page|.  You can't see diffs with
a window in another tab page.  This does make it possible to have several
diffs at the same time, each in their own tab page.

What happens is that Nvim opens a window for each of the files.  This is like
using the |-O| argument.  This uses vertical splits, but if you prefer
horizontal splits use the |-o| argument instead: >

	nvim -d -o file1 file2 [file3 [file4]]

If you always prefer horizontal splits include "horizontal" in 'diffopt'.

In each of the edited files these options are set:

	'diff'		on
	'scrollbind'	on
	'cursorbind'	on
	'scrollopt'	includes "hor"
	'wrap'		off, or leave as-is if 'diffopt' includes "followwrap"
	'foldmethod'	"diff"
	'foldcolumn'	value from 'diffopt', default is 2

These options are set local to the window.  When editing another file they are
reset to the global value.
The options can still be overruled from a modeline when re-editing the file.
However, 'foldmethod' and 'wrap' won't be set from a modeline when 'diff' is
set.
See `:diffoff` for an easy way to revert the options.

The differences shown are actually the differences in the buffer.  Thus if you
make changes after loading a file, these will be included in the displayed
diffs.  You might have to do ":diffupdate" now and then, not all changes are
immediately taken into account, especially when using an external diff command.

In your vimrc file you could do something special when Vim was started in
diff mode.  You could use a construct like this: >

	if &diff
	   setup for diff mode
	else
	   setup for non-diff mode
	endif

While already in Vim you can start diff mode in three ways.

							*E98*
:diffs[plit] {filename}					*:diffs* *:diffsplit*
		Open a new window on the file {filename}.  The options are set
		as for "nvim -d" for the current and the newly opened window.
		Also see 'diffexpr'.

							*:difft* *:diffthis*
:difft[his]	Make the current window part of the diff windows.  This sets
		the options as for "nvim -d".

:diffp[atch] {patchfile}			 *E816* *:diffp* *:diffpatch*
		Use the current buffer, patch it with the diff found in
		{patchfile} and open a buffer on the result.  This sets the
		options as for "nvim -d".
		{patchfile} can be in any format that the "patch" program
		understands or 'patchexpr' can handle.
		Note that {patchfile} should only contain a diff for one file,
		the current file.  If {patchfile} contains diffs for other
		files as well, the results are unpredictable.  Vim changes
		directory to /tmp to avoid files in the current directory
		accidentally being patched.  But it may still result in
		various ".rej" files to be created.  And when absolute path
		names are present these files may get patched anyway.

To make these commands use a vertical split, prepend |:vertical|.  Examples: >

	:vert diffsplit main.c~
	:vert diffpatch /tmp/diff

If you always prefer a vertical

Title: Starting and Using Diff Mode in Nvim
Summary
This section explains how to use Nvim's diff mode to compare multiple versions of a file. It covers starting diff mode with `nvim -d`, using different split orientations, understanding the options set in diff mode (like 'diff', 'scrollbind', etc.), and applying diffs to buffers. Also explains how to start diff mode while already in a Vim session, by using commands like `:diffsplit`, `:diffthis`, and `:diffpatch`.