Viml variables: Difference between revisions
From wikinotes
(Created page with "= Variable Scope = Rather than automatically setting scope for variables in vim, by default everything is global. <source lang="vim"> :help internal-variables let a:var = "te...") |
No edit summary |
||
Line 42: | Line 42: | ||
autocmd VimEnter * call RestoreLastModColorscheme() | autocmd VimEnter * call RestoreLastModColorscheme() | ||
</source> | </source> | ||
= Get/Set Variables = | |||
<blockquote> | |||
<syntaxhighlight lang="vim"> | |||
let foo = get(g:, 'pluginname_setting', "default") " retrieve variable, assign default if unassigned | |||
exists('varname') " test if variable exists | |||
</syntaxhighlight> | |||
</blockquote><!-- Get/Set Variables --> |
Revision as of 19:59, 18 July 2021
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.
Options
set filetype = 'mel' " assign vim-setting
let var = &filetype " vim-setting TO variable
let &filetype = var " vim-seteting AS variable
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