Vim .gitignore template
The Vim template ignores swap files across the whole range of extensions the editor uses, along with saved sessions, persistent undo files and the tags index.
At a glance
- Category
- IDE
- Patterns in this template
- 12 rules
- Directories ignored
- None. Every rule matches by filename or extension rather than by directory.
- Re-include rules
- 1 line starts with ! to keep something back
- Also needs an OS template
- Yes. Swap files are not operating system artefacts.
- Typical companions
- The language template for the project. This one is worth adding to any repository.
In detail
The bracket patterns look cryptic until you know what they are for. When Vim opens a file it creates a swap file holding unsaved changes, named after the original with a .swp extension. If that name is taken, because a second session is editing the same file, it uses .swo, then .swn, and continues backwards through the alphabet. The character ranges catch the whole family in a few lines instead of thirty.
The leading [._] in each pattern covers both naming conventions. A swap file for foo.txt is normally .foo.txt.swp with a leading dot, but with certain settings it is foo.txt.swp instead, so both forms need matching. The bracket is doing the same job the ranges do further along the line: collapsing a family of similar names into one rule instead of listing every possible variant.
!*.svg is the negation that stops the range patterns from doing damage. The pattern [._]*.s[a-v][a-z] would otherwise match a filename ending in .svg, because s, v and g fall inside those ranges. Without the negation, an entire directory of vector graphics would silently vanish from a repository, which is a memorable way to learn how character classes work.
Session.vim and Sessionx.vim are saved sessions: which files were open, which windows were split, where the cursor sat. They are a snapshot of one person's workspace and they contain absolute paths. [._]*.un~ files are the persistent undo history, which stores the full edit history of a file so undo survives closing the editor, and which therefore contains text you deleted and never committed.
tags is the index file that ctags generates for jump to definition. It is derived from the source, it is large, and it goes stale immediately, so regenerating it is always cheaper than merging someone else's copy. .netrwhist is the history of directories visited in the built in file browser.
Examples
[._]*.s[a-v][a-z]
!*.svg
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]
Session.vim
Sessionx.vim
.netrwhist
*~
tags
[._]*.un~12 lines exactly as the tool writes them, before any common additions you tick.
Things that catch people out
- The !*.svg negation is load bearing. Delete it and the swap file range patterns start matching .svg files, quietly removing every vector asset from git status.
- Undo files hold text you deleted before saving. Ignoring them is right, and a committed one is closer to a leaked draft than to build output.
- *~ appears here and in several other templates. It is a generic backup suffix, so a repository that keeps a file deliberately ending in a tilde will find it ignored.
Try it: .gitignore Generator
Pick one or more templates, tick the common additions you want, and copy or download the combined .gitignore.
.gitignore Generator
LOCAL · Browser-OnlyGenerate comprehensive .gitignore files by combining templates for languages, frameworks, IDEs, and operating systems
Language
Framework
IDE
OS
Build Tool
Infrastructure
Common Additions
Preview
Select templates or common additions to generate a .gitignore file.
Related .gitignore templates
All .gitignore templates →Emacs .gitignore template
The Emacs template covers tilde backup files, the hash delimited auto save files, the symbolic lock files and compiled Lisp, plus per project tooling state.
Linux .gitignore template
The shortest template in the collection. Five rules covering editor backup files, file manager settings, per volume trash and NFS silly rename files.
VS Code .gitignore template
This template ignores the .vscode folder's contents but keeps five files back on purpose, which is the clearest statement of the commit or ignore argument.
Sublime Text .gitignore template
The Sublime template separates the project file from the workspace file, and covers sftp-config.json, which stores upload credentials in plain text.
Xcode .gitignore template
The Xcode template ignores the project bundle's contents while keeping the project file and shared data, the only way to share schemes but not sessions.
Android Studio .gitignore template
The Android Studio template ignores local.properties, which names the SDK path on one machine, along with build output, captures and native build directories.
Tools for this
- .gitignore Generator Generate .gitignore files for any project - 50+ templates for languages, frameworks, IDEs, and OS
- Git Commit Message Generator Free online git commit message generator - generate conventional commit messages with type, scope, and description