Ignore backup files produced by VIm and Emacs in all your git projects

Overview

Using VIm and Emacs on a daily basis is very common for programmers.

They have recovering systems to avoid losing work if a computer crashes, that would make your directories full of strange files ending in ~, swap files (.swp) or backup files named like #<filename>#.

Those files would appear from time to time in your git status listings that wouldn’t add any valuable information.

Better than disabling them, is to avoid seeing them globally, so any new git repository won’t show them in the status of changed file list but you will still be able to recover them if anything bad happens while editing.

Enabling gitignore globally

We will set up a global .gitignore_global file, i.e.: a list of rules for ignoring files in every Git repository on the computer.


$ git config --global core.excludesfile ~/.gitignore_global


This command will automatically set the following configuration in your ~/.gitconfig_global file.

[core]
        excludesfile = /home/user/.gitignore_global

Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by core.excludesFile in the user’s ~/.gitconfig. Its default value is $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/ignore is used instead.

gitignore

Github has a great project with useful .gitignore files optimized for a Operating System and editor specific.

We grab the ones for Emacs and Vim (I don’t think you ever need any other editor ;)

You should put them together in a single ~/.gitignore_global file that will be processed globally.

gitignore for Emacs

Gitignore for Emacs editor:

# -*- mode: gitignore; -*-
*~
\#*\#
/.emacs.desktop
/.emacs.desktop.lock
*.elc
auto-save-list
tramp
.\#*

# Org-mode
.org-id-locations
*_archive

# flymake-mode
*_flymake.*

# eshell files
/eshell/history
/eshell/lastdir

# elpa packages
/elpa/

# reftex files
*.rel

# AUCTeX auto folder
/auto/

# cask packages
.cask/
dist/

# Flycheck
flycheck_*.el

# server auth directory
/server/

# projectiles files
.projectile

# directory configuration
.dir-locals.el

gitignore for Vim

Gitignore for Vim editor:

# swap
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-v][a-z]
[._]sw[a-p]
# session
Session.vim
# temporary
.netrwhist
*~
# auto-generated tag files
tags

Other global gitignore properties

Other common .gitignore configurations are also available in Github’s gist that may worth adding them to the global gitignore file:

Conclusion

Now every time you work in a new or old project you won’t have to deal with any backup files automatically generated by Vim or Emacs.

References

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.