In my .vimrc, I have the following variable defined:

let $VIMFILES = "~/.vim"

Further in my .vimrc, I attempt to set the backupdir option like so:

set backupdir=$VIMFILES/backup
" `set backupdir?` will return: backupdir=~/.vim/backup

However, it does not work. Attempting to :w a file will fail and return the following:

"foo" E510: Can't make backup file (add ! to override)

Strangely enough, setting backupdir using the following method works just fine...

exec "set backupdir=" . $VIMFILES . "/backup"
" Like the first method, `set backupdir?` will
" also return: backupdir=~/.vim/backup

Can anyone shed light as to why the first method fails and the second method works, despite backupdir having the desired value in both cases? Am I using any improper techniques, and if so, what should I use instead?


I can reproduce this, and indeed, it looks quite puzzling. One tricky thing is that :set backupdir? does not necessarily print the actual option value; it performs some transformations, so what you see is not always what is used.

I think the problem lies in the use of the ~/ in the $VIMFILES variable. When you directly :set the option (as emulated by the last :execute attempt), Vim seems to automatically expand that. But when you assign that to an environment variable, it is kept verbatim, and due to the lack of expansion, the :write fails. Note that the output is identical because Vim apparently automatically translates /home/username back to ~ in the output.

I would solve this by explicitly expanding the variable value:

let $VIMFILES = expand("~/.vim")
  • Thanks for the valuable insight! I was not aware of how and when Vim internally expanded the variables. You're quite right about set transforming the value too. For example, :echo expand("~/.vim/backup") returns: /home/zeroknight/.vim/backup, and :set backupdir? returns: ~/.vim/backup. Thank you again for the solution and the insight!
    – ZeroKnight
    Jan 16 '14 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.