# 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:

TypePurposeDefault Location
SwapCrash recoverySame directory as file
BackupPrevious versionSame directory as file
UndoUndo historySame directory as file

Disable Backup Files

To stop creating file~ backup files:

vim
set nobackup

This 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:

bash
mkdir -p ~/.vim/backup

The // 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:

vim
" Disable swap files
set noswapfile

Or move to dedicated location:

vim
set directory=~/.vim/swap//

Create directory:

bash
mkdir -p ~/.vim/swap
chmod 700 ~/.vim/swap  " Secure permissions

Undo File Configuration

Undo files persist undo history across sessions:

vim
" Disable undo files
set noundofile

Or move to dedicated location:

vim
set undodir=~/.vim/undo//
set undofile

Create directory:

bash
mkdir -p ~/.vim/undo
chmod 700 ~/.vim/undo

Complete 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:

vim
set backupskip=/tmp/*,/private/tmp/*,$TMPDIR/*,$TMP/*,$TEMP/*

Git automatically ignores temporary directories, but add to your .gitignore:

gitignore
# Vim safety files
*.swp
*.swo
*~
*.un~

Or be more thorough:

gitignore
# 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:

vim
:recover filename

Or use swap file directly:

vim
:vim -r filename.swp

List available swap files:

vim
:swapname

Writebackup Alternative

For temporary backups only during write:

vim
set writebackup
set nobackup

This creates a backup during the write operation, then deletes it. Safer but no clutter.

Backup Retention

Control how many backups to keep:

vim
" Keep only one backup
set backupcopy=yes

For versioned backups, use a plugin like vim-backup:

vim
Plug 'dag/vim-backup'

Project-Specific Settings

For specific projects, use local vimrc:

vim
" In project root, create .vimrc or .exrc
set nobackup
set noswapfile

Or use editorconfig:

ini
[*.py]
vim_backup = false
vim_swapfile = false

Clean 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. 1.Check backupdir: :set backupdir?
  2. 2.Check directory (swap): :set directory?
  3. 3.Check undodir: :set undodir?
  4. 4.Verify directories exist
  5. 5.Add patterns to gitignore
  6. 6.Set backupskip for temp files
  7. 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.