38

I installed this plugin with Vundle and noticed that it has some settings (the README calls them variables) like:

b:javascript_fold

and

g:javascript_conceal

The README says I, "can add them to your .vimrc to enable/disable their features." But I'm not sure how to set them via :. These variables are not available through set or echo, nor are they commands or functions. How can I set/inspect these?

2 Answers 2

62

These are internal variables.

You create and modify them with the :let command:

:let g:var_name = 1

You destroy them with :unlet.

You inspect them with :echo.

The prefix shows the scope of the variable; from :help internal-variables:

buffer-variable    b:    Local to the current buffer.
window-variable    w:    Local to the current window.
tabpage-variable   t:    Local to the current tab page.
global-variable    g:    Global.
local-variable     l:    Local to a function.
script-variable    s:    Local to a |:source|'ed Vim script.
function-argument  a:    Function argument (only inside a function).
vim-variable       v:    Global, predefined by Vim.

Adding that kind of variable to your ~/.vimrc goes like this:

let g:var_name = 1

Usually, only global variables are to be added to your ~/.vimrc, buffer-local variables are to be used in filetype plugins.

1
  • 2
    For completeness: with no prefix: In a function: local to a function; otherwise: global
    – Tom Hale
    Aug 2, 2016 at 8:46
12

Configuration variables are not options, they only "emulate" those for plugins. Therefore, you don't use :set, but :let (and :echo to list their current value).

The sigil in front of the variable determines its scope. g: means global; those usually need to be set before the plugin is loaded, i.e. by placing

let g:javascript_conceal = 1

into your ~/.vimrc (and restarting Vim).

As some configuration applies only to certain filetypes, these use the b: prefix. Setting them in ~/.vimrc would just apply them to the first opened buffer, which is not what you want. Instead, you need to hook into the filetype detection mechanism.

If you only want to enable a config option for certain filetypes, use :let b:var = value instead, and put the corresponding commands into ~/.vim/after/ftplugin/{filetype}.vim, where {filetype} is the actual filetype (e.g. javascript). (This requires that you have :filetype plugin on; use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/{filetype}.vim.)

Alternatively, you could define an :autocmd FileType {filetype} setlocal option=value directly in your ~/.vimrc, but this tends to become unwieldy once you have many customizations.

autocmd Filetype javascript let b:javascript_fold = 1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .