Level 2 Linux Exercises
Linux Exercise #2.8 – Unexpected Situations (Swap Files)
Background Information
IMPORTANT:
If you like to use background/foreground modes with editors, please read this section careful. Use with caution!
- Backgrounding/Foregrounding Vim: Use care when working with files in the background. If your changes are not saved, when re-compiling programs the changes will not be in effect. You also have a higher risk of having left over swap files which can be problematic. Only use background mode if you are an advanced vim user:
- Send current session to the background
control z
Return to the most recent background session
fg
- Send current session to the background
- Temporary swap files are created for files that have been opened. If vim is exited or killed without using the typical quit command, a swap file will be left over from that session. To resolve use the following:
- When opening a file that has a swap file, you should first try to determine if your file is still open but in the background. To do this, first abort the request to pen a file: Type “
a
” to abort. Now you are back at the command line. - To determine if your file is still open (but in the background), type “
fg
”- If this opens your file, then just continue editing and next time use “
fg
” if you usedctrl z
. - If you commonly receive swap files, avoid using the background mode. It is not necessary to program!
- If this opens your file, then just continue editing and next time use “
- If there were no files in the background, then determine if there are swap files in your directory. Type “
ls -a
” to see if a.swp
file exists for the file you were trying to open. The-a
flag shows what hidden files are in the directory- If it exists, you will need to see which file has your newest changes
- Check both the
.swp
and the regular file
- Examine the swap file
- If the
.swp
file is not current, remove the swap file (use therm
command) - If the
.swp
file is current, you will need to recover the file by typing “r
” when first opening it. This will merge the files and remove the.swp
file from your directory and you can just use the file as normal.
- If the
- When opening a file that has a swap file, you should first try to determine if your file is still open but in the background. To do this, first abort the request to pen a file: Type “
Handling Unexpected Situations