Yochem van Rosmalen | 2090405 | 2025-06-03 20:54:33 +0200 | [diff] [blame] | 1 | *usr_11.txt* For Vim version 9.1. Last change: 2025 Jun 03 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | VIM USER MANUAL - by Bram Moolenaar |
| 4 | |
| 5 | Recovering from a crash |
| 6 | |
| 7 | |
| 8 | Did your computer crash? And you just spent hours editing? Don't panic! Vim |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 9 | stores enough information to be able to restore most of your work. This |
| 10 | chapter shows you how to get your work back and explains how the swap file is |
| 11 | used. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 12 | |
| 13 | |11.1| Basic recovery |
| 14 | |11.2| Where is the swap file? |
| 15 | |11.3| Crashed or not? |
| 16 | |11.4| Further reading |
| 17 | |
| 18 | Next chapter: |usr_12.txt| Clever tricks |
| 19 | Previous chapter: |usr_10.txt| Making big changes |
| 20 | Table of contents: |usr_toc.txt| |
| 21 | |
| 22 | ============================================================================== |
| 23 | *11.1* Basic recovery |
| 24 | |
| 25 | In most cases recovering a file is quite simple, assuming you know which file |
| 26 | you were editing (and the harddisk is still working). Start Vim on the file, |
| 27 | with the "-r" argument added: > |
| 28 | |
| 29 | vim -r help.txt |
| 30 | |
| 31 | Vim will read the swap file (used to store text you were editing) and may read |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 32 | bits and pieces of the original file. If Vim recovered your changes you will |
| 33 | see these messages (with different file names, of course): |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 34 | |
| 35 | Using swap file ".help.txt.swp" ~ |
| 36 | Original file "~/vim/runtime/doc/help.txt" ~ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 37 | Recovery completed. You should check if everything is OK. ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | (You might want to write out this file under another name ~ |
| 39 | and run diff with the original file to check for changes) ~ |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 40 | You may want to delete the .swp file now. ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 41 | |
| 42 | To be on the safe side, write this file under another name: > |
| 43 | |
| 44 | :write help.txt.recovered |
| 45 | |
| 46 | Compare the file with the original file to check if you ended up with what you |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 47 | expected. Vimdiff is very useful for this |08.7|. For example: > |
| 48 | |
| 49 | :write help.txt.recovered |
| 50 | :edit # |
| 51 | :diffsp help.txt |
| 52 | |
| 53 | Watch out for the original file to contain a more recent version (you saved |
| 54 | the file just before the computer crashed). And check that no lines are |
| 55 | missing (something went wrong that Vim could not recover). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 56 | If Vim produces warning messages when recovering, read them carefully. |
| 57 | This is rare though. |
| 58 | |
Bram Moolenaar | fc2d5bd | 2010-05-15 17:06:53 +0200 | [diff] [blame] | 59 | If the recovery resulted in text that is exactly the same as the file |
| 60 | contents, you will get this message: |
| 61 | |
| 62 | Using swap file ".help.txt.swp" ~ |
| 63 | Original file "~/vim/runtime/doc/help.txt" ~ |
| 64 | Recovery completed. Buffer contents equals file contents. ~ |
| 65 | You may want to delete the .swp file now. ~ |
| 66 | |
| 67 | This usually happens if you already recovered your changes, or you wrote the |
| 68 | file after making changes. It is safe to delete the swap file now. |
| 69 | |
| 70 | It is normal that the last few changes can not be recovered. Vim flushes the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 71 | changes to disk when you don't type for about four seconds, or after typing |
| 72 | about two hundred characters. This is set with the 'updatetime' and |
| 73 | 'updatecount' options. Thus when Vim didn't get a chance to save itself when |
| 74 | the system went down, the changes after the last flush will be lost. |
| 75 | |
| 76 | If you were editing without a file name, give an empty string as argument: > |
| 77 | |
| 78 | vim -r "" |
| 79 | |
| 80 | You must be in the right directory, otherwise Vim can't find the swap file. |
| 81 | |
| 82 | ============================================================================== |
| 83 | *11.2* Where is the swap file? |
| 84 | |
| 85 | Vim can store the swap file in several places. Normally it is in the same |
| 86 | directory as the original file. To find it, change to the directory of the |
| 87 | file, and use: > |
| 88 | |
| 89 | vim -r |
| 90 | |
| 91 | Vim will list the swap files that it can find. It will also look in other |
| 92 | directories where the swap file for files in the current directory may be |
| 93 | located. It will not find swap files in any other directories though, it |
| 94 | doesn't search the directory tree. |
| 95 | The output could look like this: |
| 96 | |
| 97 | Swap files found: ~ |
| 98 | In current directory: ~ |
| 99 | 1. .main.c.swp ~ |
| 100 | owned by: mool dated: Tue May 29 21:00:25 2001 ~ |
| 101 | file name: ~mool/vim/vim6/src/main.c ~ |
| 102 | modified: YES ~ |
| 103 | user name: mool host name: masaka.moolenaar.net ~ |
| 104 | process ID: 12525 ~ |
| 105 | In directory ~/tmp: ~ |
| 106 | -- none -- ~ |
| 107 | In directory /var/tmp: ~ |
| 108 | -- none -- ~ |
| 109 | In directory /tmp: ~ |
| 110 | -- none -- ~ |
| 111 | |
| 112 | If there are several swap files that look like they may be the one you want to |
| 113 | use, a list is given of these swap files and you are requested to enter the |
| 114 | number of the one you want to use. Carefully look at the dates to decide |
| 115 | which one you want to use. |
| 116 | In case you don't know which one to use, just try them one by one and check |
| 117 | the resulting files if they are what you expected. |
| 118 | |
| 119 | |
| 120 | USING A SPECIFIC SWAP FILE |
| 121 | |
| 122 | If you know which swap file needs to be used, you can recover by giving the |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 123 | swap file name. Vim will then find out the name of the original file from |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 124 | the swap file. |
| 125 | |
| 126 | Example: > |
| 127 | vim -r .help.txt.swo |
| 128 | |
| 129 | This is also handy when the swap file is in another directory than expected. |
Yochem van Rosmalen | 2090405 | 2025-06-03 20:54:33 +0200 | [diff] [blame] | 130 | Vim recognizes files with the pattern "*.s[uvw][a-z]" as swap files. |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 131 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 132 | If this still does not work, see what file names Vim reports and rename the |
| 133 | files accordingly. Check the 'directory' option to see where Vim may have |
| 134 | put the swap file. |
| 135 | |
| 136 | Note: |
| 137 | Vim tries to find the swap file by searching the directories in the |
| 138 | 'dir' option, looking for files that match "filename.sw?". If |
| 139 | wildcard expansion doesn't work (e.g., when the 'shell' option is |
| 140 | invalid), Vim does a desperate try to find the file "filename.swp". |
| 141 | If that fails too, you will have to give the name of the swapfile |
| 142 | itself to be able to recover the file. |
| 143 | |
| 144 | ============================================================================== |
| 145 | *11.3* Crashed or not? *ATTENTION* *E325* |
| 146 | |
| 147 | Vim tries to protect you from doing stupid things. Suppose you innocently |
| 148 | start editing a file, expecting the contents of the file to show up. Instead, |
| 149 | Vim produces a very long message: |
| 150 | |
| 151 | E325: ATTENTION ~ |
| 152 | Found a swap file by the name ".main.c.swp" ~ |
| 153 | owned by: mool dated: Tue May 29 21:09:28 2001 ~ |
| 154 | file name: ~mool/vim/vim6/src/main.c ~ |
| 155 | modified: no ~ |
| 156 | user name: mool host name: masaka.moolenaar.net ~ |
| 157 | process ID: 12559 (still running) ~ |
| 158 | While opening file "main.c" ~ |
| 159 | dated: Tue May 29 19:46:12 2001 ~ |
| 160 | ~ |
| 161 | (1) Another program may be editing the same file. ~ |
| 162 | If this is the case, be careful not to end up with two ~ |
| 163 | different instances of the same file when making changes. ~ |
| 164 | Quit, or continue with caution. ~ |
| 165 | ~ |
| 166 | (2) An edit session for this file crashed. ~ |
| 167 | If this is the case, use ":recover" or "vim -r main.c" ~ |
| 168 | to recover the changes (see ":help recovery"). ~ |
| 169 | If you did this already, delete the swap file ".main.c.swp" ~ |
| 170 | to avoid this message. ~ |
| 171 | |
| 172 | You get this message, because, when starting to edit a file, Vim checks if a |
| 173 | swap file already exists for that file. If there is one, there must be |
| 174 | something wrong. It may be one of these two situations. |
| 175 | |
| 176 | 1. Another edit session is active on this file. Look in the message for the |
| 177 | line with "process ID". It might look like this: |
| 178 | |
| 179 | process ID: 12559 (still running) ~ |
| 180 | |
| 181 | The text "(still running)" indicates that the process editing this file |
| 182 | runs on the same computer. When working on a non-Unix system you will not |
| 183 | get this extra hint. When editing a file over a network, you may not see |
| 184 | the hint, because the process might be running on another computer. In |
| 185 | those two cases you must find out what the situation is yourself. |
| 186 | If there is another Vim editing the same file, continuing to edit will |
| 187 | result in two versions of the same file. The one that is written last will |
| 188 | overwrite the other one, resulting in loss of changes. You better quit |
| 189 | this Vim. |
| 190 | |
| 191 | 2. The swap file might be the result from a previous crash of Vim or the |
| 192 | computer. Check the dates mentioned in the message. If the date of the |
| 193 | swap file is newer than the file you were editing, and this line appears: |
| 194 | |
| 195 | modified: YES ~ |
| 196 | |
| 197 | Then you very likely have a crashed edit session that is worth recovering. |
| 198 | If the date of the file is newer than the date of the swap file, then |
| 199 | either it was changed after the crash (perhaps you recovered it earlier, |
| 200 | but didn't delete the swap file?), or else the file was saved before the |
| 201 | crash but after the last write of the swap file (then you're lucky: you |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 202 | don't even need that old swap file). Vim will warn you for this with this |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 203 | extra line: |
| 204 | |
| 205 | NEWER than swap file! ~ |
| 206 | |
| 207 | |
Bram Moolenaar | 67cf86b | 2019-04-28 22:25:38 +0200 | [diff] [blame] | 208 | NOTE that in the following situation Vim knows the swap file is not useful and |
| 209 | will automatically delete it: |
| 210 | - The file is a valid swap file (Magic number is correct). |
| 211 | - The flag that the file was modified is not set. |
| 212 | - The process is not running. |
| 213 | |
Bram Moolenaar | 73fef33 | 2020-06-21 22:12:03 +0200 | [diff] [blame] | 214 | You can programmatically deal with this situation with the |FileChangedShell| |
Bram Moolenaar | ade0d39 | 2020-01-21 22:33:58 +0100 | [diff] [blame] | 215 | autocommand event. |
Bram Moolenaar | 67cf86b | 2019-04-28 22:25:38 +0200 | [diff] [blame] | 216 | |
Bram Moolenaar | ade0d39 | 2020-01-21 22:33:58 +0100 | [diff] [blame] | 217 | |
| 218 | UNREADABLE SWAP FILE ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 219 | |
| 220 | Sometimes the line |
| 221 | |
| 222 | [cannot be read] ~ |
| 223 | |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 224 | will appear under the name of the swap file. This can be good or bad, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 225 | depending on circumstances. |
| 226 | |
| 227 | It is good if a previous editing session crashed without having made any |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 228 | changes to the file. Then a directory listing of the swap file will show |
| 229 | that it has zero bytes. You may delete it and proceed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 230 | |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 231 | It is slightly bad if you don't have read permission for the swap file. You |
| 232 | may want to view the file read-only, or quit. On multi-user systems, if you |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 233 | yourself did the last changes under a different login name, a logout |
Bram Moolenaar | 13fcaaf | 2005-04-15 21:13:42 +0000 | [diff] [blame] | 234 | followed by a login under that other name might cure the "read error". Or |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 235 | else you might want to find out who last edited (or is editing) the file and |
| 236 | have a talk with them. |
| 237 | |
| 238 | It is very bad if it means there is a physical read error on the disk |
| 239 | containing the swap file. Fortunately, this almost never happens. |
| 240 | You may want to view the file read-only at first (if you can), to see the |
| 241 | extent of the changes that were "forgotten". If you are the one in charge of |
| 242 | that file, be prepared to redo your last changes. |
| 243 | |
| 244 | |
Bram Moolenaar | 8f3f58f | 2010-01-06 20:52:26 +0100 | [diff] [blame] | 245 | WHAT TO DO? *swap-exists-choices* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 246 | |
Bram Moolenaar | 7dda86f | 2018-04-20 22:36:41 +0200 | [diff] [blame] | 247 | If dialogs are supported you will be asked to select one of six choices: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 248 | |
| 249 | Swap file ".main.c.swp" already exists! ~ |
| 250 | [O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort, (D)elete it: ~ |
| 251 | |
| 252 | O Open the file readonly. Use this when you just want to view the file and |
| 253 | don't need to recover it. You might want to use this when you know someone |
| 254 | else is editing the file, but you just want to look in it and not make |
| 255 | changes. |
| 256 | |
| 257 | E Edit the file anyway. Use this with caution! If the file is being edited |
| 258 | in another Vim, you might end up with two versions of the file. Vim will |
Bram Moolenaar | 7ff7846 | 2020-07-10 22:00:53 +0200 | [diff] [blame] | 259 | try to warn you when this happens, but better be safe than sorry. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 260 | |
| 261 | R Recover the file from the swap file. Use this if you know that the swap |
| 262 | file contains changes that you want to recover. |
| 263 | |
| 264 | Q Quit. This avoids starting to edit the file. Use this if there is another |
| 265 | Vim editing the same file. |
| 266 | When you just started Vim, this will exit Vim. When starting Vim with |
| 267 | files in several windows, Vim quits only if there is a swap file for the |
| 268 | first one. When using an edit command, the file will not be loaded and you |
| 269 | are taken back to the previously edited file. |
| 270 | |
| 271 | A Abort. Like Quit, but also abort further commands. This is useful when |
| 272 | loading a script that edits several files, such as a session with multiple |
| 273 | windows. |
| 274 | |
| 275 | D Delete the swap file. Use this when you are sure you no longer need it. |
| 276 | For example, when it doesn't contain changes, or when the file itself is |
| 277 | newer than the swap file. |
| 278 | On Unix this choice is only offered when the process that created the |
| 279 | swap file does not appear to be running. |
| 280 | |
| 281 | If you do not get the dialog (you are running a version of Vim that does not |
| 282 | support it), you will have to do it manually. To recover the file, use this |
| 283 | command: > |
| 284 | |
| 285 | :recover |
| 286 | |
| 287 | |
| 288 | Vim cannot always detect that a swap file already exists for a file. This is |
| 289 | the case when the other edit session puts the swap files in another directory |
| 290 | or when the path name for the file is different when editing it on different |
| 291 | machines. Therefore, don't rely on Vim always warning you. |
| 292 | |
| 293 | If you really don't want to see this message, you can add the 'A' flag to the |
| 294 | 'shortmess' option. But it's very unusual that you need this. |
| 295 | |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 296 | For remarks about encryption and the swap file, see |:recover-crypt|. |
Bram Moolenaar | cb80aa2 | 2020-10-26 21:12:46 +0100 | [diff] [blame] | 297 | For programmatic access to the swap file, see |swapinfo()|. |
Bram Moolenaar | a8ffcbb | 2010-06-21 06:15:46 +0200 | [diff] [blame] | 298 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 299 | ============================================================================== |
| 300 | *11.4* Further reading |
| 301 | |
| 302 | |swap-file| An explanation about where the swap file will be created and |
| 303 | what its name is. |
| 304 | |:preserve| Manually flushing the swap file to disk. |
| 305 | |:swapname| See the name of the swap file for the current file. |
| 306 | 'updatecount' Number of key strokes after which the swap file is flushed to |
| 307 | disk. |
| 308 | 'updatetime' Timeout after which the swap file is flushed to disk. |
| 309 | 'swapsync' Whether the disk is synced when the swap file is flushed. |
| 310 | 'directory' List of directory names where to store the swap file. |
| 311 | 'maxmem' Limit for memory usage before writing text to the swap file. |
| 312 | 'maxmemtot' Same, but for all files in total. |
| 313 | |
| 314 | ============================================================================== |
| 315 | |
| 316 | Next chapter: |usr_12.txt| Clever tricks |
| 317 | |
Bram Moolenaar | d473c8c | 2018-08-11 18:00:22 +0200 | [diff] [blame] | 318 | Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: |