Skip to content

Instantly share code, notes, and snippets.

@kaleb
Last active January 17, 2022 23:16
Show Gist options
  • Save kaleb/3885679 to your computer and use it in GitHub Desktop.
Save kaleb/3885679 to your computer and use it in GitHub Desktop.
VIM XDG Configuration
" XDG Environment For VIM
" =======================
"
" References
" ----------
"
" - http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
" - http://tlvince.com/vim-respect-xdg
"
if empty($XDG_CACHE_HOME)
let $XDG_CACHE_HOME = '~/.cache'
endif
if empty($XDG_CONFIG_HOME)
let $XDG_CONFIG_HOME = '~/.config'
endif
if !isdirectory($XDG_CACHE_HOME . "/vim/swap")
call mkdir($XDG_CACHE_HOME . "/vim/swap", "p")
endif
set directory=$XDG_CACHE_HOME/vim/swap//,/var/tmp//,/tmp//
if !isdirectory($XDG_CACHE_HOME . "/vim/backup")
call mkdir($XDG_CACHE_HOME . "/vim/backup", "p")
endif
set backupdir=$XDG_CACHE_HOME/vim/backup//,/var/tmp//,/tmp//
" Double slash does not actually work for backupdir, here's a fix
au BufWritePre * let &backupext='@'.substitute(substitute(substitute(expand('%:p:h'), '/', '%', 'g'), '\', '%', 'g'), ':', '', 'g')
" see :help persistent-undo
if !isdirectory($XDG_CACHE_HOME . "/vim/undo")
call mkdir($XDG_CACHE_HOME . "/vim/undo", "p")
endif
set undodir=$XDG_CACHE_HOME/vim/undo//,/var/tmp//,/tmp//
set undofile
set viminfo+=n$XDG_CACHE_HOME/vim/viminfo
set runtimepath-=~/.vim
set runtimepath^=$XDG_CONFIG_HOME/vim
set runtimepath-=~/.vim/after
set runtimepath+=$XDG_CONFIG_HOME/vim/after
source $XDG_CONFIG_HOME/vim/vimrc
@dkasak
Copy link

dkasak commented Feb 26, 2015

This should be added:

set undodir=$XDG_CACHE_HOME/vim,~/,/tmp

Also consider separating these paths into separate directories under the $XDG_CACHE_HOME/vim directory so the files are easier to find in case a manual manipulation is needed, like this:

set directory=$XDG_CACHE_HOME/vim/swap,~/,/tmp
set backupdir=$XDG_CACHE_HOME/vim/backup,~/,/tmp
set undodir=$XDG_CACHE_HOME/vim/undo,~/,/tmp

@kaleb
Copy link
Author

kaleb commented May 8, 2015

@dkasak I actually did end up doing something similar. I should update this gist. https://github.com/kaleb/vim-files/blob/master/xdg.vim

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