Skip to content

Instantly share code, notes, and snippets.

@dkasak
Forked from kaleb/XDG.vim
Last active May 4, 2024 05:45
Show Gist options
  • Save dkasak/6ae1c6bf0d771155f23b to your computer and use it in GitHub Desktop.
Save dkasak/6ae1c6bf0d771155f23b to your computer and use it in GitHub Desktop.
vim XDG Base Directory support
" XDG Environment For VIM
" =======================
"
" References
" ----------
"
" - http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
" - http://tlvince.com/vim-respect-xdg
" - https://gist.github.com/kaleb/3885679 (the original version)
"
" Instructions
" ------------
"
" 1. Create the following directory structure:
"
" - $XDG_CACHE_HOME/vim
" - $XDG_CACHE_HOME/vim/undo
" - $XDG_CACHE_HOME/vim/swap
" - $XDG_CACHE_HOME/vim/backup
" - $XDG_CONFIG_HOME/vim
" - $XDG_DATA_HOME/vim/bundle (optional, for a plugin manager such as Vundle)
"
" Example commands:
" `mkdir -p $XDG_CACHE_HOME/vim/{undo,swap,backup} $XDG_CONFIG_HOME/vim` or
" `mkdir -p $HOME/.cache/vim/{undo,swap,backup} " $HOME/.config`.
"
" 2. Source this file near the top of your vimrc (but *below* set nocompatible,
" since setting that resets the viminfo setting)
" 3. (Optional) vim still tries to read your vimrc from standard paths, so if
" you want to move it elsewhere (e.g. $XDG_CONFIG_HOME/vim/vimrc), you can
" do oneof of two things:
" 3a. Always run vim using "vim -u <path_to_vimrc>".
" 3b. Set the environment variable VIMINIT to "source <path_to_vimrc>"
" (the content of VIMINIT can be any ex command).
if empty("$XDG_CACHE_HOME")
let $XDG_CACHE_HOME="$HOME/.cache"
endif
if empty("$XDG_CONFIG_HOME")
let $XDG_CONFIG_HOME="$HOME/.config"
endif
if empty("$XDG_DATA_HOME")
let $XDG_DATA_HOME="$HOME/.local/share"
endif
set directory=$XDG_CACHE_HOME/vim/swap,~/,/tmp
set backupdir=$XDG_CACHE_HOME/vim/backup,~/,/tmp
set undodir=$XDG_CACHE_HOME/vim/undo,~/,/tmp
set viminfo+=n$XDG_CACHE_HOME/vim/viminfo
set runtimepath+=$XDG_CONFIG_HOME/vim,$XDG_CONFIG_HOME/vim/after,$VIM,$VIMRUNTIME
let $MYVIMRC="$XDG_CONFIG_HOME/vim/vimrc"
@aod
Copy link

aod commented Jul 6, 2020

Thank you so much this is a great find for setting up XDG+Vim.

One thing I might add is to use "$XDG_DATA_HOME" instead for undodir, backupdir, etc. which is where user-specific data should be written to.

... There is a single base directory relative to which user-specific non-essential (cached) data should be written. This directory is defined by the environment variable $XDG_CACHE_HOME. ...

@dkasak
Copy link
Author

dkasak commented Jul 7, 2020

@aod Hey, thanks. The reason I used XDG_CACHE_HOME is that I consider undo info and backups as non-essential. The logic I use is this: in a regular situation, would I lose data (code, documents, etc) by deleting this content? The answer is no, unless it's an outlier situation where I already lost the original data. On the other hand, if a program needs a place to store its primary database, that's a clear case for XDG_DATA_HOME since that's the primary copy of the data. I'll take your suggestion into account and think about it, though.

@gaelicWizard
Copy link

@dkasak, @aod, there's a new $XDG_STATE_HOME for specifically this not-config, not-data, not-cache stuff. Undo is definitely state, backup probably is too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment