blob: 8a6c7afde6c892028a7c6ca5f612b4444ff74c00 [file] [log] [blame]
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +00001*diff.txt* For Vim version 7.0aa. Last change: 2006 Mar 14
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7 *diff* *vimdiff* *gvimdiff* *diff-mode*
8This file describes the +diff feature: Showing differences between two or
9three versions of the same file.
10
11The basics are explained in section |08.7| of the user manual.
12
131. Starting diff mode |vimdiff|
142. Viewing diffs |view-diffs|
153. Jumping to diffs |jumpto-diffs|
164. Copying diffs |copy-diffs|
175. Diff options |diff-options|
18
19{not in Vi}
20
21==============================================================================
221. Starting diff mode
23
24The easiest way to start editing in diff mode is with the "vimdiff" command.
25This starts Vim as usual, and additionally sets up for viewing the differences
26between the arguments. >
27
28 vimdiff file1 file2 [file3 [file4]]
29
30This is equivalent to: >
31
32 vim -d file1 file2 [file3 [file4]]
33
34You may also use "gvimdiff" or "vim -d -g". The GUI is started then.
35You may also use "viewdiff" or "gviewdiff". Vim starts in readonly mode then.
36"r" may be prepended for restricted mode (see |-Z|).
37
38The second and following arguments may also be a directory name. Vim will
39then append the file name of the first argument to the directory name to find
40the file.
41
42This only works when a standard "diff" command is available. See 'diffexpr'.
43
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000044Diffs are local to the current tab page |tab-page|. You can't see diffs with
45a window in another tab page. This does make it possible to have several
46diffs at the same time, each in their own tab page.
47
Bram Moolenaar071d4272004-06-13 20:20:40 +000048What happens is that Vim opens a window for each of the files. This is like
49using the |-O| argument. This uses vertical splits. If you prefer horizontal
50splits add the |-o| argument: >
51
52 vimdiff -o file1 file2 [file3]
53
54In each of the edited files these options are set:
55
56 'diff' on
57 'scrollbind' on
58 'scrollopt' includes "hor"
59 'wrap' off
60 'foldmethod' "diff"
61 'foldcolumn' 2
62
63These options are set local to the window. When editing another file they are
64reset to the global value.
65
66The differences shown are actually the differences in the buffer. Thus if you
67make changes after loading a file, these will be included in the displayed
68diffs. You might have to do ":diffupdate" now and then, not all changes are
69immediately taken into account.
70
71In your .vimrc file you could do something special when Vim was started in
72diff mode. You could use a construct like this: >
73
74 if &diff
75 setup for diff mode
76 else
77 setup for non-diff mode
78 endif
79
80While already in Vim you can start diff mode in three ways.
81
82 *E98*
83:diffsplit {filename} *:diffs* *:diffsplit*
84 Open a new window on the file {filename}. The options are set
85 as for "vimdiff" for the current and the newly opened window.
86 Also see 'diffexpr'.
87
88 *:difft* *:diffthis*
89:diffthis Make the current window part of the diff windows. This sets
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000090 the options like for "vimdiff".
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
92:diffpatch {patchfile} *:diffp* *:diffpatch*
93 Use the current buffer, patch it with the diff found in
94 {patchfile} and open a buffer on the result. The options are
95 set as for "vimdiff".
96 {patchfile} can be in any format that the "patch" program
97 understands or 'patchexpr' can handle.
98 Note that {patchfile} should only contain a diff for one file,
99 the current file. If {patchfile} contains diffs for other
100 files as well, the results are unpredictable. Vim changes
101 directory to /tmp to avoid files in the current directory
102 accidentally being patched. But it may still result in
103 various ".rej" files to be created. And when absolute path
104 names are present these files may get patched anyway.
105
106To make these commands use a vertical split, prepend |:vertical|. Examples: >
107
108 :vert diffsplit main.c~
109 :vert diffpatch /tmp/diff
110<
111 *E96*
112There can be up to four buffers with 'diff' set.
113
114Since the option values are remembered with the buffer, you can edit another
115file for a moment and come back to the same file and be in diff mode again.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000117 *:diffo* *:diffoff*
118:diffoff Switch off diff mode for the current window.
119
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000120:diffoff! Switch off diff mode for all windows in the current tab page.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000121
122The ":diffoff" command resets the relevant options to their default value.
123This may be different from what the values were before diff mode was started,
124the old values are not remembered.
125
126 'diff' off
127 'scrollbind' off
128 'scrollopt' without "hor"
129 'wrap' on
130 'foldmethod' "manual"
131 'foldcolumn' 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
133==============================================================================
1342. Viewing diffs *view-diffs*
135
136The effect is that the diff windows show the same text, with the differences
137highlighted. When scrolling the text, the 'scrollbind' option will make the
138text in other windows to be scrolled as well. With vertical splits the text
139should be aligned properly.
140
141The alignment of text will go wrong when:
142- 'wrap' is on, some lines will be wrapped and occupy two or more screen
143 lines
144- folds are open in one window but not another
145- 'scrollbind' is off
146- changes have been made to the text
147- "filler" is not present in 'diffopt', deleted/inserted lines makes the
148 alignment go wrong
149
150All the buffers edited in a window where the 'diff' option is set will join in
151the diff. This is also possible for hidden buffers. They must have been
152edited in a window first for this to be possible.
153
154Since 'diff' is a window-local option, it's possible to view the same buffer
155in diff mode in one window and "normal" in another window. It is also
156possible to view the changes you have made to a buffer, but since Vim doesn't
157allow having two buffers for the same file, you need to make a copy of the
158original file and diff with that. For example: >
159 :!cp % tempfile
160 :diffsplit tempfile
161
162A buffer that is unloaded cannot be used for the diff. But it does work for
163hidden buffers. You can use ":hide" to close a window without unloading the
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000164buffer. If you don't want a buffer to remain used for the diff do ":set
165nodiff" before hiding it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166
167 *:diffu* *:diffupdate*
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000168:diffu[pdate] Update the diff highlighting and folds.
169
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170Vim attempts to keep the differences updated when you make changes to the
171text. This mostly takes care of inserted and deleted lines. Changes within a
172line and more complicated changes do not cause the differences to be updated.
173To force the differences to be updated use: >
174
175 :diffupdate
176
177
178Vim will show filler lines for lines that are missing in one window but are
179present in another. These lines were inserted in another file or deleted in
180this file. Removing "filler" from the 'diffopt' option will make Vim not
181display these filler lines.
182
183
184Folds are used to hide the text that wasn't changed. See |folding| for all
185the commands that can be used with folds.
186
187The context of lines above a difference that are not included in the fold can
188be set with the 'diffopt' option. For example, to set the context to three
189lines: >
190
191 :set diffopt=filler,context:3
192
193
194The diffs are highlighted with these groups:
195
196|hl-DiffAdd| DiffAdd Added (inserted) lines. These lines exist in
197 this buffer but not in another.
198|hl-DiffChange| DiffChange Changed lines.
199|hl-DiffText| DiffText Changed text inside a Changed line. Vim
200 finds the first character that is different,
201 and the last character that is different
202 (searching from the end of the line). The
203 text in between is highlighted. This means
204 that parts in the middle that are still the
205 same are highlighted anyway.
206|hl-DiffDelete| DiffDelete Deleted lines. Also called filler lines,
207 because they don't really exist in this
208 buffer.
209
210==============================================================================
2113. Jumping to diffs *jumpto-diffs*
212
213Two commands can be used to jump to diffs:
214 *[c*
215 [c Jump backwards to the previous start of a change.
216 When a count is used, do it that many times.
217 *]c*
218 ]c Jump forwards to the next start of a change.
219 When a count is used, do it that many times.
220
221It is an error if there is no change for the cursor to move to.
222
223==============================================================================
2244. Diff copying *copy-diffs* *E99* *E100* *E101* *E102* *E103*
Bram Moolenaar1e015462005-09-25 22:16:38 +0000225 *merge*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226There are two commands to copy text from one buffer to another. The result is
227that the buffers will be equal within the specified range.
228
229 *:diffg* *:diffget*
230:[range]diffg[et] [bufspec]
231 Modify the current buffer to undo difference with another
232 buffer. If [bufspec] is given, that buffer is used.
233 Otherwise this only works if there is one other buffer in diff
234 mode.
235 See below for [range].
236
237 *:diffpu* *:diffput*
238:[range]diffpu[t] [bufspec]
239 Modify another buffer to undo difference with the current
240 buffer. Just like ":diffget" but the other buffer is modified
241 instead of the current one.
Bram Moolenaar1e015462005-09-25 22:16:38 +0000242 When [bufspec] is omitted and there is more than one other
243 buffer in diff mode where 'modifiable' is set this fails.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 See below for [range].
245
246 *do*
247do Same as ":diffget" without argument or range. The "o" stands
248 for "obtain" ("dg" can't be used, it could be the start of
249 "dgg"!).
250
251 *dp*
252dp Same as ":diffput" without argument or range.
253
254When no [range] is given, the diff at the cursor position or just above it is
255affected. When [range] is used, Vim tries to only put or get the specified
256lines. When there are deleted lines, this may not always be possible.
257
258There can be deleted lines below the last line of the buffer. When the cursor
259is on the last line in the buffer and there is no diff above this line, the
260":diffget" and "do" commands will obtain lines from the other buffer.
261
262To be able to get those lines from another buffer in a [range] it's allowed to
263use the last line number plus one. This command gets all diffs from the other
264buffer: >
265
266 :1,$+1diffget
267
268Note that deleted lines are displayed, but not counted as text lines. You
269can't move the cursor into them. To fill the deleted lines with the lines
270from another buffer use ":diffget" on the line below them.
271
272The [bufspec] argument above can be a buffer number, a pattern for a buffer
273name or a part of a buffer name. Examples:
274
275 :diffget Use the other buffer which is in diff mode
276 :diffget 3 Use buffer 3
277 :diffget v2 Use the buffer which matches "v2" and is in
278 diff mode (e.g., "file.c.v2")
279
280==============================================================================
2815. Diff options *diff-options*
282
283Also see |'diffopt'| and the "diff" item of |'fillchars'|.
284
285
286FINDING THE DIFFERENCES *diff-diffexpr*
287
288The 'diffexpr' option can be set to use something else than the standard
289"diff" program to compare two files and find the differences.
290
291When 'diffexpr' is empty, Vim uses this command to find the differences
292between file1 and file2: >
293
294 diff file1 file2 > outfile
295
296The ">" is replaced with the value of 'shellredir'.
297
298The output of "diff" must be a normal "ed" style diff. Do NOT use a context
299diff. This example explains the format that Vim expects: >
300
301 1a2
302 > bbb
303 4d4
304 < 111
305 7c7
306 < GGG
307 ---
308 > ggg
309
310The "1a2" item appends the line "bbb".
311The "4d4" item deletes the line "111".
312The '7c7" item replaces the line "GGG" with "ggg".
313
314When 'diffexpr' is not empty, Vim evaluates to obtain a diff file in the
315format mentioned. These variables are set to the file names used:
316
317 v:fname_in original file
318 v:fname_new new version of the same file
319 v:fname_out resulting diff file
320
321Additionally, 'diffexpr' should take care of "icase" and "iwhite" in the
322'diffopt' option. 'diffexpr' cannot change the value of 'lines' and
323'columns'.
324
325Example (this does almost the same as 'diffexpr' being empty): >
326
327 set diffexpr=MyDiff()
328 function MyDiff()
329 let opt = ""
330 if &diffopt =~ "icase"
331 let opt = opt . "-i "
332 endif
333 if &diffopt =~ "iwhite"
334 let opt = opt . "-b "
335 endif
336 silent execute "!diff -a --binary " . opt . v:fname_in . " " . v:fname_new .
337 \ " > " . v:fname_out
338 endfunction
339
340The "-a" argument is used to force comparing the files as text, comparing as
341binaries isn't useful. The "--binary" argument makes the files read in binary
342mode, so that a CTRL-Z doesn't end the text on DOS.
343
344 *E97*
345Vim will do a test if the diff output looks alright. If it doesn't, you will
346get an error message. Possible causes:
347- The "diff" program cannot be executed.
348- The "diff" program doesn't produce normal "ed" style diffs (see above).
349- The 'shell' and associated options are not set correctly. Try if filtering
350 works with a command like ":!sort".
351- You are using 'diffexpr' and it doesn't work.
Bram Moolenaar71fe80d2006-01-22 23:25:56 +0000352If it's not clear what the problem is set the 'verbose' option to one or more
353to see more messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000355The self-installing Vim includes a diff program. If you don't have it you
356might want to download a diff.exe. For example from
357http://jlb.twu.net/code/unixkit.php.
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
360USING PATCHES *diff-patchexpr*
361
362The 'patchexpr' option can be set to use something else than the standard
363"patch" program.
364
365When 'patchexpr' is empty, Vim will call the "patch" program like this: >
366
367 patch -o outfile origfile < patchfile
368
369This should work fine with most versions of the "patch" program. Note that a
370CR in the middle of a line may cause problems, it is seen as a line break.
371
372If the default doesn't work for you, set the 'patchexpr' to an expression that
373will have the same effect. These variables are set to the file names used:
374
375 v:fname_in original file
376 v:fname_diff patch file
377 v:fname_out resulting patched file
378
379Example (this does the same as 'patchexpr' being empty): >
380
Bram Moolenaar1f4d4de2006-03-14 23:00:46 +0000381 let patchexpr=MyPatch()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 function MyPatch
383 :call system("patch -o " . v:fname_out . " " . v:fname_in .
384 \ " < " . v:fname_diff)
385 endfunction
386
387Make sure that using the "patch" program doesn't have unwanted side effects.
388For example, watch out for additionally generated files, which should be
389deleted. It should just patch the file and nothing else.
390 Vim will change directory to "/tmp" or another temp directory before
391evaluating 'patchexpr'. This hopefully avoids that files in the current
392directory are accidentally patched. Vim will also delete files starting with
393v:fname_in and ending in ".rej" and ".orig".
394
395 vim:tw=78:ts=8:ft=help:norl: