diff.txt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. *diff.txt* For Vim version 9.0. Last change: 2022 Oct 01
  2. VIM REFERENCE MANUAL by Bram Moolenaar
  3. *diff* *vimdiff* *gvimdiff* *diff-mode*
  4. This file describes the |+diff| feature: Showing differences between two to
  5. eight versions of the same file.
  6. The basics are explained in section |08.7| of the user manual.
  7. 1. Starting diff mode |start-vimdiff|
  8. 2. Viewing diffs |view-diffs|
  9. 3. Jumping to diffs |jumpto-diffs|
  10. 4. Copying diffs |copy-diffs|
  11. 5. Diff options |diff-options|
  12. ==============================================================================
  13. 1. Starting diff mode *start-vimdiff*
  14. The easiest way to start editing in diff mode is with the "vimdiff" command.
  15. This starts Vim as usual, and additionally sets up for viewing the differences
  16. between the arguments. >
  17. vimdiff file1 file2 [file3 [file4]]
  18. This is equivalent to: >
  19. vim -d file1 file2 [file3 [file4]]
  20. You may also use "gvimdiff" or "vim -d -g". The GUI is started then.
  21. You may also use "viewdiff" or "gviewdiff". Vim starts in readonly mode then.
  22. "r" may be prepended for restricted mode (see |-Z|).
  23. The second and following arguments may also be a directory name. Vim will
  24. then append the file name of the first argument to the directory name to find
  25. the file.
  26. By default an internal diff library will be used. When 'diffopt' or
  27. 'diffexpr' has been set an external "diff" command will be used. This only
  28. works when such a diff program is available.
  29. Diffs are local to the current tab page |tab-page|. You can't see diffs with
  30. a window in another tab page. This does make it possible to have several
  31. diffs at the same time, each in their own tab page.
  32. What happens is that Vim opens a window for each of the files. This is like
  33. using the |-O| argument. This uses vertical splits. If you prefer horizontal
  34. splits add the |-o| argument: >
  35. vimdiff -o file1 file2 [file3 [file4]]
  36. If you always prefer horizontal splits include "horizontal" in 'diffopt'.
  37. In each of the edited files these options are set:
  38. 'diff' on
  39. 'scrollbind' on
  40. 'cursorbind' on
  41. 'scrollopt' includes "hor"
  42. 'wrap' off, or leave as-is if 'diffopt' includes "followwrap"
  43. 'foldmethod' "diff"
  44. 'foldcolumn' value from 'diffopt', default is 2
  45. These options are set local to the window. When editing another file they are
  46. reset to the global value.
  47. The options can still be overruled from a modeline when re-editing the file.
  48. However, 'foldmethod' and 'wrap' won't be set from a modeline when 'diff' is
  49. set.
  50. See `:diffoff` for an easy way to revert the options.
  51. The differences shown are actually the differences in the buffer. Thus if you
  52. make changes after loading a file, these will be included in the displayed
  53. diffs. You might have to do ":diffupdate" now and then, not all changes are
  54. immediately taken into account, especially when using an external diff command.
  55. In your .vimrc file you could do something special when Vim was started in
  56. diff mode. You could use a construct like this: >
  57. if &diff
  58. setup for diff mode
  59. else
  60. setup for non-diff mode
  61. endif
  62. While already in Vim you can start diff mode in three ways.
  63. *E98*
  64. :diffs[plit] {filename} *:diffs* *:diffsplit*
  65. Open a new window on the file {filename}. The options are set
  66. as for "vimdiff" for the current and the newly opened window.
  67. Also see 'diffexpr'.
  68. *:difft* *:diffthis*
  69. :difft[his] Make the current window part of the diff windows. This sets
  70. the options like for "vimdiff".
  71. :diffp[atch] {patchfile} *E816* *:diffp* *:diffpatch*
  72. Use the current buffer, patch it with the diff found in
  73. {patchfile} and open a buffer on the result. The options are
  74. set as for "vimdiff".
  75. {patchfile} can be in any format that the "patch" program
  76. understands or 'patchexpr' can handle.
  77. Note that {patchfile} should only contain a diff for one file,
  78. the current file. If {patchfile} contains diffs for other
  79. files as well, the results are unpredictable. Vim changes
  80. directory to /tmp to avoid files in the current directory
  81. accidentally being patched. But it may still result in
  82. various ".rej" files to be created. And when absolute path
  83. names are present these files may get patched anyway.
  84. To make these commands use a vertical split, prepend |:vertical|. Examples: >
  85. :vert diffsplit main.c~
  86. :vert diffpatch /tmp/diff
  87. If you always prefer a vertical split include "vertical" in 'diffopt'.
  88. *E96*
  89. There can be up to eight buffers with 'diff' set.
  90. Since the option values are remembered with the buffer, you can edit another
  91. file for a moment and come back to the same file and be in diff mode again.
  92. *:diffo* *:diffoff*
  93. :diffo[ff] Switch off diff mode for the current window. Resets related
  94. options also when 'diff' was not set.
  95. :diffo[ff]! Switch off diff mode for the current window and in all windows
  96. in the current tab page where 'diff' is set. Resetting
  97. related options only happens in a window that has 'diff' set,
  98. if the current window does not have 'diff' set then no options
  99. in it are changed.
  100. Hidden buffers are also removed from the list of diff'ed
  101. buffers.
  102. The `:diffoff` command resets the relevant options to the values they had when
  103. using `:diffsplit`, `:diffpatch` , `:diffthis`. or starting Vim in diff mode.
  104. When using `:diffoff` twice the last saved values are restored.
  105. Otherwise they are set to their default value:
  106. 'diff' off
  107. 'scrollbind' off
  108. 'cursorbind' off
  109. 'scrollopt' without "hor"
  110. 'wrap' on, or leave as-is if 'diffopt' includes "followwrap"
  111. 'foldmethod' "manual"
  112. 'foldcolumn' 0
  113. ==============================================================================
  114. 2. Viewing diffs *view-diffs*
  115. The effect is that the diff windows show the same text, with the differences
  116. highlighted. When scrolling the text, the 'scrollbind' option will make the
  117. text in other windows to be scrolled as well. With vertical splits the text
  118. should be aligned properly.
  119. The alignment of text will go wrong when:
  120. - 'wrap' is on, some lines will be wrapped and occupy two or more screen
  121. lines
  122. - folds are open in one window but not another
  123. - 'scrollbind' is off
  124. - changes have been made to the text
  125. - "filler" is not present in 'diffopt', deleted/inserted lines makes the
  126. alignment go wrong
  127. All the buffers edited in a window where the 'diff' option is set will join in
  128. the diff. This is also possible for hidden buffers. They must have been
  129. edited in a window first for this to be possible. To get rid of the hidden
  130. buffers use `:diffoff!`.
  131. *:DiffOrig* *diff-original-file*
  132. Since 'diff' is a window-local option, it's possible to view the same buffer
  133. in diff mode in one window and "normal" in another window. It is also
  134. possible to view the changes you have made to a buffer since the file was
  135. loaded. Since Vim doesn't allow having two buffers for the same file, you
  136. need another buffer. This command is useful: >
  137. command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_
  138. \ | diffthis | wincmd p | diffthis
  139. (this is in |defaults.vim|). Use ":DiffOrig" to see the differences between
  140. the current buffer and the file it was loaded from.
  141. A buffer that is unloaded cannot be used for the diff. But it does work for
  142. hidden buffers. You can use ":hide" to close a window without unloading the
  143. buffer. If you don't want a buffer to remain used for the diff do ":set
  144. nodiff" before hiding it.
  145. *:dif* *:diff* *:diffupdate*
  146. :dif[fupdate][!] Update the diff highlighting and folds.
  147. Vim attempts to keep the differences updated when you make changes to the
  148. text. This mostly takes care of inserted and deleted lines. Changes within a
  149. line and more complicated changes do not cause the differences to be updated.
  150. To force the differences to be updated use: >
  151. :diffupdate
  152. If the ! is included Vim will check if the file was changed externally and
  153. needs to be reloaded. It will prompt for each changed file, like `:checktime`
  154. was used.
  155. Vim will show filler lines for lines that are missing in one window but are
  156. present in another. These lines were inserted in another file or deleted in
  157. this file. Removing "filler" from the 'diffopt' option will make Vim not
  158. display these filler lines.
  159. Folds are used to hide the text that wasn't changed. See |folding| for all
  160. the commands that can be used with folds.
  161. The context of lines above a difference that are not included in the fold can
  162. be set with the 'diffopt' option. For example, to set the context to three
  163. lines: >
  164. :set diffopt=filler,context:3
  165. The diffs are highlighted with these groups:
  166. |hl-DiffAdd| DiffAdd Added (inserted) lines. These lines exist in
  167. this buffer but not in another.
  168. |hl-DiffChange| DiffChange Changed lines.
  169. |hl-DiffText| DiffText Changed text inside a Changed line. Vim
  170. finds the first character that is different,
  171. and the last character that is different
  172. (searching from the end of the line). The
  173. text in between is highlighted. This means
  174. that parts in the middle that are still the
  175. same are highlighted anyway. The 'diffopt'
  176. flags "iwhite" and "icase" are used here.
  177. |hl-DiffDelete| DiffDelete Deleted lines. Also called filler lines,
  178. because they don't really exist in this
  179. buffer.
  180. ==============================================================================
  181. 3. Jumping to diffs *jumpto-diffs*
  182. Two commands can be used to jump to diffs:
  183. *[c*
  184. [c Jump backwards to the previous start of a change.
  185. When a count is used, do it that many times.
  186. *]c*
  187. ]c Jump forwards to the next start of a change.
  188. When a count is used, do it that many times.
  189. It is an error if there is no change for the cursor to move to.
  190. ==============================================================================
  191. 4. Diff copying *copy-diffs* *E99* *E100* *E101* *E102* *E103*
  192. *merge*
  193. There are two commands to copy text from one buffer to another. The result is
  194. that the buffers will be equal within the specified range.
  195. *:diffg* *:diffget*
  196. :[range]diffg[et] [bufspec]
  197. Modify the current buffer to undo difference with another
  198. buffer. If [bufspec] is given, that buffer is used. If
  199. [bufspec] refers to the current buffer then nothing happens.
  200. Otherwise this only works if there is one other buffer in diff
  201. mode.
  202. See below for [range].
  203. *:diffpu* *:diffput* *E793*
  204. :[range]diffpu[t] [bufspec]
  205. Modify another buffer to undo difference with the current
  206. buffer. Just like ":diffget" but the other buffer is modified
  207. instead of the current one.
  208. When [bufspec] is omitted and there is more than one other
  209. buffer in diff mode where 'modifiable' is set this fails.
  210. See below for [range].
  211. *do*
  212. [count]do Same as ":diffget" without range. The "o" stands for "obtain"
  213. ("dg" can't be used, it could be the start of "dgg"!). Note:
  214. this doesn't work in Visual mode.
  215. If you give a [count], it is used as the [bufspec] argument
  216. for ":diffget".
  217. *dp*
  218. [count]dp Same as ":diffput" without range. Note: this doesn't work in
  219. Visual mode.
  220. If you give a [count], it is used as the [bufspec] argument
  221. for ":diffput".
  222. When no [range] is given, the diff at the cursor position or just above it is
  223. affected. When [range] is used, Vim tries to only put or get the specified
  224. lines. When there are deleted lines, this may not always be possible.
  225. There can be deleted lines below the last line of the buffer. When the cursor
  226. is on the last line in the buffer and there is no diff above this line, the
  227. ":diffget" and "do" commands will obtain lines from the other buffer.
  228. To be able to get those lines from another buffer in a [range] it's allowed to
  229. use the last line number plus one. This command gets all diffs from the other
  230. buffer: >
  231. :1,$+1diffget
  232. Note that deleted lines are displayed, but not counted as text lines. You
  233. can't move the cursor into them. To fill the deleted lines with the lines
  234. from another buffer use ":diffget" on the line below them.
  235. *E787*
  236. When the buffer that is about to be modified is read-only and the autocommand
  237. that is triggered by |FileChangedRO| changes buffers the command will fail.
  238. The autocommand must not change buffers.
  239. The [bufspec] argument above can be a buffer number, a pattern for a buffer
  240. name or a part of a buffer name. Examples:
  241. :diffget Use the other buffer which is in diff mode
  242. :diffget 3 Use buffer 3
  243. :diffget v2 Use the buffer which matches "v2" and is in
  244. diff mode (e.g., "file.c.v2")
  245. ==============================================================================
  246. 5. Diff options *diff-options*
  247. Also see |'diffopt'| and the "diff" item of |'fillchars'|.
  248. *diff-slow* *diff_translations*
  249. For very long lines, the diff syntax highlighting might be slow, especially
  250. since it tries to match all different kind of localisations. To disable
  251. localisations and speed up the syntax highlighting, set the global variable
  252. g:diff_translations to zero: >
  253. let g:diff_translations = 0
  254. <
  255. After setting this variable, reload the syntax script: >
  256. set syntax=diff
  257. <
  258. FINDING THE DIFFERENCES *diff-diffexpr*
  259. The 'diffexpr' option can be set to use something else than the internal diff
  260. support or the standard "diff" program to compare two files and find the
  261. differences. *E959*
  262. When 'diffexpr' is empty, Vim uses this command to find the differences
  263. between file1 and file2: >
  264. diff file1 file2 > outfile
  265. The ">" is replaced with the value of 'shellredir'.
  266. The output of "diff" must be a normal "ed" style diff or a unified diff. A
  267. context diff will NOT work. For a unified diff no context lines can be used.
  268. Using "diff -u" will NOT work, use "diff -U0".
  269. This example explains the format that Vim expects for the "ed" style diff: >
  270. 1a2
  271. > bbb
  272. 4d4
  273. < 111
  274. 7c7
  275. < GGG
  276. ---
  277. > ggg
  278. The "1a2" item appends the line "bbb".
  279. The "4d4" item deletes the line "111".
  280. The "7c7" item replaces the line "GGG" with "ggg".
  281. When 'diffexpr' is not empty, Vim evaluates it to obtain a diff file in the
  282. format mentioned. These variables are set to the file names used:
  283. v:fname_in original file
  284. v:fname_new new version of the same file
  285. v:fname_out where to write the resulting diff file
  286. Additionally, 'diffexpr' should take care of "icase" and "iwhite" in the
  287. 'diffopt' option. 'diffexpr' cannot change the value of 'lines' and
  288. 'columns'.
  289. The advantage of using a function call without arguments is that it is faster,
  290. see |expr-option-function|.
  291. Example (this does almost the same as 'diffexpr' being empty): >
  292. set diffexpr=MyDiff()
  293. function MyDiff()
  294. let opt = ""
  295. if &diffopt =~ "icase"
  296. let opt = opt .. "-i "
  297. endif
  298. if &diffopt =~ "iwhite"
  299. let opt = opt .. "-b "
  300. endif
  301. silent execute "!diff -a --binary " .. opt .. v:fname_in .. " " .. v:fname_new ..
  302. \ " > " .. v:fname_out
  303. redraw!
  304. endfunction
  305. The "-a" argument is used to force comparing the files as text, comparing as
  306. binaries isn't useful. The "--binary" argument makes the files read in binary
  307. mode, so that a CTRL-Z doesn't end the text on DOS.
  308. The `redraw!` command may not be needed, depending on whether executing a
  309. shell command shows something on the display or not.
  310. If the 'diffexpr' expression starts with s: or |<SID>|, then it is replaced
  311. with the script ID (|local-function|). Example: >
  312. set diffexpr=s:MyDiffExpr()
  313. set diffexpr=<SID>SomeDiffExpr()
  314. Otherwise, the expression is evaluated in the context of the script where the
  315. option was set, thus script-local items are available.
  316. *E810* *E97*
  317. Vim will do a test if the diff output looks alright. If it doesn't, you will
  318. get an error message. Possible causes:
  319. - The "diff" program cannot be executed.
  320. - The "diff" program doesn't produce normal "ed" style diffs (see above).
  321. - The 'shell' and associated options are not set correctly. Try if filtering
  322. works with a command like ":!sort".
  323. - You are using 'diffexpr' and it doesn't work.
  324. If it's not clear what the problem is set the 'verbose' option to one or more
  325. to see more messages.
  326. The self-installing Vim for MS-Windows includes a diff program. If you don't
  327. have it you might want to download a diff.exe. For example from
  328. http://gnuwin32.sourceforge.net/packages/diffutils.htm.
  329. USING PATCHES *diff-patchexpr*
  330. The 'patchexpr' option can be set to use something else than the standard
  331. "patch" program.
  332. When 'patchexpr' is empty, Vim will call the "patch" program like this: >
  333. patch -o outfile origfile < patchfile
  334. This should work fine with most versions of the "patch" program. Note that a
  335. CR in the middle of a line may cause problems, it is seen as a line break.
  336. If the default doesn't work for you, set the 'patchexpr' to an expression that
  337. will have the same effect. These variables are set to the file names used:
  338. v:fname_in original file
  339. v:fname_diff patch file
  340. v:fname_out resulting patched file
  341. The advantage of using a function call without arguments is that it is faster,
  342. see |expr-option-function|.
  343. Example (this does the same as 'patchexpr' being empty): >
  344. set patchexpr=MyPatch()
  345. function MyPatch()
  346. :call system("patch -o " .. v:fname_out .. " " .. v:fname_in ..
  347. \ " < " .. v:fname_diff)
  348. endfunction
  349. Make sure that using the "patch" program doesn't have unwanted side effects.
  350. For example, watch out for additionally generated files, which should be
  351. deleted. It should just patch the file and nothing else.
  352. Vim will change directory to "/tmp" or another temp directory before
  353. evaluating 'patchexpr'. This hopefully avoids that files in the current
  354. directory are accidentally patched. Vim will also delete files starting with
  355. v:fname_in and ending in ".rej" and ".orig".
  356. If the 'patchexpr' expression starts with s: or |<SID>|, then it is replaced
  357. with the script ID (|local-function|). Example: >
  358. set patchexpr=s:MyPatchExpr()
  359. set patchexpr=<SID>SomePatchExpr()
  360. Otherwise, the expression is evaluated in the context of the script where the
  361. option was set, thus script-local items are available.
  362. vim:tw=78:ts=8:noet:ft=help:norl: