undo.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. *undo.txt* Nvim
  2. VIM REFERENCE MANUAL by Bram Moolenaar
  3. Undo and redo *undo-redo*
  4. The basics are explained in section |02.5| of the user manual.
  5. Type |gO| to see the table of contents.
  6. ==============================================================================
  7. 1. Undo and redo commands *undo-commands*
  8. <Undo> or *undo* *<Undo>* *u*
  9. u Undo [count] changes.
  10. *:u* *:un* *:undo*
  11. :u[ndo] Undo one change.
  12. *E830*
  13. :u[ndo] {N} Jump to after change number {N}. See |undo-branches|
  14. for the meaning of {N}.
  15. :u[ndo]! Undo one change and remove it from undo history.
  16. *E5767*
  17. :u[ndo]! {N} Like ":u[ndo] {N}", but forget all changes in the
  18. current undo branch up until {N}. You may only use
  19. ":undo! {N}" to move backwards in the same undo
  20. branch, not to redo or switch to a different undo
  21. branch.
  22. *CTRL-R*
  23. CTRL-R Redo [count] changes which were undone.
  24. *:red* *:redo* *redo*
  25. :red[o] Redo one change which was undone.
  26. *U*
  27. U Undo all latest changes on one line, the line where
  28. the latest change was made. |U| itself also counts as
  29. a change, and thus |U| undoes a previous |U|.
  30. The last changes are remembered. You can use the undo and redo commands above
  31. to revert the text to how it was before each change. You can also apply the
  32. changes again, getting back the text before the undo.
  33. The "U" command is treated by undo/redo just like any other command. Thus a
  34. "u" command undoes a "U" command and a 'CTRL-R' command redoes it again. When
  35. mixing "U", "u" and 'CTRL-R' you will notice that the "U" command will
  36. restore the situation of a line to before the previous "U" command. This may
  37. be confusing. Try it out to get used to it.
  38. The "U" command will always mark the buffer as changed. When "U" changes the
  39. buffer back to how it was without changes, it is still considered changed.
  40. Use "u" to undo changes until the buffer becomes unchanged.
  41. ==============================================================================
  42. 2. Two ways of undo *undo-two-ways*
  43. How undo and redo commands work depends on the 'u' flag in 'cpoptions'.
  44. There is the Vim way ('u' excluded) and the Vi-compatible way ('u' included).
  45. In the Vim way, "uu" undoes two changes. In the Vi-compatible way, "uu" does
  46. nothing (undoes an undo).
  47. 'u' excluded, the Vim way:
  48. You can go back in time with the undo command. You can then go forward again
  49. with the redo command. If you make a new change after the undo command,
  50. the redo will not be possible anymore.
  51. 'u' included, the Vi-compatible way:
  52. The undo command undoes the previous change, and also the previous undo
  53. command. The redo command repeats the previous undo command. It does NOT
  54. repeat a change command, use "." for that.
  55. Examples Vim way Vi-compatible way ~
  56. "uu" two times undo no-op
  57. "u CTRL-R" no-op two times undo
  58. Rationale: Nvi uses the "." command instead of CTRL-R. Unfortunately, this
  59. is not Vi compatible. For example "dwdwu." in Vi deletes two
  60. words, in Nvi it does nothing.
  61. ==============================================================================
  62. 3. Undo blocks *undo-blocks*
  63. One undo command normally undoes a typed command, no matter how many changes
  64. that command makes. This sequence of undo-able changes forms an undo block.
  65. Thus if the typed key(s) call a function, all the commands in the function are
  66. undone together.
  67. If you want to write a function or script that doesn't create a new undoable
  68. change but joins in with the previous change use this command:
  69. *:undoj* *:undojoin* *E790*
  70. :undoj[oin] Join further changes with the previous undo block.
  71. Warning: Use with care, it may prevent the user from
  72. properly undoing changes. Don't use this after undo
  73. or redo.
  74. This is most useful when you need to prompt the user halfway through a change.
  75. For example in a function that calls |getchar()|. Do make sure that there was
  76. a related change before this that you must join with.
  77. This doesn't work by itself, because the next key press will start a new
  78. change again. But you can do something like this: >
  79. :undojoin | delete
  80. After this a "u" command will undo the delete command and the previous
  81. change.
  82. *undo-break* *undo-close-block*
  83. To do the opposite, use a new undo block for the next change, in Insert mode
  84. use CTRL-G u. This is useful if you want an insert command to be undoable in
  85. parts. E.g., for each sentence. |i_CTRL-G_u|
  86. Setting the value of 'undolevels' also closes the undo block. Even when the
  87. new value is equal to the old value. Use `g:undolevels` to explicitly read
  88. and write only the global value of 'undolevels'. >
  89. let &g:undolevels = &g:undolevels
  90. Note that the similar-looking assignment `let &undolevels=&undolevels` does not
  91. preserve the global option value of 'undolevels' in the event that the local
  92. option has been set to a different value. For example: >
  93. " Start with different global and local values for 'undolevels'.
  94. let &g:undolevels = 1000
  95. let &l:undolevels = 2000
  96. " This assignment changes the global option to 2000:
  97. let &undolevels = &undolevels
  98. ==============================================================================
  99. 4. Undo branches *undo-branches* *undo-tree*
  100. Above we only discussed one line of undo/redo. But it is also possible to
  101. branch off. This happens when you undo a few changes and then make a new
  102. change. The undone changes become a branch. You can go to that branch with
  103. the following commands.
  104. This is explained in the user manual: |usr_32.txt|.
  105. *:undol* *:undolist*
  106. :undol[ist] List the leafs in the tree of changes. Example:
  107. number changes when saved ~
  108. 88 88 2010/01/04 14:25:53
  109. 108 107 08/07 12:47:51
  110. 136 46 13:33:01 7
  111. 166 164 3 seconds ago
  112. The "number" column is the change number. This number
  113. continuously increases and can be used to identify a
  114. specific undo-able change, see |:undo|.
  115. The "changes" column is the number of changes to this
  116. leaf from the root of the tree.
  117. The "when" column is the date and time when this
  118. change was made. The four possible formats are:
  119. N seconds ago
  120. HH:MM:SS hour, minute, seconds
  121. MM/DD HH:MM:SS idem, with month and day
  122. YYYY/MM/DD HH:MM:SS idem, with year
  123. The "saved" column specifies, if this change was
  124. written to disk and which file write it was. This can
  125. be used with the |:later| and |:earlier| commands.
  126. For more details use the |undotree()| function.
  127. *g-*
  128. g- Go to older text state. With a count repeat that many
  129. times.
  130. *:ea* *:earlier*
  131. :ea[rlier] {count} Go to older text state {count} times.
  132. :ea[rlier] {N}s Go to older text state about {N} seconds before.
  133. :ea[rlier] {N}m Go to older text state about {N} minutes before.
  134. :ea[rlier] {N}h Go to older text state about {N} hours before.
  135. :ea[rlier] {N}d Go to older text state about {N} days before.
  136. :ea[rlier] {N}f Go to older text state {N} file writes before.
  137. When changes were made since the last write
  138. ":earlier 1f" will revert the text to the state when
  139. it was written. Otherwise it will go to the write
  140. before that.
  141. When at the state of the first file write, or when
  142. the file was not written, ":earlier 1f" will go to
  143. before the first change.
  144. *g+*
  145. g+ Go to newer text state. With a count repeat that many
  146. times.
  147. *:lat* *:later*
  148. :lat[er] {count} Go to newer text state {count} times.
  149. :lat[er] {N}s Go to newer text state about {N} seconds later.
  150. :lat[er] {N}m Go to newer text state about {N} minutes later.
  151. :lat[er] {N}h Go to newer text state about {N} hours later.
  152. :lat[er] {N}d Go to newer text state about {N} days later.
  153. :lat[er] {N}f Go to newer text state {N} file writes later.
  154. When at the state of the last file write, ":later 1f"
  155. will go to the newest text state.
  156. Note that text states will become unreachable when undo information is cleared
  157. for 'undolevels'.
  158. Don't be surprised when moving through time shows multiple changes to take
  159. place at a time. This happens when moving through the undo tree and then
  160. making a new change.
  161. EXAMPLE
  162. Start with this text:
  163. one two three ~
  164. Delete the first word by pressing "x" three times:
  165. ne two three ~
  166. e two three ~
  167. two three ~
  168. Now undo that by pressing "u" three times:
  169. e two three ~
  170. ne two three ~
  171. one two three ~
  172. Delete the second word by pressing "x" three times:
  173. one wo three ~
  174. one o three ~
  175. one three ~
  176. Now undo that by using "g-" three times:
  177. one o three ~
  178. one wo three ~
  179. two three ~
  180. You are now back in the first undo branch, after deleting "one". Repeating
  181. "g-" will now bring you back to the original text:
  182. e two three ~
  183. ne two three ~
  184. one two three ~
  185. Jump to the last change with ":later 1h":
  186. one three ~
  187. And back to the start again with ":earlier 1h":
  188. one two three ~
  189. Note that using "u" and CTRL-R will not get you to all possible text states
  190. while repeating "g-" and "g+" does.
  191. ==============================================================================
  192. 5. Undo persistence *undo-persistence* *persistent-undo*
  193. When unloading a buffer Vim normally destroys the tree of undos created for
  194. that buffer. By setting the 'undofile' option, Vim will automatically save
  195. your undo history when you write a file and restore undo history when you edit
  196. the file again.
  197. The 'undofile' option is checked after writing a file, before the BufWritePost
  198. autocommands. If you want to control what files to write undo information
  199. for, you can use a BufWritePre autocommand: >
  200. au BufWritePre /tmp/* setlocal noundofile
  201. Vim saves undo trees in a separate undo file, one for each edited file, using
  202. a simple scheme that maps filesystem paths directly to undo files. Vim will
  203. detect if an undo file is no longer synchronized with the file it was written
  204. for (with a hash of the file contents) and ignore it when the file was changed
  205. after the undo file was written, to prevent corruption. An undo file is also
  206. ignored if its owner differs from the owner of the edited file, except when
  207. the owner of the undo file is the current user. Set 'verbose' to get a
  208. message about that when opening a file.
  209. Location of the undo files is controlled by the 'undodir' option, by default
  210. they are saved to the dedicated directory in the application data folder.
  211. You can also save and restore undo histories by using ":wundo" and ":rundo"
  212. respectively:
  213. *:wundo* *:rundo*
  214. :wundo[!] {file}
  215. Write undo history to {file}.
  216. When {file} exists and it does not look like an undo file
  217. (the magic number at the start of the file is wrong), then
  218. this fails, unless the ! was added.
  219. If it exists and does look like an undo file it is
  220. overwritten. If there is no undo-history, nothing will be
  221. written.
  222. Implementation detail: Overwriting happens by first deleting
  223. the existing file and then creating a new file with the same
  224. name. So it is not possible to overwrite an existing undofile
  225. in a write-protected directory.
  226. :rundo {file} Read undo history from {file}.
  227. You can use these in autocommands to explicitly specify the name of the
  228. history file. E.g.: >
  229. au BufReadPost * call ReadUndo()
  230. au BufWritePost * call WriteUndo()
  231. func ReadUndo()
  232. if filereadable(expand('%:h') .. '/UNDO/' .. expand('%:t'))
  233. rundo %:h/UNDO/%:t
  234. endif
  235. endfunc
  236. func WriteUndo()
  237. let dirname = expand('%:h') .. '/UNDO'
  238. if !isdirectory(dirname)
  239. call mkdir(dirname)
  240. endif
  241. wundo %:h/UNDO/%:t
  242. endfunc
  243. You should keep 'undofile' off, otherwise you end up with two undo files for
  244. every write.
  245. You can use the |undofile()| function to find out the file name that Vim would
  246. use.
  247. Note that while reading/writing files and 'undofile' is set most errors will
  248. be silent, unless 'verbose' is set. With :wundo and :rundo you will get more
  249. error messages, e.g., when the file cannot be read or written.
  250. NOTE: undo files are never deleted by Vim. You need to delete them yourself.
  251. Reading an existing undo file may fail for several reasons:
  252. *E822* It cannot be opened, because the file permissions don't allow it.
  253. *E823* The magic number at the start of the file doesn't match. This usually
  254. means it is not an undo file.
  255. *E824* The version number of the undo file indicates that it's written by a
  256. newer version of Vim. You need that newer version to open it. Don't
  257. write the buffer if you want to keep the undo info in the file.
  258. "File contents changed, cannot use undo info"
  259. The file text differs from when the undo file was written. This means
  260. the undo file cannot be used, it would corrupt the text. This also
  261. happens when 'encoding' differs from when the undo file was written.
  262. *E825* The undo file does not contain valid contents and cannot be used.
  263. "Not reading undo file, owner differs"
  264. The undo file is owned by someone else than the owner of the text
  265. file. For safety the undo file is not used.
  266. Writing an undo file may fail for these reasons:
  267. *E828* The file to be written cannot be created. Perhaps you do not have
  268. write permissions in the directory.
  269. "Cannot write undo file in any directory in 'undodir'"
  270. None of the directories in 'undodir' can be used.
  271. "Will not overwrite with undo file, cannot read"
  272. A file exists with the name of the undo file to be written, but it
  273. cannot be read. You may want to delete this file or rename it.
  274. "Will not overwrite, this is not an undo file"
  275. A file exists with the name of the undo file to be written, but it
  276. does not start with the right magic number. You may want to delete
  277. this file or rename it.
  278. "Skipping undo file write, nothing to undo"
  279. There is no undo information to be written, nothing has been changed
  280. or 'undolevels' is negative.
  281. *E829* An error occurred while writing the undo file. You may want to try
  282. again.
  283. ==============================================================================
  284. 6. Remarks about undo *undo-remarks*
  285. The number of changes that are remembered is set with the 'undolevels' option.
  286. If it is zero, the Vi-compatible way is always used. If it is negative no
  287. undo is possible. Use this if you are running out of memory.
  288. *clear-undo*
  289. When you set 'undolevels' to -1 the undo information is not immediately
  290. cleared, this happens at the next change. To force clearing the undo
  291. information you can use these commands: >
  292. :let old_undolevels = &l:undolevels
  293. :setlocal undolevels=-1
  294. :exe "normal a \<BS>\<Esc>"
  295. :let &l:undolevels = old_undolevels
  296. :unlet old_undolevels
  297. Note use of `&l:undolevels` to explicitly read the local value of 'undolevels'
  298. and the use of `:setlocal` to change only the local option (which takes
  299. precedence over the corresponding global option value). Saving the option value
  300. via the use of `&undolevels` is unpredictable; it reads either the local value
  301. (if one has been set) or the global value (otherwise). Also, if a local value
  302. has been set, changing the option via `:set undolevels` will change both the
  303. global and local values, requiring extra work to save and restore both values.
  304. Marks for the buffer ('a to 'z) are also saved and restored, together with the
  305. text.
  306. When all changes have been undone, the buffer is not considered to be changed.
  307. It is then possible to exit Vim with ":q" instead of ":q!".
  308. Note that this is relative to the last write of the file. Typing "u" after
  309. ":w" actually changes the buffer, compared to what was written, so the buffer
  310. is considered changed then.
  311. When manual |folding| is being used, the folds are not saved and restored.
  312. Only changes completely within a fold will keep the fold as it was, because
  313. the first and last line of the fold don't change.
  314. The numbered registers can also be used for undoing deletes. Each time you
  315. delete text, it is put into register "1. The contents of register "1 are
  316. shifted to "2, etc. The contents of register "9 are lost. You can now get
  317. back the most recent deleted text with the put command: '"1P'. (also, if the
  318. deleted text was the result of the last delete or copy operation, 'P' or 'p'
  319. also works as this puts the contents of the unnamed register). You can get
  320. back the text of three deletes ago with '"3P'.
  321. *redo-register*
  322. If you want to get back more than one part of deleted text, you can use a
  323. special feature of the repeat command ".". It will increase the number of the
  324. register used. So if you first do '"1P', the following "." will result in a
  325. '"2P'. Repeating this will result in all numbered registers being inserted.
  326. Example: If you deleted text with 'dd....' it can be restored with
  327. '"1P....'.
  328. If you don't know in which register the deleted text is, you can use the
  329. :display command. An alternative is to try the first register with '"1P', and
  330. if it is not what you want do 'u.'. This will remove the contents of the
  331. first put, and repeat the put command for the second register. Repeat the
  332. 'u.' until you got what you want.
  333. vim:tw=78:ts=8:noet:ft=help:norl: