Viml variables: Difference between revisions
From wikinotes
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
= Variable Scope = | = Variable Scope = | ||
<blockquote> | |||
Rather than automatically setting scope for variables in vim, by default | Rather than automatically setting scope for variables in vim, by default | ||
everything is global. | everything is global. | ||
Line 17: | Line 18: | ||
v:var " vim-variable - Global, predefined by Vim. | v:var " vim-variable - Global, predefined by Vim. | ||
</source> | </source> | ||
</blockquote><!-- Variable Scope --> | |||
= Options = | = Options = | ||
<blockquote> | |||
<source lang="vim"> | <source lang="vim"> | ||
set filetype = 'mel' " assign vim-setting | set filetype = 'mel' " assign vim-setting | ||
Line 25: | Line 28: | ||
let &filetype = var " vim-seteting AS variable | let &filetype = var " vim-seteting AS variable | ||
</source> | </source> | ||
</blockquote><!-- Options --> | |||
= Variable Persistence = | = Variable Persistence = | ||
<blockquote> | |||
This setting sets up vim so that the <code>viminfo</code> file (<code>shada</code> for nvim) | This setting sets up vim so that the <code>viminfo</code> file (<code>shada</code> for nvim) | ||
automatically saves/restores global-variables stored in all-caps between vim sessions. | automatically saves/restores global-variables stored in all-caps between vim sessions. | ||
Line 42: | Line 47: | ||
autocmd VimEnter * call RestoreLastModColorscheme() | autocmd VimEnter * call RestoreLastModColorscheme() | ||
</source> | </source> | ||
</blockquote><!-- Variable Persistence --> | |||
= Get/Set Variables = | = Get/Set Variables = |
Revision as of 20:01, 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