What is the correct way to make git ignore temporary files produced by vim in all directories (either globally across the system or locally for a single project)?
13 Answers
Vim temporary files end with ~ so you can add to the file .gitignore the line
*~
Vim also creates swap files that have the swp and swo extensions. to remove those use the lines:
*.swp
*.swo
This will ignore all the vim temporary files in a single project
If you want to do it globally, you can create a .gitignore file in your home (you can give it other name or location), and use the following command:
git config --global core.excludesfile ~/.gitignore
Then you just need to add the files you want to ignore to that file.
If you want to comment in a Git file you must do this on a separate line:
# Ignore vim files:
*~
*.swp
*.swo
Any comments placed on the same line as an active git ignore will cause the whole line to be misinterpreted.
16 Comments
.*.sw* in my .gitignore to hide them all..*.sw? to be more accurate. (Though I've seen people use some variant of *.sw* to suspect I'm the one missing something really obvious...).*.s[a-w][a-z] will ignore .svg files.Alternatively you can configure vim to save the swapfiles to a separate location,
e.g. by adding lines similar to the following to your .vimrc file:
set backupdir=$TEMP//
set directory=$TEMP//
See this vim tip for more info.
4 Comments
$TMPDIR to get the actual tempdir :)$TEMP mean?set undodir=$TEMP//, since I also save persistent undo files.This is something that should only be done on a per-user basis, not per-repository. If Joe uses emacs, he will want to have emacs backup files ignored, but Betty (who uses vi) will want vi backup files ignored (in many cases, they are similar, but there are about 24,893 common editors in existence and it is pretty ridiculous to try to ignore all of the various backup extensions.)
In other words, do not put anything in .gitignore or in core.excludes or in $GIT_DIR/config. Instead, put the info in $HOME/.gitconfig (modern git allows you to use $HOME/.config/git/ignore).
However, if you want configuration across the system for all users (which you don't), you can use git config --system core.excludesFile /path/to/system/gitignore.
3 Comments
Here is the actual Vim code that generates the swap file extensions:
/*
* Change the ".swp" extension to find another file that can be used.
* First decrement the last char: ".swo", ".swn", etc.
* If that still isn't enough decrement the last but one char: ".svz"
* Can happen when editing many "No Name" buffers.
*/
if (fname[n - 1] == 'a') /* ".s?a" */
{
if (fname[n - 2] == 'a') /* ".saa": tried enough, give up */
{
EMSG(_("E326: Too many swap files found"));
vim_free(fname);
fname = NULL;
break;
}
--fname[n - 2]; /* ".svz", ".suz", etc. */
fname[n - 1] = 'z' + 1;
}
--fname[n - 1]; /* ".swo", ".swn", etc. */
This will generate swap files of the format:
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-v][a-z]
[._]sw[a-p]
Which is pretty much what is included in GitHub's own .gitignore file for Vim.
As others have correctly noted, this .gitignore will also ignore .svg image files and .swf adobe flash files.
Comments
Quit vim before "git commit".
to make vim use other folders for backup files, (/tmp for example):
set bdir-=.
set bdir+=/tmp
to make vim stop using current folder for .swp files:
set dir-=.
set dir+=/tmp
Use -=, += would be generally good, because vim has other defaults for bdir, dir, we don't want to clear all. Check vim help for more about bdir, dir:
:h bdir
:h dir
1 Comment
# VIM: Temperory files
*~
# VIM: Swap-files
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
# VIM: Commands :cs, :ctags
tags
cscope.*
# VIM session
Session.vim
# VIM: netrw.vim: Network oriented reading, writing, browsing (eg: ftp scp)
.netrwhist
The name of the swap file is normally the same as the file you are editing, with the extension ".swp".
- On Unix, a '.' is prepended to swap file names in the same directory as the edited file. This avoids that the swap file shows up in a directory listing.
- On MS-DOS machines and when the 'shortname' option is on, any '.' in the original file name is replaced with '_'.
- If this file already exists (e.g., when you are recovering from a crash) a warning is given and another extension is used, ".swo", ".swn", etc.
- An existing file will never be overwritten.
- The swap file is deleted as soon as Vim stops editing the file.
The replacement of '.' with '_' is done to avoid problems with MS-DOS compatible filesystems (e.g., crossdos, multidos).
Comments
I found this will have git ignore temporary files created by vim:
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
*.un~
Session.vim
.netrwhist
*~
It can also be viewed here.
4 Comments
.swf files that keep popping up whenever you make a flash app..svg which may be a problem.*.un~ since you have *~There exists an excellent collection of .gitignore files over at the github/gitignore repository.
Here's the relevant Vim.gitignore file (as of 2024/02/15):
# Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]
# Session
Session.vim
Sessionx.vim
# Temporary
.netrwhist
*~
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~
4 Comments
Global/Vim.gitignore file into your answer as quoted text, with your URL being the credit to the source of the information.sure,
just have to create a ".gitignore" on the home directory of your project and have to contain
*.swp
that's it
in one command
project-home-directory$ echo '*.swp' >> .gitignore
3 Comments
echo *.swp >> .gitignore it better to avoid override of the previous .gitignore.echo '*.swp' >> .gitignore> characters. I once did that to the passwd file then logged out :-)If You are using source control. vim temp files are quite useless.
So You might want to configure vim not to create them.
Just edit Your ~/.vimrc and add these lines:
set nobackup
set noswapfile
10 Comments
screen (or dtach if he does not want all screen features). Power outages are handled by UPS. Backups are done by {your favourite software for backing up all important files}. I use swap files to get notified when I try to edit files that is already edited, backup files because having N+1'th place with backup does not harm and undo files because persistent undo is handy. Only undo files are a bit important for me.
*.sw?solves the standard.swpbut also the alternative swap file extensions like.swo..swffiles. I would strongly discourage doing that, especially if you're building a Flash app..should prevent that however..tmp.swp,.tmp.swo, ...,.tmp.swa), vim creates.tmp.svz. I lack the patience to see what comes after.tmp.saa-- perhaps.tmp.rzz? UPDATE: Looking in the source (src/memline.c, functionfindswapname()), it gives up after.saawith an error: "E326: Too many swap files found".