What are those .un~ files in Vim, and how to manage them
You might have noticed some mysterious files with a .un~
extension showing up in your project directories.
These files are created by Vim, a popular text editor used by many programmers, and can sometimes cause confusion
or clutter in your file system. In this article, we’ll explain what these files are, why Vim creates them, and how
you can manage them to keep your projects organized.
What are .un~
files?
When you edit a file in Vim, the editor creates a backup file with the same name as the original file and a tilde
(~
) character at the end, by default. This file contains the contents of the original file before you started
editing it, and is used by Vim to restore the original contents if something goes wrong during editing.
However, if you have enabled Vim’s ‘persistent undo’ feature by setting the ‘undofile’ option, Vim will create
a .un~
file instead of a backup file. This file contains undo information for the edited file, and is
used to restore the undo history when the file is reopened in Vim. The .un~
file is stored in the same
directory as the original file.
Why does Vim create .un~
files?
The backup and persistent undo features are designed to help you recover from mistakes or system crashes while editing files. By default, Vim saves backup files in case you accidentally overwrite or delete the original file. With the ‘undofile’ option, Vim saves undo information so that you can undo changes made to the file even after you close and reopen it.
How to manage .un~
files?
While .un~ files can be helpful, they can also clutter your file system and cause confusion if you’re not
sure what they’re for. Here are some tips for managing .un~
files in your projects:
- Disable the ‘undofile’ option:
If you don’t need persistent undo information, you can disable the ‘undofile’ option by using the command:set noundofile
. This will prevent Vim from creating.un~
files and only create backup files when you edit files. - Customize the location of undo files:
If you do need persistent undo information, you can customize the location of the.un~
files by setting the ‘undodir’ option. For example, you can use the command:set undodir=~/.vim/undo
’ to store all undo files in the~/.vim/undo
directory. This can help keep your project directories clean and organized. - Clean up unused
.un~
files:
If you have a lot of .un~ files in your project directories, you can clean them up using the command:!rm *.un~
in Vim. This will delete all.un~
files in the current directory. However, be careful not to delete any important files by mistake.
Conclusion
Understanding what .un~
files are and why Vim creates them can help you manage your projects more effectively.
By disabling the ‘undofile’ option or customizing the location of undo files, you can reduce clutter in your
file system and keep your projects organized. And if you do need to clean up unused .un~
files, be sure to use
caution to avoid deleting important files. Happy coding!