BigFish
BigFish

Reputation: 77

How to use vim variables the right way in these case?

vimrc:

"set a variable
let g:location_pprefix='~/AppData/local'

"case1
if empty(glob(location_prefix.'/nvim/autoload/plug.vim'))
  silent !curl -fLo location_prefix/nvim/autoload/plug.vim --create-dirs
    \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  autocmd VimEnter * PlugInstall --sync | source location_prefix/nvim/init.vim
endif

"case2
map ooo ilocation_prefix

"case3
set backupdir=location_pprefix/nvim/tmp/backup,.

"case4
noremap <silent> <LEADER>rc :e location_pprefix/nvim/init.vim<CR>

‍‍‎​‎‏​‌​‌‏​‏‎​‍​‏‎​‎‎​‍‏​‎​‏‏‌​‍​‎​‍‌​‌​‍‌​‍​‍‏​‎‏​‌ ‍‍‎​‎‏​‌​‌‏​‏‎​‍​‏‎​‎‎​‍‏​‎​‏‏‌​‍​‎​‍‌​‌​‍‌​‍​‍‏​‎‏​‌ ‍‍‎​‎‏​‌​‌‏​‏‎​‍​‏‎​‎‎​‍‏​‎​‏‏‌​‍​‎​‍‌​‌​‍‌​‍​‍‏​‎‏​‌

Upvotes: 0

Views: 195

Answers (1)

Amadan
Amadan

Reputation: 198314

In all of those situations, you could use :execute to concatenate the variable into the command.

exe "silent !curl -fLo ".g:location_prefix."/nvim/autoload/plug.vim..."
autocmd VimEnter * PlugInstall --sync | exe "source ".g:location_prefix."/nvim/init.vim"

The :map examples can be done more simply using <expr> (see :help :map-expression).

map <expr> ooo "i".g:location_prefix
noremap <expr> <silent> <LEADER>rc ":e ".g:location_prefix."/nvim/init.vim<CR>"

As for the :set example, you can do it by using :let instead (options can be accessed as variables prefixed by &):

let &backupdir = location_prefix."/nvim/tmp/backup,."

Upvotes: 2

Related Questions