xref: /vim-8.2.3635/runtime/doc/diff.txt (revision d2ea7cf1)
1*d2ea7cf1SBram Moolenaar*diff.txt*      For Vim version 8.2.  Last change: 2021 May 24
2071d4279SBram Moolenaar
3071d4279SBram Moolenaar
4071d4279SBram Moolenaar		  VIM REFERENCE MANUAL    by Bram Moolenaar
5071d4279SBram Moolenaar
6071d4279SBram Moolenaar
7071d4279SBram Moolenaar				*diff* *vimdiff* *gvimdiff* *diff-mode*
8015efc32SBram MoolenaarThis file describes the |+diff| feature: Showing differences between two to
9015efc32SBram Moolenaareight versions of the same file.
10071d4279SBram Moolenaar
11071d4279SBram MoolenaarThe basics are explained in section |08.7| of the user manual.
12071d4279SBram Moolenaar
1324a98a0eSBram Moolenaar1. Starting diff mode		|start-vimdiff|
14071d4279SBram Moolenaar2. Viewing diffs		|view-diffs|
15071d4279SBram Moolenaar3. Jumping to diffs		|jumpto-diffs|
16071d4279SBram Moolenaar4. Copying diffs		|copy-diffs|
17071d4279SBram Moolenaar5. Diff options			|diff-options|
18071d4279SBram Moolenaar
19071d4279SBram Moolenaar==============================================================================
2024a98a0eSBram Moolenaar1. Starting diff mode					*start-vimdiff*
21071d4279SBram Moolenaar
22071d4279SBram MoolenaarThe easiest way to start editing in diff mode is with the "vimdiff" command.
23071d4279SBram MoolenaarThis starts Vim as usual, and additionally sets up for viewing the differences
24071d4279SBram Moolenaarbetween the arguments. >
25071d4279SBram Moolenaar
26071d4279SBram Moolenaar	vimdiff file1 file2 [file3 [file4]]
27071d4279SBram Moolenaar
28071d4279SBram MoolenaarThis is equivalent to: >
29071d4279SBram Moolenaar
30071d4279SBram Moolenaar	vim -d file1 file2 [file3 [file4]]
31071d4279SBram Moolenaar
32071d4279SBram MoolenaarYou may also use "gvimdiff" or "vim -d -g".  The GUI is started then.
33071d4279SBram MoolenaarYou may also use "viewdiff" or "gviewdiff".  Vim starts in readonly mode then.
34071d4279SBram Moolenaar"r" may be prepended for restricted mode (see |-Z|).
35071d4279SBram Moolenaar
36071d4279SBram MoolenaarThe second and following arguments may also be a directory name.  Vim will
37071d4279SBram Moolenaarthen append the file name of the first argument to the directory name to find
38071d4279SBram Moolenaarthe file.
39071d4279SBram Moolenaar
40e828b762SBram MoolenaarBy default an internal diff library will be used.  When 'diffopt' or
41e828b762SBram Moolenaar'diffexpr' has been set an external "diff" command will be used.  This only
42e828b762SBram Moolenaarworks when such a diff program is available.
43071d4279SBram Moolenaar
447e8fd636SBram MoolenaarDiffs are local to the current tab page |tab-page|.  You can't see diffs with
457e8fd636SBram Moolenaara window in another tab page.  This does make it possible to have several
467e8fd636SBram Moolenaardiffs at the same time, each in their own tab page.
477e8fd636SBram Moolenaar
48071d4279SBram MoolenaarWhat happens is that Vim opens a window for each of the files.  This is like
49071d4279SBram Moolenaarusing the |-O| argument.  This uses vertical splits.  If you prefer horizontal
50071d4279SBram Moolenaarsplits add the |-o| argument: >
51071d4279SBram Moolenaar
52e37d50a5SBram Moolenaar	vimdiff -o file1 file2 [file3 [file4]]
53071d4279SBram Moolenaar
547d47b6eeSBram MoolenaarIf you always prefer horizontal splits include "horizontal" in 'diffopt'.
557d47b6eeSBram Moolenaar
56071d4279SBram MoolenaarIn each of the edited files these options are set:
57071d4279SBram Moolenaar
58071d4279SBram Moolenaar	'diff'		on
59071d4279SBram Moolenaar	'scrollbind'	on
60a9d52e3bSBram Moolenaar	'cursorbind'	on
61071d4279SBram Moolenaar	'scrollopt'	includes "hor"
624223d43cSBram Moolenaar	'wrap'		off, or leave as-is if 'diffopt' includes "followwrap"
63071d4279SBram Moolenaar	'foldmethod'	"diff"
647d47b6eeSBram Moolenaar	'foldcolumn'	value from 'diffopt', default is 2
65071d4279SBram Moolenaar
66071d4279SBram MoolenaarThese options are set local to the window.  When editing another file they are
67071d4279SBram Moolenaarreset to the global value.
688f3f58f2SBram MoolenaarThe options can still be overruled from a modeline when re-editing the file.
698f3f58f2SBram MoolenaarHowever, 'foldmethod' and 'wrap' won't be set from a modeline when 'diff' is
708f3f58f2SBram Moolenaarset.
715ef1c6a4SBram MoolenaarSee `:diffoff` for an easy way to revert the options.
72071d4279SBram Moolenaar
73071d4279SBram MoolenaarThe differences shown are actually the differences in the buffer.  Thus if you
74071d4279SBram Moolenaarmake changes after loading a file, these will be included in the displayed
75071d4279SBram Moolenaardiffs.  You might have to do ":diffupdate" now and then, not all changes are
765ef1c6a4SBram Moolenaarimmediately taken into account, especially when using an external diff command.
77071d4279SBram Moolenaar
78071d4279SBram MoolenaarIn your .vimrc file you could do something special when Vim was started in
79071d4279SBram Moolenaardiff mode.  You could use a construct like this: >
80071d4279SBram Moolenaar
81071d4279SBram Moolenaar	if &diff
82071d4279SBram Moolenaar	   setup for diff mode
83071d4279SBram Moolenaar	else
84071d4279SBram Moolenaar	   setup for non-diff mode
85071d4279SBram Moolenaar	endif
86071d4279SBram Moolenaar
87071d4279SBram MoolenaarWhile already in Vim you can start diff mode in three ways.
88071d4279SBram Moolenaar
89071d4279SBram Moolenaar							*E98*
90d09acef4SBram Moolenaar:diffs[plit] {filename}					*:diffs* *:diffsplit*
91071d4279SBram Moolenaar		Open a new window on the file {filename}.  The options are set
92071d4279SBram Moolenaar		as for "vimdiff" for the current and the newly opened window.
93071d4279SBram Moolenaar		Also see 'diffexpr'.
94071d4279SBram Moolenaar
95071d4279SBram Moolenaar							*:difft* *:diffthis*
96d09acef4SBram Moolenaar:difft[his]	Make the current window part of the diff windows.  This sets
972df6dcc5SBram Moolenaar		the options like for "vimdiff".
98071d4279SBram Moolenaar
99d09acef4SBram Moolenaar:diffp[atch] {patchfile}			 *E816* *:diffp* *:diffpatch*
100071d4279SBram Moolenaar		Use the current buffer, patch it with the diff found in
101071d4279SBram Moolenaar		{patchfile} and open a buffer on the result.  The options are
102071d4279SBram Moolenaar		set as for "vimdiff".
103071d4279SBram Moolenaar		{patchfile} can be in any format that the "patch" program
104071d4279SBram Moolenaar		understands or 'patchexpr' can handle.
105071d4279SBram Moolenaar		Note that {patchfile} should only contain a diff for one file,
106071d4279SBram Moolenaar		the current file.  If {patchfile} contains diffs for other
107071d4279SBram Moolenaar		files as well, the results are unpredictable.  Vim changes
108071d4279SBram Moolenaar		directory to /tmp to avoid files in the current directory
109071d4279SBram Moolenaar		accidentally being patched.  But it may still result in
110071d4279SBram Moolenaar		various ".rej" files to be created.  And when absolute path
111071d4279SBram Moolenaar		names are present these files may get patched anyway.
112071d4279SBram Moolenaar
113071d4279SBram MoolenaarTo make these commands use a vertical split, prepend |:vertical|.  Examples: >
114071d4279SBram Moolenaar
115071d4279SBram Moolenaar	:vert diffsplit main.c~
116071d4279SBram Moolenaar	:vert diffpatch /tmp/diff
1177d47b6eeSBram Moolenaar
1187d47b6eeSBram MoolenaarIf you always prefer a vertical split include "vertical" in 'diffopt'.
1197d47b6eeSBram Moolenaar
120071d4279SBram Moolenaar							*E96*
121015efc32SBram MoolenaarThere can be up to eight buffers with 'diff' set.
122071d4279SBram Moolenaar
123071d4279SBram MoolenaarSince the option values are remembered with the buffer, you can edit another
124071d4279SBram Moolenaarfile for a moment and come back to the same file and be in diff mode again.
125071d4279SBram Moolenaar
1262df6dcc5SBram Moolenaar							*:diffo* *:diffoff*
127baca7f70SBram Moolenaar:diffo[ff]	Switch off diff mode for the current window.  Resets related
128baca7f70SBram Moolenaar		options also when 'diff' was not set.
1292df6dcc5SBram Moolenaar
130d09acef4SBram Moolenaar:diffo[ff]!	Switch off diff mode for the current window and in all windows
131baca7f70SBram Moolenaar		in the current tab page where 'diff' is set.  Resetting
132baca7f70SBram Moolenaar		related options only happens in a window that has 'diff' set,
133baca7f70SBram Moolenaar		if the current window does not have 'diff' set then no options
134baca7f70SBram Moolenaar		in it are changed.
1353df0173fSBram Moolenaar		Hidden buffers are also removed from the list of diff'ed
1363df0173fSBram Moolenaar		buffers.
1372df6dcc5SBram Moolenaar
13886ae720dSBram MoolenaarThe `:diffoff` command resets the relevant options to the values they had when
13986ae720dSBram Moolenaarusing `:diffsplit`, `:diffpatch` , `:diffthis`. or starting Vim in diff mode.
14086ae720dSBram MoolenaarWhen using `:diffoff` twice the last saved values are restored.
141dd007ed5SBram MoolenaarOtherwise they are set to their default value:
1422df6dcc5SBram Moolenaar
1432df6dcc5SBram Moolenaar	'diff'		off
1442df6dcc5SBram Moolenaar	'scrollbind'	off
145a9d52e3bSBram Moolenaar	'cursorbind'	off
1462df6dcc5SBram Moolenaar	'scrollopt'	without "hor"
1474223d43cSBram Moolenaar	'wrap'		on, or leave as-is if 'diffopt' includes "followwrap"
1482df6dcc5SBram Moolenaar	'foldmethod'	"manual"
1492df6dcc5SBram Moolenaar	'foldcolumn'	0
150071d4279SBram Moolenaar
151071d4279SBram Moolenaar==============================================================================
152071d4279SBram Moolenaar2. Viewing diffs						*view-diffs*
153071d4279SBram Moolenaar
154071d4279SBram MoolenaarThe effect is that the diff windows show the same text, with the differences
155071d4279SBram Moolenaarhighlighted.  When scrolling the text, the 'scrollbind' option will make the
156071d4279SBram Moolenaartext in other windows to be scrolled as well.  With vertical splits the text
157071d4279SBram Moolenaarshould be aligned properly.
158071d4279SBram Moolenaar
159071d4279SBram MoolenaarThe alignment of text will go wrong when:
160071d4279SBram Moolenaar- 'wrap' is on, some lines will be wrapped and occupy two or more screen
161071d4279SBram Moolenaar  lines
162071d4279SBram Moolenaar- folds are open in one window but not another
163071d4279SBram Moolenaar- 'scrollbind' is off
164071d4279SBram Moolenaar- changes have been made to the text
165071d4279SBram Moolenaar- "filler" is not present in 'diffopt', deleted/inserted lines makes the
166071d4279SBram Moolenaar  alignment go wrong
167071d4279SBram Moolenaar
168071d4279SBram MoolenaarAll the buffers edited in a window where the 'diff' option is set will join in
169071d4279SBram Moolenaarthe diff.  This is also possible for hidden buffers.  They must have been
1703df0173fSBram Moolenaaredited in a window first for this to be possible.  To get rid of the hidden
1713df0173fSBram Moolenaarbuffers use `:diffoff!`.
172071d4279SBram Moolenaar
1739964e468SBram Moolenaar					*:DiffOrig* *diff-original-file*
174071d4279SBram MoolenaarSince 'diff' is a window-local option, it's possible to view the same buffer
175071d4279SBram Moolenaarin diff mode in one window and "normal" in another window.  It is also
1769964e468SBram Moolenaarpossible to view the changes you have made to a buffer since the file was
1779964e468SBram Moolenaarloaded.  Since Vim doesn't allow having two buffers for the same file, you
1789964e468SBram Moolenaarneed another buffer.  This command is useful: >
1798e5af3e5SBram Moolenaar	 command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_
1808e5af3e5SBram Moolenaar	 	\ | diffthis | wincmd p | diffthis
18126967617SBram Moolenaar(this is in |defaults.vim|).  Use ":DiffOrig" to see the differences between
18226967617SBram Moolenaarthe current buffer and the file it was loaded from.
183071d4279SBram Moolenaar
184071d4279SBram MoolenaarA buffer that is unloaded cannot be used for the diff.  But it does work for
185071d4279SBram Moolenaarhidden buffers.  You can use ":hide" to close a window without unloading the
186111ff9f4SBram Moolenaarbuffer.  If you don't want a buffer to remain used for the diff do ":set
187111ff9f4SBram Moolenaarnodiff" before hiding it.
188071d4279SBram Moolenaar
18961da1bfaSBram Moolenaar						*:dif* *:diff* *:diffupdate*
190b4ff518dSBram Moolenaar:dif[fupdate][!]		Update the diff highlighting and folds.
191a7fc0101SBram Moolenaar
192071d4279SBram MoolenaarVim attempts to keep the differences updated when you make changes to the
193071d4279SBram Moolenaartext.  This mostly takes care of inserted and deleted lines.  Changes within a
194071d4279SBram Moolenaarline and more complicated changes do not cause the differences to be updated.
195071d4279SBram MoolenaarTo force the differences to be updated use: >
196071d4279SBram Moolenaar
197071d4279SBram Moolenaar	:diffupdate
198071d4279SBram Moolenaar
199bd1d5608SBram MoolenaarIf the ! is included Vim will check if the file was changed externally and
200bd1d5608SBram Moolenaarneeds to be reloaded.  It will prompt for each changed file, like `:checktime`
201bd1d5608SBram Moolenaarwas used.
202071d4279SBram Moolenaar
203071d4279SBram MoolenaarVim will show filler lines for lines that are missing in one window but are
204071d4279SBram Moolenaarpresent in another.  These lines were inserted in another file or deleted in
205071d4279SBram Moolenaarthis file.  Removing "filler" from the 'diffopt' option will make Vim not
206071d4279SBram Moolenaardisplay these filler lines.
207071d4279SBram Moolenaar
208071d4279SBram Moolenaar
209071d4279SBram MoolenaarFolds are used to hide the text that wasn't changed.  See |folding| for all
210071d4279SBram Moolenaarthe commands that can be used with folds.
211071d4279SBram Moolenaar
212071d4279SBram MoolenaarThe context of lines above a difference that are not included in the fold can
213071d4279SBram Moolenaarbe set with the 'diffopt' option.  For example, to set the context to three
214071d4279SBram Moolenaarlines: >
215071d4279SBram Moolenaar
216071d4279SBram Moolenaar	:set diffopt=filler,context:3
217071d4279SBram Moolenaar
218071d4279SBram Moolenaar
219071d4279SBram MoolenaarThe diffs are highlighted with these groups:
220071d4279SBram Moolenaar
221071d4279SBram Moolenaar|hl-DiffAdd|	DiffAdd		Added (inserted) lines.  These lines exist in
222071d4279SBram Moolenaar				this buffer but not in another.
223071d4279SBram Moolenaar|hl-DiffChange|	DiffChange	Changed lines.
224071d4279SBram Moolenaar|hl-DiffText|	DiffText	Changed text inside a Changed line.  Vim
225071d4279SBram Moolenaar				finds the first character that is different,
226071d4279SBram Moolenaar				and the last character that is different
227071d4279SBram Moolenaar				(searching from the end of the line).  The
228071d4279SBram Moolenaar				text in between is highlighted.  This means
229071d4279SBram Moolenaar				that parts in the middle that are still the
23037c64c78SBram Moolenaar				same are highlighted anyway.  The 'diffopt'
23137c64c78SBram Moolenaar				flags "iwhite" and "icase" are used here.
232071d4279SBram Moolenaar|hl-DiffDelete|	DiffDelete	Deleted lines.  Also called filler lines,
233071d4279SBram Moolenaar				because they don't really exist in this
234071d4279SBram Moolenaar				buffer.
235071d4279SBram Moolenaar
236071d4279SBram Moolenaar==============================================================================
237071d4279SBram Moolenaar3. Jumping to diffs					*jumpto-diffs*
238071d4279SBram Moolenaar
239071d4279SBram MoolenaarTwo commands can be used to jump to diffs:
240071d4279SBram Moolenaar								*[c*
241071d4279SBram Moolenaar	[c		Jump backwards to the previous start of a change.
242071d4279SBram Moolenaar			When a count is used, do it that many times.
243071d4279SBram Moolenaar								*]c*
244071d4279SBram Moolenaar	]c		Jump forwards to the next start of a change.
245071d4279SBram Moolenaar			When a count is used, do it that many times.
246071d4279SBram Moolenaar
247071d4279SBram MoolenaarIt is an error if there is no change for the cursor to move to.
248071d4279SBram Moolenaar
249071d4279SBram Moolenaar==============================================================================
250071d4279SBram Moolenaar4. Diff copying			*copy-diffs* *E99* *E100* *E101* *E102* *E103*
2511e015460SBram Moolenaar								*merge*
252071d4279SBram MoolenaarThere are two commands to copy text from one buffer to another.  The result is
253071d4279SBram Moolenaarthat the buffers will be equal within the specified range.
254071d4279SBram Moolenaar
255071d4279SBram Moolenaar							*:diffg* *:diffget*
256071d4279SBram Moolenaar:[range]diffg[et] [bufspec]
257071d4279SBram Moolenaar		Modify the current buffer to undo difference with another
2588f3f58f2SBram Moolenaar		buffer.  If [bufspec] is given, that buffer is used.  If
2598f3f58f2SBram Moolenaar		[bufspec] refers to the current buffer then nothing happens.
260071d4279SBram Moolenaar		Otherwise this only works if there is one other buffer in diff
261071d4279SBram Moolenaar		mode.
262071d4279SBram Moolenaar		See below for [range].
263071d4279SBram Moolenaar
2649964e468SBram Moolenaar						*:diffpu* *:diffput* *E793*
265071d4279SBram Moolenaar:[range]diffpu[t] [bufspec]
266071d4279SBram Moolenaar		Modify another buffer to undo difference with the current
267071d4279SBram Moolenaar		buffer.  Just like ":diffget" but the other buffer is modified
268071d4279SBram Moolenaar		instead of the current one.
2691e015460SBram Moolenaar		When [bufspec] is omitted and there is more than one other
2701e015460SBram Moolenaar		buffer in diff mode where 'modifiable' is set this fails.
271071d4279SBram Moolenaar		See below for [range].
272071d4279SBram Moolenaar
273071d4279SBram Moolenaar							*do*
27432efaf61SBram Moolenaar[count]do	Same as ":diffget" without range.  The "o" stands for "obtain"
27532efaf61SBram Moolenaar		("dg" can't be used, it could be the start of "dgg"!). Note:
27632efaf61SBram Moolenaar		this doesn't work in Visual mode.
27732efaf61SBram Moolenaar		If you give a [count], it is used as the [bufspec] argument
27832efaf61SBram Moolenaar		for ":diffget".
279071d4279SBram Moolenaar
280071d4279SBram Moolenaar							*dp*
28132efaf61SBram Moolenaar[count]dp	Same as ":diffput" without range.  Note: this doesn't work in
28232efaf61SBram Moolenaar		Visual mode.
28332efaf61SBram Moolenaar		If you give a [count], it is used as the [bufspec] argument
28432efaf61SBram Moolenaar		for ":diffput".
2854a74803eSBram Moolenaar
286071d4279SBram Moolenaar
287071d4279SBram MoolenaarWhen no [range] is given, the diff at the cursor position or just above it is
288071d4279SBram Moolenaaraffected.  When [range] is used, Vim tries to only put or get the specified
289071d4279SBram Moolenaarlines.  When there are deleted lines, this may not always be possible.
290071d4279SBram Moolenaar
291071d4279SBram MoolenaarThere can be deleted lines below the last line of the buffer.  When the cursor
292071d4279SBram Moolenaaris on the last line in the buffer and there is no diff above this line, the
293071d4279SBram Moolenaar":diffget" and "do" commands will obtain lines from the other buffer.
294071d4279SBram Moolenaar
295071d4279SBram MoolenaarTo be able to get those lines from another buffer in a [range] it's allowed to
296071d4279SBram Moolenaaruse the last line number plus one.  This command gets all diffs from the other
297071d4279SBram Moolenaarbuffer: >
298071d4279SBram Moolenaar
299071d4279SBram Moolenaar	:1,$+1diffget
300071d4279SBram Moolenaar
301071d4279SBram MoolenaarNote that deleted lines are displayed, but not counted as text lines.  You
302071d4279SBram Moolenaarcan't move the cursor into them.  To fill the deleted lines with the lines
303071d4279SBram Moolenaarfrom another buffer use ":diffget" on the line below them.
304910f66f9SBram Moolenaar								*E787*
305910f66f9SBram MoolenaarWhen the buffer that is about to be modified is read-only and the autocommand
306910f66f9SBram Moolenaarthat is triggered by |FileChangedRO| changes buffers the command will fail.
307910f66f9SBram MoolenaarThe autocommand must not change buffers.
308071d4279SBram Moolenaar
309071d4279SBram MoolenaarThe [bufspec] argument above can be a buffer number, a pattern for a buffer
310071d4279SBram Moolenaarname or a part of a buffer name.  Examples:
311071d4279SBram Moolenaar
312071d4279SBram Moolenaar	:diffget		Use the other buffer which is in diff mode
313071d4279SBram Moolenaar	:diffget 3		Use buffer 3
314071d4279SBram Moolenaar	:diffget v2		Use the buffer which matches "v2" and is in
315071d4279SBram Moolenaar				diff mode (e.g., "file.c.v2")
316071d4279SBram Moolenaar
317071d4279SBram Moolenaar==============================================================================
318071d4279SBram Moolenaar5. Diff options						*diff-options*
319071d4279SBram Moolenaar
320071d4279SBram MoolenaarAlso see |'diffopt'| and the "diff" item of |'fillchars'|.
321071d4279SBram Moolenaar
3220122c407SBram Moolenaar					    *diff-slow* *diff_translations*
3230122c407SBram MoolenaarFor very long lines, the diff syntax highlighting might be slow, especially
3240122c407SBram Moolenaarsince it tries to match all different kind of localisations. To disable
3250122c407SBram Moolenaarlocalisations and speed up the syntax highlighting, set the global variable
3260122c407SBram Moolenaarg:diff_translations to zero: >
3270122c407SBram Moolenaar
3280122c407SBram Moolenaar    let g:diff_translations = 0
3290122c407SBram Moolenaar<
33001164a65SBram MoolenaarAfter setting this variable, reload the syntax script: >
3310122c407SBram Moolenaar
3320122c407SBram Moolenaar    set syntax=diff
3330122c407SBram Moolenaar<
3340122c407SBram Moolenaar
335071d4279SBram Moolenaar
336071d4279SBram MoolenaarFINDING THE DIFFERENCES					*diff-diffexpr*
337071d4279SBram Moolenaar
338071d4279SBram MoolenaarThe 'diffexpr' option can be set to use something else than the standard
3395477506aSBram Moolenaar"diff" program to compare two files and find the differences. *E959*
340071d4279SBram Moolenaar
341071d4279SBram MoolenaarWhen 'diffexpr' is empty, Vim uses this command to find the differences
342071d4279SBram Moolenaarbetween file1 and file2: >
343071d4279SBram Moolenaar
344071d4279SBram Moolenaar	diff file1 file2 > outfile
345071d4279SBram Moolenaar
346071d4279SBram MoolenaarThe ">" is replaced with the value of 'shellredir'.
347071d4279SBram Moolenaar
348*d2ea7cf1SBram MoolenaarThe output of "diff" must be a normal "ed" style diff or a unified diff.  A
349*d2ea7cf1SBram Moolenaarcontext diff will NOT work.  For a unified diff no context lines can be used.
350*d2ea7cf1SBram MoolenaarUsing "diff -u" will NOT work, use "diff -U0".
351*d2ea7cf1SBram Moolenaar
352*d2ea7cf1SBram MoolenaarThis example explains the format that Vim expects for the "ed" style diff: >
353071d4279SBram Moolenaar
354071d4279SBram Moolenaar	1a2
355071d4279SBram Moolenaar	> bbb
356071d4279SBram Moolenaar	4d4
357071d4279SBram Moolenaar	< 111
358071d4279SBram Moolenaar	7c7
359071d4279SBram Moolenaar	< GGG
360071d4279SBram Moolenaar	---
361071d4279SBram Moolenaar	> ggg
362071d4279SBram Moolenaar
363071d4279SBram MoolenaarThe "1a2" item appends the line "bbb".
364071d4279SBram MoolenaarThe "4d4" item deletes the line "111".
3659b451250SBram MoolenaarThe "7c7" item replaces the line "GGG" with "ggg".
366071d4279SBram Moolenaar
3678f3f58f2SBram MoolenaarWhen 'diffexpr' is not empty, Vim evaluates it to obtain a diff file in the
368071d4279SBram Moolenaarformat mentioned.  These variables are set to the file names used:
369071d4279SBram Moolenaar
370071d4279SBram Moolenaar	v:fname_in		original file
371071d4279SBram Moolenaar	v:fname_new		new version of the same file
372071d4279SBram Moolenaar	v:fname_out		resulting diff file
373071d4279SBram Moolenaar
374071d4279SBram MoolenaarAdditionally, 'diffexpr' should take care of "icase" and "iwhite" in the
375071d4279SBram Moolenaar'diffopt' option.  'diffexpr' cannot change the value of 'lines' and
376071d4279SBram Moolenaar'columns'.
377071d4279SBram Moolenaar
378071d4279SBram MoolenaarExample (this does almost the same as 'diffexpr' being empty): >
379071d4279SBram Moolenaar
380071d4279SBram Moolenaar	set diffexpr=MyDiff()
381071d4279SBram Moolenaar	function MyDiff()
382071d4279SBram Moolenaar	   let opt = ""
383071d4279SBram Moolenaar	   if &diffopt =~ "icase"
384071d4279SBram Moolenaar	     let opt = opt . "-i "
385071d4279SBram Moolenaar	   endif
386071d4279SBram Moolenaar	   if &diffopt =~ "iwhite"
387071d4279SBram Moolenaar	     let opt = opt . "-b "
388071d4279SBram Moolenaar	   endif
389071d4279SBram Moolenaar	   silent execute "!diff -a --binary " . opt . v:fname_in . " " . v:fname_new .
390071d4279SBram Moolenaar		\  " > " . v:fname_out
391d473c8c1SBram Moolenaar	   redraw!
392071d4279SBram Moolenaar	endfunction
393071d4279SBram Moolenaar
394071d4279SBram MoolenaarThe "-a" argument is used to force comparing the files as text, comparing as
395071d4279SBram Moolenaarbinaries isn't useful.  The "--binary" argument makes the files read in binary
396071d4279SBram Moolenaarmode, so that a CTRL-Z doesn't end the text on DOS.
397071d4279SBram Moolenaar
398d473c8c1SBram MoolenaarThe `redraw!` command may not be needed, depending on whether executing a
399d473c8c1SBram Moolenaarshell command shows something on the display or not.
400d473c8c1SBram Moolenaar
4018f3f58f2SBram Moolenaar						*E810* *E97*
402071d4279SBram MoolenaarVim will do a test if the diff output looks alright.  If it doesn't, you will
403071d4279SBram Moolenaarget an error message.  Possible causes:
404071d4279SBram Moolenaar-  The "diff" program cannot be executed.
405071d4279SBram Moolenaar-  The "diff" program doesn't produce normal "ed" style diffs (see above).
406071d4279SBram Moolenaar-  The 'shell' and associated options are not set correctly.  Try if filtering
407071d4279SBram Moolenaar   works with a command like ":!sort".
408071d4279SBram Moolenaar-  You are using 'diffexpr' and it doesn't work.
40971fe80ddSBram MoolenaarIf it's not clear what the problem is set the 'verbose' option to one or more
41071fe80ddSBram Moolenaarto see more messages.
411071d4279SBram Moolenaar
412166af9bbSBram MoolenaarThe self-installing Vim for MS-Windows includes a diff program.  If you don't
413166af9bbSBram Moolenaarhave it you might want to download a diff.exe.  For example from
41481af9250SBram Moolenaarhttp://gnuwin32.sourceforge.net/packages/diffutils.htm.
4155eb86f91SBram Moolenaar
416071d4279SBram Moolenaar
417071d4279SBram MoolenaarUSING PATCHES					*diff-patchexpr*
418071d4279SBram Moolenaar
419071d4279SBram MoolenaarThe 'patchexpr' option can be set to use something else than the standard
420071d4279SBram Moolenaar"patch" program.
421071d4279SBram Moolenaar
422071d4279SBram MoolenaarWhen 'patchexpr' is empty, Vim will call the "patch" program like this: >
423071d4279SBram Moolenaar
424071d4279SBram Moolenaar	patch -o outfile origfile < patchfile
425071d4279SBram Moolenaar
426071d4279SBram MoolenaarThis should work fine with most versions of the "patch" program.  Note that a
427071d4279SBram MoolenaarCR in the middle of a line may cause problems, it is seen as a line break.
428071d4279SBram Moolenaar
429071d4279SBram MoolenaarIf the default doesn't work for you, set the 'patchexpr' to an expression that
430071d4279SBram Moolenaarwill have the same effect.  These variables are set to the file names used:
431071d4279SBram Moolenaar
432071d4279SBram Moolenaar	v:fname_in		original file
433071d4279SBram Moolenaar	v:fname_diff		patch file
434071d4279SBram Moolenaar	v:fname_out		resulting patched file
435071d4279SBram Moolenaar
436071d4279SBram MoolenaarExample (this does the same as 'patchexpr' being empty): >
437071d4279SBram Moolenaar
4387d47b6eeSBram Moolenaar	set patchexpr=MyPatch()
4397d47b6eeSBram Moolenaar	function MyPatch()
440071d4279SBram Moolenaar	   :call system("patch -o " . v:fname_out . " " . v:fname_in .
441071d4279SBram Moolenaar	   \  " < " . v:fname_diff)
442071d4279SBram Moolenaar	endfunction
443071d4279SBram Moolenaar
444071d4279SBram MoolenaarMake sure that using the "patch" program doesn't have unwanted side effects.
445071d4279SBram MoolenaarFor example, watch out for additionally generated files, which should be
446071d4279SBram Moolenaardeleted.  It should just patch the file and nothing else.
447071d4279SBram Moolenaar   Vim will change directory to "/tmp" or another temp directory before
448071d4279SBram Moolenaarevaluating 'patchexpr'.  This hopefully avoids that files in the current
449071d4279SBram Moolenaardirectory are accidentally patched.  Vim will also delete files starting with
450071d4279SBram Moolenaarv:fname_in and ending in ".rej" and ".orig".
451071d4279SBram Moolenaar
45291f84f6eSBram Moolenaar vim:tw=78:ts=8:noet:ft=help:norl:
453