Viml variables

From wikinotes

Variable Scope

Rather than automatically setting scope for variables in vim, by default everything is global.

:help internal-variables
let a:var = "test"        "variable 'var' is local to current function

g:var  " global-variable    - Global.
l:var  " local-variable     - Local to a function.
s:var  " script-variable    - Local to a :source ed Vim script.
a:var  " function-argument  - Function argument (only inside a function).

b:var  " buffer-variable    - Local to the current buffer.
w:var  " window-variable    - Local to the current window.
t:var  " tabpage-variable   - Local to the current tab page.
v:var  " vim-variable       - Global, predefined by Vim.


set filetype = 'mel'  " assign vim-setting

let var = &filetype   " vim-setting TO variable
let &filetype = var   " vim-seteting AS variable

Ex Command Output

let output = execute("hi Normal")

Environment Variables

let $TMP="/var/tmp"

Variable Persistence

This setting sets up vim so that the viminfo file (shada for nvim) automatically saves/restores global-variables stored in all-caps between vim sessions.

Save occurs on vim-exit. Restore occurs during startup (see :help initialization).

:set viminfo+=!

If your plugin depends on loading these variables, during vim's init, you'll need to write an autocmd to load it.

autocmd VimEnter * call RestoreLastModColorscheme()

Get/Set Variables

let foo = get(g:, 'pluginname_setting', "default")  " retrieve variable, assign default if unassigned
exists('varname')                                   " test if variable exists