# Vim Backup Files Cluttering Directory
Your project directory is full of files like file.txt~, file.txt.swp, and .file.txt.un~. These are Vim's safety files, but they can clutter your workspace and confuse version control. Let me show you how to manage them.
Understanding Vim's Safety Files
Vim creates several types of safety files:
| Type | Purpose | Default Location |
|---|---|---|
| Swap | Crash recovery | Same directory as file |
| Backup | Previous version | Same directory as file |
| Undo | Undo history | Same directory as file |
Disable Backup Files
To stop creating file~ backup files:
set nobackupThis prevents Vim from creating backup files entirely.
Configure Backup Location
Instead of disabling, move backups to a dedicated directory:
```vim " Create backup directory set backupdir=~/.vim/backup//
" Enable backups set backup ```
Create the directory:
mkdir -p ~/.vim/backupThe // suffix causes Vim to use the full path as filename, preventing collisions:
```vim " Without // set backupdir=~/.vim/backup " Backup: /home/user/.vim/backup/file.txt~
" With // set backupdir=~/.vim/backup// " Backup: /home/user/.vim/backup/home_user_project_file.txt~ ```
Swap File Configuration
Swap files enable crash recovery but clutter directories:
" Disable swap files
set noswapfileOr move to dedicated location:
set directory=~/.vim/swap//Create directory:
mkdir -p ~/.vim/swap
chmod 700 ~/.vim/swap " Secure permissionsUndo File Configuration
Undo files persist undo history across sessions:
" Disable undo files
set noundofileOr move to dedicated location:
set undodir=~/.vim/undo//
set undofileCreate directory:
mkdir -p ~/.vim/undo
chmod 700 ~/.vim/undoComplete Safe-File Configuration
```vim " Backup configuration set backup set backupdir=~/.vim/backup// set backupskip=/tmp/*,/private/tmp/*
" Swap configuration set directory=~/.vim/swap//
" Undo configuration set undofile set undodir=~/.vim/undo//
" Create directories if needed if !isdirectory(&backupdir) call mkdir(&backupdir, 'p') endif if !isdirectory(&directory) call mkdir(&directory, 'p') endif if !isdirectory(&undodir) call mkdir(&undodir, 'p') endif ```
Neovim Configuration
```lua vim.opt.backup = true vim.opt.backupdir = vim.fn.expand('~/.vim/backup//') vim.opt.directory = vim.fn.expand('~/.vim/swap//') vim.opt.undofile = true vim.opt.undodir = vim.fn.expand('~/.vim/undo//')
-- For XDG compliance vim.opt.backupdir = vim.fn.expand('~/.local/share/nvim/backup//') vim.opt.directory = vim.fn.expand('~/.local/share/nvim/swap//') vim.opt.undodir = vim.fn.expand('~/.local/share/nvim/undo//') ```
Skip Backups for Certain Files
Don't create backups for temporary or volatile files:
set backupskip=/tmp/*,/private/tmp/*,$TMPDIR/*,$TMP/*,$TEMP/*Git automatically ignores temporary directories, but add to your .gitignore:
# Vim safety files
*.swp
*.swo
*~
*.un~Or be more thorough:
# Vim
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-v][a-z]
[._]sw[a-p]
*~Cleanup Existing Safety Files
Remove existing backup/swap/undo files:
```bash # Find and remove backup files find . -name "*~" -delete
# Find and remove swap files find . -name "*.swp" -delete find . -name "*.swo" -delete
# Find and remove undo files find . -name "*.un~" -delete ```
Or use Vim's :DeleteHiddenBuffers if available.
Recovering from Swap Files
When Vim crashes, it offers to recover from swap. If you miss the prompt:
:recover filenameOr use swap file directly:
:vim -r filename.swpList available swap files:
:swapnameWritebackup Alternative
For temporary backups only during write:
set writebackup
set nobackupThis creates a backup during the write operation, then deletes it. Safer but no clutter.
Backup Retention
Control how many backups to keep:
" Keep only one backup
set backupcopy=yesFor versioned backups, use a plugin like vim-backup:
Plug 'dag/vim-backup'Project-Specific Settings
For specific projects, use local vimrc:
" In project root, create .vimrc or .exrc
set nobackup
set noswapfileOr use editorconfig:
[*.py]
vim_backup = false
vim_swapfile = falseClean Viminfo
Viminfo stores history but can get corrupted:
```bash # Clean viminfo rm ~/.viminfo
# Clean shada (Neovim) rm ~/.local/share/nvim/shada/main.shada ```
Troubleshooting Checklist
- 1.Check backupdir:
:set backupdir? - 2.Check directory (swap):
:set directory? - 3.Check undodir:
:set undodir? - 4.Verify directories exist
- 5.Add patterns to gitignore
- 6.Set backupskip for temp files
- 7.Consider writebackup for safer writes
Quick Reference
```vim " Disable all safety files set nobackup set noswapfile set noundofile
" Move to dedicated directories set backupdir=~/.vim/backup// set directory=~/.vim/swap// set undodir=~/.vim/undo// set undofile
" Skip certain files set backupskip=/tmp/*
" Clean clutter :!find . -name "*~" -delete :!find . -name "*.swp" -delete ```
Organizing Vim's safety files into dedicated directories keeps your projects clean while maintaining crash recovery and undo history. The // suffix is key to preventing filename collisions.