yaohunzhanyue / collcetion

6 stars 0 forks source link

How to use vim variables #7

Open yaohunzhanyue opened 4 years ago

yaohunzhanyue commented 4 years ago

https://stackoverflow.com/questions/60703143/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>
yaohunzhanyue commented 4 years ago

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,."