diff.txt 17 KB

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