254

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)?

jrdioko
  • 28,317
  • 26
  • 75
  • 116
  • 6
    The pattern `*.sw?` solves the standard `.swp` but also the alternative swap file extensions like `.swo`. – buley Apr 08 '12 at 19:30
  • 7
    @buley your suggestion will also ignore `.swf` files. I would strongly discourage doing that, especially if you're building a Flash app. – stephenmurdoch May 12 '14 at 11:23
  • 1
    @marflar Good point. Same pattern prefixed with the hidden file `.` should prevent that however. – buley May 13 '14 at 15:38
  • I saw a .swx today too. – Richard May 14 '15 at 03:37
  • 4
    Experiment shows that after creating 16 backup files (`.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`, function `findswapname()`), it gives up after `.saa` with an error: "E326: Too many swap files found". – Keith Thompson Jul 15 '16 at 23:53
  • Unfortunately, `enew` creates `.swp` – D. Ben Knoble Dec 22 '18 at 23:26

12 Answers12

414

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

Metagrapher
  • 7,732
  • 1
  • 22
  • 30
nunopolonia
  • 13,237
  • 3
  • 24
  • 28
  • 2
    I also recommend putting user-specific patterns in a file specified by `code.excludesfile` (so that the `.gitignore` file at the root of each project only collects project-specific patterns instead of the patterns to avoid the temporary files from every contributor’s pet platform/editor/IDE/whatever). Also, if there is a chance that you might want to apply Git to your home directory, you might use something like `~/.git-user-excludes` for `core.excludesfile` so that `~/.gitignore` can be used for the patterns that are specific to your home directory itself. – Chris Johnsen Jan 28 '11 at 03:11
  • That was the reason I said that any filename or location could be used to store the global excludesfile. As for the advices on the use of the global excludes file, I totally agree with you. – nunopolonia Jan 28 '11 at 03:13
  • 31
    Vim will create successively named swap files (.swp, .swo, etc.), so I use `.*.sw*` in my `.gitignore` to hide them all. – Drew Stephens May 20 '11 at 01:15
  • 34
    @DrewStephens, I believe `.*.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...) – Morten Siebuhr Nov 07 '11 at 12:55
  • 10
    @Morten According to http://vimdoc.sourceforge.net/htmldoc/recover.html, .*.s?? would catch them all on Unix (the .s?? decrements from .swp to .saa). – Max Nanasy Jul 22 '12 at 19:32
  • 6
    Just to expand on @MatrixFrog excellent comment a bit - since this question is for vim you can find the correct .gitignore in that project [here](https://github.com/github/gitignore/blob/master/Global/vim.gitignore). – SnapShot Aug 10 '12 at 13:58
  • github gitignore file uses .*.s[a-w][a-z] – nunopolonia Sep 30 '13 at 22:19
  • 19
    Be careful. github's `.*.s[a-w][a-z]` will ignore `.svg` files. – Jon Oct 02 '13 at 16:27
  • 2
    Corrected github URL, the V is uppercase. https://github.com/github/gitignore/blob/master/Global/Vim.gitignore – Gavilan Comun Mar 28 '15 at 12:29
  • The answer does not mention the right command for setting up a global .gitignore on Windows. You can do so by using `git config core.excludesfile C:\Users\\.gitignore_global`. You can refer to the sources here (1) [Visual Studio](https://www.visualstudio.com/en-us/docs/git/tutorial/ignore-files) and (2) [Setting up a global .gitignore on Mac and Windows](http://islegend.com/development/setting-global-gitignore-mac-windows/). – user3613932 Dec 28 '16 at 23:22
  • Find a good explanation (from the source code) of those swapfile names at https://unix.stackexchange.com/a/326710/9745 – Bruno Bronosky Oct 26 '17 at 16:03
  • @DrewStephens `:enew` creates `.swp`, not covered by `.*.sw?` – D. Ben Knoble Dec 22 '18 at 23:27
  • .svg files are no longer ignored by github's gitignore by default. Check https://github.com/github/gitignore/pull/3169/commits/663769ca50ca326899815f6351b57e7af1123068 – mazunki Sep 10 '19 at 17:48
124

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.

Rolando Isidoro
  • 4,386
  • 2
  • 27
  • 37
martinl
  • 1,520
  • 1
  • 8
  • 10
26

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 in $GIT_DIR/config. Put the info in $HOME/.gitconfig instead (as nunopolonia suggests with --global.) Note that "global" means per-user, not per-system.

If you want configuration across the system for all users (which you don't), you'll need a different mechanism. (Possibly with templates setup prior to initialization of the repository.)

William Pursell
  • 174,418
  • 44
  • 247
  • 279
  • 3
    Apparently, Betty is much smarter than Joe :-) Good advice and deserving of a vote, even though I ended up using the answer from martinl (fixing it in Vim rather than Git). – paxdiablo Dec 11 '15 at 02:33
11

I would also recommend to think to ignore files like:

.*.swp
.*.swo

as you may have files that end with .swp

Anastasios Andronidis
  • 4,661
  • 4
  • 26
  • 44
6

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.

rouble
  • 11,713
  • 15
  • 86
  • 91
5

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
Andrew_1510
  • 9,708
  • 8
  • 47
  • 50
  • 6
    Be careful where you store your vim swap files. You could accidentally leak credentials or other data from a file you are editing: http://security.stackexchange.com/questions/65883/what-sensitive-information-can-be-leaked-in-a-vims-swap-file – Mnebuerquo Jul 06 '15 at 14:46
4
# 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).

http://vimdoc.sourceforge.net/htmldoc/recover.html

http://www.vim.org/scripts/script.php?script_id=1075

user31986
  • 1,234
  • 1
  • 8
  • 25
3

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.

SnapShot
  • 5,124
  • 4
  • 39
  • 36
1

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
bishop
  • 32,403
  • 9
  • 89
  • 122
forellana
  • 877
  • 1
  • 8
  • 17
0

In myy case the temporary files are already commited by previous actions, so modifying .gitignore will not affect those commited files..., you have to git rm files_to_be_ignored --cache first, then commit, then DONE.

ddwolf
  • 65
  • 1
  • 10
0

This works on a Mac as noted by Alex Moore-Niemi:

set backupdir=$TMPDIR//
set directory=$TMPDIR//

Make sure to use TMPDIR and not TEMPDIR.

Powers
  • 12,561
  • 7
  • 60
  • 82
-4

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
Arnis Lapsa
  • 39,296
  • 27
  • 111
  • 190
  • 6
    Source control does not operate at the same granularity as either backups or Vim’s swapfiles. Even if you were committing after every save (or after a certain number of characters or seconds — a simplification of what Vim’s swapfiles give you), it is still highly likely that you might want to edit files that are not source controlled (for which backups and swapfiles are still desirable). – Chris Johnsen Jan 28 '11 at 03:23
  • @Chris I don't see that as simplification. And source control commits are supposed to happen often. But whatever. Was just trying to help. – Arnis Lapsa Jan 28 '11 at 07:17
  • 8
    VIM temporary files are extremely useful for editor crashes and for several other reasons. Turning them off is throwing out the baby with the bath water. – Brian Riehman Jan 28 '11 at 14:03
  • @Brian what are You doing that Your vim crashes? – Arnis Lapsa Jan 28 '11 at 14:28
  • 5
    @Arnis Say, a power outage? Or having your X session die for some reason. vim temp files are quite important IMO. – jrdioko Jan 28 '11 at 17:04
  • 1
    @jrdioko X crash is the reason why one may want to use `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. – ZyX Jan 28 '11 at 21:42
  • It's just from different background. I'm not a long time hard core vim user. I'm used to saving often, doing same in vim too with `:wa`. With often commits - I just never faced such a problem so backup files seems useless for me. So my answer is quite legit - just from different angle. – Arnis Lapsa Jan 29 '11 at 19:17
  • Thanks this is definitely the best solution: I also think `:wa` often is a great idea. – user157251 Dec 01 '11 at 18:00
  • 5
    You are better off telling vim to store temp files elsewhere than completely turning them off. – Kyle Oct 25 '13 at 20:49
  • 1
    The question asked was how to make git ignore the swap files. Removing them entirely doesn't really answer the question. I use git, save often, run in a tmux session, and have a battery backup, but I still want vim to make backup files just in case, and I want to keep them out of my git repos. – Mnebuerquo Jul 06 '15 at 14:53