diff.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. ==============================================================================
  105. 2. Viewing diffs *view-diffs*
  106. The effect is that the diff windows show the same text, with the differences
  107. highlighted. When scrolling the text, the 'scrollbind' option will make the
  108. text in other windows to be scrolled as well. With vertical splits the text
  109. should be aligned properly.
  110. The alignment of text will go wrong when:
  111. - 'wrap' is on, some lines will be wrapped and occupy two or more screen
  112. lines
  113. - folds are open in one window but not another
  114. - 'scrollbind' is off
  115. - changes have been made to the text
  116. - "filler" is not present in 'diffopt', deleted/inserted lines makes the
  117. alignment go wrong
  118. All the buffers edited in a window where the 'diff' option is set will join in
  119. the diff. This is also possible for hidden buffers. They must have been
  120. edited in a window first for this to be possible. To get rid of the hidden
  121. buffers use `:diffoff!`.
  122. *:DiffOrig* *diff-original-file*
  123. Since 'diff' is a window-local option, it's possible to view the same buffer
  124. in diff mode in one window and "normal" in another window. It is also
  125. possible to view the changes you have made to a buffer since the file was
  126. loaded. Since Vim doesn't allow having two buffers for the same file, you
  127. need another buffer. This command is useful: >
  128. command DiffOrig vert new | set buftype=nofile | read ++edit # | 0d_
  129. \ | diffthis | wincmd p | diffthis
  130. Use ":DiffOrig" to see the differences
  131. between the current buffer and the file it was loaded from.
  132. A buffer that is unloaded cannot be used for the diff. But it does work for
  133. hidden buffers. You can use ":hide" to close a window without unloading the
  134. buffer. If you don't want a buffer to remain used for the diff do ":set
  135. nodiff" before hiding it.
  136. *:dif* *:diff* *:diffupdate*
  137. :dif[fupdate][!] Update the diff highlighting and folds.
  138. Vim attempts to keep the differences updated when you make changes to the
  139. text. This mostly takes care of inserted and deleted lines. Changes within a
  140. line and more complicated changes do not cause the differences to be updated.
  141. To force the differences to be updated use: >
  142. :diffupdate
  143. If the ! is included Vim will check if the file was changed externally and
  144. needs to be reloaded. It will prompt for each changed file, like `:checktime`
  145. was used.
  146. Vim will show filler lines for lines that are missing in one window but are
  147. present in another. These lines were inserted in another file or deleted in
  148. this file. Removing "filler" from the 'diffopt' option will make Vim not
  149. display these filler lines.
  150. Folds are used to hide the text that wasn't changed. See |folding| for all
  151. the commands that can be used with folds.
  152. The context of lines above a difference that are not included in the fold can
  153. be set with the 'diffopt' option. For example, to set the context to three
  154. lines: >
  155. :set diffopt=filler,context:3
  156. The diffs are highlighted with these groups:
  157. |hl-DiffAdd| DiffAdd Added (inserted) lines. These lines exist in
  158. this buffer but not in another.
  159. |hl-DiffChange| DiffChange Changed lines.
  160. |hl-DiffText| DiffText Changed text inside a Changed line. Vim
  161. finds the first character that is different,
  162. and the last character that is different
  163. (searching from the end of the line). The
  164. text in between is highlighted. This means
  165. that parts in the middle that are still the
  166. same are highlighted anyway. The 'diffopt'
  167. flags "iwhite" and "icase" are used here.
  168. |hl-DiffDelete| DiffDelete Deleted lines. Also called filler lines,
  169. because they don't really exist in this
  170. buffer.
  171. ==============================================================================
  172. 3. Jumping to diffs *jumpto-diffs*
  173. Two commands can be used to jump to diffs:
  174. *[c*
  175. [c Jump backwards to the previous start of a change.
  176. When a count is used, do it that many times.
  177. *]c*
  178. ]c Jump forwards to the next start of a change.
  179. When a count is used, do it that many times.
  180. It is an error if there is no change for the cursor to move to.
  181. ==============================================================================
  182. 4. Diff copying *copy-diffs* *E99* *E100* *E101* *E102* *E103*
  183. *merge*
  184. There are two commands to copy text from one buffer to another. The result is
  185. that the buffers will be equal within the specified range.
  186. *:diffg* *:diffget*
  187. :[range]diffg[et] [bufspec]
  188. Modify the current buffer to undo difference with another
  189. buffer. If [bufspec] is given, that buffer is used. If
  190. [bufspec] refers to the current buffer then nothing happens.
  191. Otherwise this only works if there is one other buffer in diff
  192. mode.
  193. See below for [range].
  194. *:diffpu* *:diffput* *E793*
  195. :[range]diffpu[t] [bufspec]
  196. Modify another buffer to undo difference with the current
  197. buffer. Just like ":diffget" but the other buffer is modified
  198. instead of the current one.
  199. When [bufspec] is omitted and there is more than one other
  200. buffer in diff mode where 'modifiable' is set this fails.
  201. See below for [range].
  202. *do*
  203. [count]do Same as ":diffget" without range. The "o" stands for "obtain"
  204. ("dg" can't be used, it could be the start of "dgg"!). Note:
  205. this doesn't work in Visual mode.
  206. If you give a [count], it is used as the [bufspec] argument
  207. for ":diffget".
  208. *dp*
  209. [count]dp Same as ":diffput" without range. Note: this doesn't work in
  210. Visual mode.
  211. If you give a [count], it is used as the [bufspec] argument
  212. for ":diffput".
  213. When no [range] is given, the diff at the cursor position or just above it is
  214. affected. When [range] is used, Vim tries to only put or get the specified
  215. lines. When there are deleted lines, this may not always be possible.
  216. There can be deleted lines below the last line of the buffer. When the cursor
  217. is on the last line in the buffer and there is no diff above this line, the
  218. ":diffget" and "do" commands will obtain lines from the other buffer.
  219. To be able to get those lines from another buffer in a [range] it's allowed to
  220. use the last line number plus one. This command gets all diffs from the other
  221. buffer: >
  222. :1,$+1diffget
  223. Note that deleted lines are displayed, but not counted as text lines. You
  224. can't move the cursor into them. To fill the deleted lines with the lines
  225. from another buffer use ":diffget" on the line below them.
  226. *E787*
  227. When the buffer that is about to be modified is read-only and the autocommand
  228. that is triggered by |FileChangedRO| changes buffers the command will fail.
  229. The autocommand must not change buffers.
  230. The [bufspec] argument above can be a buffer number, a pattern for a buffer
  231. name or a part of a buffer name. Examples:
  232. :diffget Use the other buffer which is in diff mode
  233. :diffget 3 Use buffer 3
  234. :diffget v2 Use the buffer which matches "v2" and is in
  235. diff mode (e.g., "file.c.v2")
  236. ==============================================================================
  237. 5. Diff options *diff-options*
  238. Also see |'diffopt'| and the "diff" item of |'fillchars'|.
  239. *diff-slow* *diff_translations*
  240. For very long lines, the diff syntax highlighting might be slow, especially
  241. since it tries to match all different kind of localisations. To disable
  242. localisations and speed up the syntax highlighting, set the global variable
  243. g:diff_translations to zero: >
  244. let g:diff_translations = 0
  245. <
  246. After setting this variable, reload the syntax script: >
  247. set syntax=diff
  248. <
  249. FINDING THE DIFFERENCES *diff-diffexpr*
  250. The 'diffexpr' option can be set to use something else than the internal diff
  251. support or the standard "diff" program to compare two files and find the
  252. differences.
  253. When 'diffexpr' is empty, Vim uses this command to find the differences
  254. between file1 and file2: >
  255. diff file1 file2 > outfile
  256. The ">" is replaced with the value of 'shellredir'.
  257. The output of "diff" must be a normal "ed" style diff or a unified diff. A
  258. context diff will NOT work. For a unified diff no context lines can be used.
  259. Using "diff -u" will NOT work, use "diff -U0".
  260. This example explains the format that Vim expects for the "ed" style diff: >
  261. 1a2
  262. > bbb
  263. 4d4
  264. < 111
  265. 7c7
  266. < GGG
  267. ---
  268. > ggg
  269. The "1a2" item appends the line "bbb".
  270. The "4d4" item deletes the line "111".
  271. The "7c7" item replaces the line "GGG" with "ggg".
  272. When 'diffexpr' is not empty, Vim evaluates it to obtain a diff file in the
  273. format mentioned. These variables are set to the file names used:
  274. v:fname_in original file
  275. v:fname_new new version of the same file
  276. v:fname_out where to write the resulting diff file
  277. Additionally, 'diffexpr' should take care of "icase" and "iwhite" in the
  278. 'diffopt' option. 'diffexpr' cannot change the value of 'lines' and
  279. 'columns'.
  280. Example (this does almost the same as 'diffexpr' being empty): >
  281. set diffexpr=MyDiff()
  282. function MyDiff()
  283. let opt = ""
  284. if &diffopt =~ "icase"
  285. let opt = opt .. "-i "
  286. endif
  287. if &diffopt =~ "iwhite"
  288. let opt = opt .. "-b "
  289. endif
  290. silent execute "!diff -a --binary " .. opt .. v:fname_in .. " " .. v:fname_new ..
  291. \ " > " .. v:fname_out
  292. redraw!
  293. endfunction
  294. The "-a" argument is used to force comparing the files as text, comparing as
  295. binaries isn't useful. The "--binary" argument makes the files read in binary
  296. mode, so that a CTRL-Z doesn't end the text on DOS.
  297. The `redraw!` command may not be needed, depending on whether executing a
  298. shell command shows something on the display or not.
  299. *E810* *E97*
  300. Vim will do a test if the diff output looks alright. If it doesn't, you will
  301. get an error message. Possible causes:
  302. - The "diff" program cannot be executed.
  303. - The "diff" program doesn't produce normal "ed" style diffs (see above).
  304. - The 'shell' and associated options are not set correctly. Try if filtering
  305. works with a command like ":!sort".
  306. - You are using 'diffexpr' and it doesn't work.
  307. If it's not clear what the problem is set the 'verbose' option to one or more
  308. to see more messages.
  309. The self-installing Vim for MS-Windows includes a diff program. If you don't
  310. have it you might want to download a diff.exe. For example from
  311. https://gnuwin32.sourceforge.net/packages/diffutils.htm.
  312. USING PATCHES *diff-patchexpr*
  313. The 'patchexpr' option can be set to use something else than the standard
  314. "patch" program.
  315. When 'patchexpr' is empty, Vim will call the "patch" program like this: >
  316. patch -o outfile origfile < patchfile
  317. This should work fine with most versions of the "patch" program. Note that a
  318. CR in the middle of a line may cause problems, it is seen as a line break.
  319. If the default doesn't work for you, set the 'patchexpr' to an expression that
  320. will have the same effect. These variables are set to the file names used:
  321. v:fname_in original file
  322. v:fname_diff patch file
  323. v:fname_out resulting patched file
  324. Example (this does the same as 'patchexpr' being empty): >
  325. set patchexpr=MyPatch()
  326. function MyPatch()
  327. :call system("patch -o " .. v:fname_out .. " " .. v:fname_in ..
  328. \ " < " .. v:fname_diff)
  329. endfunction
  330. Make sure that using the "patch" program doesn't have unwanted side effects.
  331. For example, watch out for additionally generated files, which should be
  332. deleted. It should just patch the file and nothing else.
  333. Vim will change directory to "/tmp" or another temp directory before
  334. evaluating 'patchexpr'. This hopefully avoids that files in the current
  335. directory are accidentally patched. Vim will also delete files starting with
  336. v:fname_in and ending in ".rej" and ".orig".
  337. vim:tw=78:ts=8:noet:ft=help:norl: