Skip to content

Instantly share code, notes, and snippets.

@sirlancelot
Created January 21, 2012 04:03
Show Gist options
  • Select an option

  • Save sirlancelot/1651208 to your computer and use it in GitHub Desktop.

Select an option

Save sirlancelot/1651208 to your computer and use it in GitHub Desktop.
A Vim function to set an option only if it is currently at its default value.
" ===============================================
" _ __(_)__ _ ________
" _| |/ / // ' \/ __/ __/
" (_)___/_//_/_/_/_/ \__/
"
" Maintainer: Matthew Pietz
"
" ===============================================
" Put your overrides in here
runtime! vimrc.local.vim
" The goods
fun! SetIfDefault(Option, New)
let l:Stored = eval('&'.a:Option)
exe 'set '.a:Option.'&' | " reset the option back to its default
" Set the New option if the stored one matches the current setting
exe 'set '.a:Option.'='.(l:Stored == eval('&'.a:Option) ? a:New : l:Stored)
endfun " ;(
" Use it like this...
call SetIfDefault('backupdir', '$HOME/.vimbackup')
call SetIfDefault('directory', '$HOME/.vimswap')
call SetIfDefault('undodir', '$HOME/.vim/undo')
" Or even better...
call SetIfDefault('undodir', '$HOME/Dropbox/vim/undo')
call SetIfDefault('backupdir', '$HOME/Dropbox/vim/backup')
@sirlancelot

Copy link
Copy Markdown
Author

you can also discuss this on Reddit.

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