I don't know much of the theory behind Vim. It looks as if we set some variables, and let other variables. What's the difference, and when is one required over the other?

Also, the solarized vim colorscheme says to let g:blabla... what is the significance of g: in this instance?


let assigns a value to a variable, and set assigns a value to one of Vim's internal options.

For example, you would use

:let mystring='Hello!'

to declare a new variable, but you would use

:set tabstop=4


:set expandtab

to set one of Vim's options.

See also: :help let, :help set.

g: simply signifies a global variable. There are several of these "variable namespaces"—from :help internal-variables:

        (nothing) In a function: local to a function; otherwise: global
|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.
| improve this answer | |
  • 10
    Do note that you can set vim's internal options using let by doing let &{option name}=value – EvergreenTree Apr 30 '15 at 16:51

:set is used for internal options and handles the types of the option. It allows things like inversion of boolean options (e.g. :set invpaste) adding and removing settings from a list (e.g. :set dictionary-=/tmp/myfile).

:let, on the other hand, allows access to a plethora of changeable things in vim, including regular variables, options, registers, etc. and the value being set is the result of evaluating an expression, instead of a simple value like with :set, which means :let gives you access to math, string manipulation, and functions.

For example, you can :let &dictionary=/tmp/myfile to set the option 'dictionary' to /tmp/myfile (thus modifying the same option shown above in the :set example)

You can set a global variable with :let g:myvar=42. The prefix you asked about provides scope. Doorknob's answer covers the scoping very well.

You can :let @a="Some text" to change the contents of register a to contain Some text, which you could later paste with "ap for example.

You can :let $PATH = $PATH . ":/home/me/bin" to append your own bin directory to the PATH environment variable, which is passed to any external commands executed by Vim (this works for any environment variable).

Although let has some pretty powerful abilities (like :let $MYFILE = @% to set an environment variable to the filename of the current buffer), it doesn't know that a setting is a boolean, so it doesn't offer nice accesses or error checking like :set does.

In the end, it's about using the best tool for the job. If you want to simply modify an option, :set is your tool. If you want to move information around between registers, options, variables, or base your value on the result of an expression, then :let is your tool.

Although you explicitly asked about Vim, another thing to note is that :set is more common in the vi/Vim families, where :let is less common (for example, nvi doesn't have a let command, and viemu doesn't provide access to registers via let).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.