undo.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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: >
  88. let &undolevels = &undolevels
  89. ==============================================================================
  90. 4. Undo branches *undo-branches* *undo-tree*
  91. Above we only discussed one line of undo/redo. But it is also possible to
  92. branch off. This happens when you undo a few changes and then make a new
  93. change. The undone changes become a branch. You can go to that branch with
  94. the following commands.
  95. This is explained in the user manual: |usr_32.txt|.
  96. *:undol* *:undolist*
  97. :undol[ist] List the leafs in the tree of changes. Example:
  98. number changes when saved ~
  99. 88 88 2010/01/04 14:25:53
  100. 108 107 08/07 12:47:51
  101. 136 46 13:33:01 7
  102. 166 164 3 seconds ago
  103. The "number" column is the change number. This number
  104. continuously increases and can be used to identify a
  105. specific undo-able change, see |:undo|.
  106. The "changes" column is the number of changes to this
  107. leaf from the root of the tree.
  108. The "when" column is the date and time when this
  109. change was made. The four possible formats are:
  110. N seconds ago
  111. HH:MM:SS hour, minute, seconds
  112. MM/DD HH:MM:SS idem, with month and day
  113. YYYY/MM/DD HH:MM:SS idem, with year
  114. The "saved" column specifies, if this change was
  115. written to disk and which file write it was. This can
  116. be used with the |:later| and |:earlier| commands.
  117. For more details use the |undotree()| function.
  118. *g-*
  119. g- Go to older text state. With a count repeat that many
  120. times.
  121. *:ea* *:earlier*
  122. :earlier {count} Go to older text state {count} times.
  123. :earlier {N}s Go to older text state about {N} seconds before.
  124. :earlier {N}m Go to older text state about {N} minutes before.
  125. :earlier {N}h Go to older text state about {N} hours before.
  126. :earlier {N}d Go to older text state about {N} days before.
  127. :earlier {N}f Go to older text state {N} file writes before.
  128. When changes were made since the last write
  129. ":earlier 1f" will revert the text to the state when
  130. it was written. Otherwise it will go to the write
  131. before that.
  132. When at the state of the first file write, or when
  133. the file was not written, ":earlier 1f" will go to
  134. before the first change.
  135. *g+*
  136. g+ Go to newer text state. With a count repeat that many
  137. times.
  138. *:lat* *:later*
  139. :later {count} Go to newer text state {count} times.
  140. :later {N}s Go to newer text state about {N} seconds later.
  141. :later {N}m Go to newer text state about {N} minutes later.
  142. :later {N}h Go to newer text state about {N} hours later.
  143. :later {N}d Go to newer text state about {N} days later.
  144. :later {N}f Go to newer text state {N} file writes later.
  145. When at the state of the last file write, ":later 1f"
  146. will go to the newest text state.
  147. Note that text states will become unreachable when undo information is cleared
  148. for 'undolevels'.
  149. Don't be surprised when moving through time shows multiple changes to take
  150. place at a time. This happens when moving through the undo tree and then
  151. making a new change.
  152. EXAMPLE
  153. Start with this text:
  154. one two three ~
  155. Delete the first word by pressing "x" three times:
  156. ne two three ~
  157. e two three ~
  158. two three ~
  159. Now undo that by pressing "u" three times:
  160. e two three ~
  161. ne two three ~
  162. one two three ~
  163. Delete the second word by pressing "x" three times:
  164. one wo three ~
  165. one o three ~
  166. one three ~
  167. Now undo that by using "g-" three times:
  168. one o three ~
  169. one wo three ~
  170. two three ~
  171. You are now back in the first undo branch, after deleting "one". Repeating
  172. "g-" will now bring you back to the original text:
  173. e two three ~
  174. ne two three ~
  175. one two three ~
  176. Jump to the last change with ":later 1h":
  177. one three ~
  178. And back to the start again with ":earlier 1h":
  179. one two three ~
  180. Note that using "u" and CTRL-R will not get you to all possible text states
  181. while repeating "g-" and "g+" does.
  182. ==============================================================================
  183. 5. Undo persistence *undo-persistence* *persistent-undo*
  184. When unloading a buffer Vim normally destroys the tree of undos created for
  185. that buffer. By setting the 'undofile' option, Vim will automatically save
  186. your undo history when you write a file and restore undo history when you edit
  187. the file again.
  188. The 'undofile' option is checked after writing a file, before the BufWritePost
  189. autocommands. If you want to control what files to write undo information
  190. for, you can use a BufWritePre autocommand: >
  191. au BufWritePre /tmp/* setlocal noundofile
  192. Vim saves undo trees in a separate undo file, one for each edited file, using
  193. a simple scheme that maps filesystem paths directly to undo files. Vim will
  194. detect if an undo file is no longer synchronized with the file it was written
  195. for (with a hash of the file contents) and ignore it when the file was changed
  196. after the undo file was written, to prevent corruption. An undo file is also
  197. ignored if its owner differs from the owner of the edited file, except when
  198. the owner of the undo file is the current user. Set 'verbose' to get a
  199. message about that when opening a file.
  200. Location of the undo files is controlled by the 'undodir' option, by default
  201. they are saved to the dedicated directory in the application data folder.
  202. You can also save and restore undo histories by using ":wundo" and ":rundo"
  203. respectively:
  204. *:wundo* *:rundo*
  205. :wundo[!] {file}
  206. Write undo history to {file}.
  207. When {file} exists and it does not look like an undo file
  208. (the magic number at the start of the file is wrong), then
  209. this fails, unless the ! was added.
  210. If it exists and does look like an undo file it is
  211. overwritten. If there is no undo-history, nothing will be
  212. written.
  213. Implementation detail: Overwriting happens by first deleting
  214. the existing file and then creating a new file with the same
  215. name. So it is not possible to overwrite an existing undofile
  216. in a write-protected directory.
  217. :rundo {file} Read undo history from {file}.
  218. You can use these in autocommands to explicitly specify the name of the
  219. history file. E.g.: >
  220. au BufReadPost * call ReadUndo()
  221. au BufWritePost * call WriteUndo()
  222. func ReadUndo()
  223. if filereadable(expand('%:h') .. '/UNDO/' .. expand('%:t'))
  224. rundo %:h/UNDO/%:t
  225. endif
  226. endfunc
  227. func WriteUndo()
  228. let dirname = expand('%:h') .. '/UNDO'
  229. if !isdirectory(dirname)
  230. call mkdir(dirname)
  231. endif
  232. wundo %:h/UNDO/%:t
  233. endfunc
  234. You should keep 'undofile' off, otherwise you end up with two undo files for
  235. every write.
  236. You can use the |undofile()| function to find out the file name that Vim would
  237. use.
  238. Note that while reading/writing files and 'undofile' is set most errors will
  239. be silent, unless 'verbose' is set. With :wundo and :rundo you will get more
  240. error messages, e.g., when the file cannot be read or written.
  241. NOTE: undo files are never deleted by Vim. You need to delete them yourself.
  242. Reading an existing undo file may fail for several reasons:
  243. *E822* It cannot be opened, because the file permissions don't allow it.
  244. *E823* The magic number at the start of the file doesn't match. This usually
  245. means it is not an undo file.
  246. *E824* The version number of the undo file indicates that it's written by a
  247. newer version of Vim. You need that newer version to open it. Don't
  248. write the buffer if you want to keep the undo info in the file.
  249. "File contents changed, cannot use undo info"
  250. The file text differs from when the undo file was written. This means
  251. the undo file cannot be used, it would corrupt the text. This also
  252. happens when 'encoding' differs from when the undo file was written.
  253. *E825* The undo file does not contain valid contents and cannot be used.
  254. "Not reading undo file, owner differs"
  255. The undo file is owned by someone else than the owner of the text
  256. file. For safety the undo file is not used.
  257. Writing an undo file may fail for these reasons:
  258. *E828* The file to be written cannot be created. Perhaps you do not have
  259. write permissions in the directory.
  260. "Cannot write undo file in any directory in 'undodir'"
  261. None of the directories in 'undodir' can be used.
  262. "Will not overwrite with undo file, cannot read"
  263. A file exists with the name of the undo file to be written, but it
  264. cannot be read. You may want to delete this file or rename it.
  265. "Will not overwrite, this is not an undo file"
  266. A file exists with the name of the undo file to be written, but it
  267. does not start with the right magic number. You may want to delete
  268. this file or rename it.
  269. "Skipping undo file write, nothing to undo"
  270. There is no undo information to be written, nothing has been changed
  271. or 'undolevels' is negative.
  272. *E829* An error occurred while writing the undo file. You may want to try
  273. again.
  274. ==============================================================================
  275. 6. Remarks about undo *undo-remarks*
  276. The number of changes that are remembered is set with the 'undolevels' option.
  277. If it is zero, the Vi-compatible way is always used. If it is negative no
  278. undo is possible. Use this if you are running out of memory.
  279. *clear-undo*
  280. When you set 'undolevels' to -1 the undo information is not immediately
  281. cleared, this happens at the next change. To force clearing the undo
  282. information you can use these commands: >
  283. :let old_undolevels = &undolevels
  284. :set undolevels=-1
  285. :exe "normal a \<BS>\<Esc>"
  286. :let &undolevels = old_undolevels
  287. :unlet old_undolevels
  288. Marks for the buffer ('a to 'z) are also saved and restored, together with the
  289. text.
  290. When all changes have been undone, the buffer is not considered to be changed.
  291. It is then possible to exit Vim with ":q" instead of ":q!".
  292. Note that this is relative to the last write of the file. Typing "u" after
  293. ":w" actually changes the buffer, compared to what was written, so the buffer
  294. is considered changed then.
  295. When manual |folding| is being used, the folds are not saved and restored.
  296. Only changes completely within a fold will keep the fold as it was, because
  297. the first and last line of the fold don't change.
  298. The numbered registers can also be used for undoing deletes. Each time you
  299. delete text, it is put into register "1. The contents of register "1 are
  300. shifted to "2, etc. The contents of register "9 are lost. You can now get
  301. back the most recent deleted text with the put command: '"1P'. (also, if the
  302. deleted text was the result of the last delete or copy operation, 'P' or 'p'
  303. also works as this puts the contents of the unnamed register). You can get
  304. back the text of three deletes ago with '"3P'.
  305. *redo-register*
  306. If you want to get back more than one part of deleted text, you can use a
  307. special feature of the repeat command ".". It will increase the number of the
  308. register used. So if you first do '"1P', the following "." will result in a
  309. '"2P'. Repeating this will result in all numbered registers being inserted.
  310. Example: If you deleted text with 'dd....' it can be restored with
  311. '"1P....'.
  312. If you don't know in which register the deleted text is, you can use the
  313. :display command. An alternative is to try the first register with '"1P', and
  314. if it is not what you want do 'u.'. This will remove the contents of the
  315. first put, and repeat the put command for the second register. Repeat the
  316. 'u.' until you got what you want.
  317. vim:tw=78:ts=8:noet:ft=help:norl: