In Vim, what are settings/commands that that begin with a prefix (b:, g:)?
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.
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