180

What's the difference between let and set in the vim editor?

I've always wondered why both of them exist?

Also, I'd be interested to hear its historical background.

7
  • 18
    Did you try :help set and :help let and read the manual?
    – hochl
    Apr 3, 2012 at 9:09
  • 7
    yes I did that, but I still don't get why set has to exist, because let does the same things. Apr 3, 2012 at 9:18
  • 1
    @DavidHalter: let does not do the same things. There are things you cannot achieve with set that are possible with let, see my answer. Apr 3, 2012 at 9:56
  • 6
    No one has mentioned setlocal yet... they are equivalent to &l:* variables. Apr 3, 2012 at 12:19
  • 1
    @VictorZamanian: for boolean settings, it's arguably easier to just set noic or set ic. As far as I know, set is also compatible with vi which let, as I assume, is not. Feb 16, 2017 at 5:48

5 Answers 5

191

:set is for setting options, :let for assigning a value to a variable.

It happens that the value for an option is linked to the name of the option prepended by a & (the &option-name construct then behaves very similar to "ordinary" variables). So, the following are equivalent:

:set  tw=40
:let &tw=40

But, for example, assigning 50 to the global variable foo (:let g:foo=50) cannot be achieved with a :set command (because g:foo is a variable and not an option).

Some options are boolean like. When setting these, no value is needed (as in :set noic and the opposite :set ic).

26

:set only works with options, and sehe's answer showcases some good usage examples.

:let on the other hand can do almost everything that :set can do, plus more. It can assign a value to

  • a variable, e.g. let vi = 'vim'
  • an option, e.g. let &tw = 40
  • a register, e.g. let @a = $HOME . '/vimfiles'
  • an environment variable, e.g. let $NOTHING = 'NOTHING'

Another major difference is that the right hand side of :let is an expression, meaning you can do things like string concatenation (as seen in my register example above) and arithmetic operations (e.g. let &tw = 40 + 60). This also means that you have to quote the value if it's a string. :set on the other hand reads the value verbatim.

It's easier to use :set with options even though :let can also do most of it, Here are some comparison using sehe's examples ("n/a" means no way to do it with :let)

  • :verbose set vs n/a (don't think there's another way to list all options)
  • :set tw=40 vs :let &tw = 40 (yes, you can use the same shorthand in let too)
  • :set wrap& vs n/a
  • :set nowrap vs :let &wrap = 0 (for boolean options, 0 is false and 1 is true)
  • :set wrap! vs :let &wrap = !&wrap

A few more examples

  • print the value of an option: :set formatoptions? vs :echo &formatoptions (let doesn't print values, unlike set)
  • assigning to multiple options at the same time:

    :set et sw=4 sts=4 
    

    vs

    :let [&et, &sw, &sts] = [0, 4, 4]
    
  • set global option: setglobal et vs let &g:et = 1

  • set local option: setlocal et vs let &l:et = 1

See :h :set and :h :let for more details


tl;dr

:set only works with options but the syntax is much simpler. :let works with not just options but also variables, registers, and environment variables. Unlike :set, the right hand side of :let is an expression.

25

Set is a more user-friendly interface specialized for options

E.g.

:verbose set

to display all options in effect.

:set tw=40

Will work as a shorthand for set textwidth=40

:set wrap&

Will set the default value for option wrap

:set nowrap

Will unset the option

:set wrap!

Will toggle the option

Most importantly,

:setTab # to get tab completion!

Few of the above can (easily) be achieved with let.

3
  • re: :set nowrap, options.txt (:help set-option) says: :se[t] {option} Toggle option: set, switch it on. Number option: show value. String option: show value. This doesn't agree with what you have here, when you say 'will set the default value for option wrap'. ?? Jul 13, 2020 at 22:24
  • @Ellipticalview I'm a little bit confused. You are quoting parts of the documentation. If you read the whole thing you'll find it agrees with all my cases. [Note: the no-prefixed options can be confusing. Also note option& has a trailing & that you may have missed.]
    – sehe
    Jul 13, 2020 at 23:47
  • 1
    Very late upvote for saying set is more user-friendly instead of saying set is restricted. For me that was the missing piece. Mar 16, 2022 at 19:29
10

Expanding on what people have written about :let, I've noticed that it can be used to assign a value in a variable to an option, something :set can't do. For example, this function uses let to assign the value in the global variable orig_tw to the textwidthoption:

" Toggle Autowrap
" Default of 72 but can be overridden by tw settings in other vimrc files
let g:orig_tw = 72
function Toggle_autowrap_mode()
    if &textwidth == 0
        " Must use let instead of set here in order for g:orig_tw to be
        " evaluated properly
        let &textwidth = g:orig_tw
        echo "Autowrap mode on tw=" . &textwidth
    else
        let g:orig_tw = &textwidth
        set textwidth=0
        echo "Autowrap mode off tw=" . &textwidth
    endif
endfunction

noremap _A :call Toggle_autowrap_mode()<CR>
1
  • Summary: let allows for assigning a variable to an option: let &textwidth = g:orig_tw and vice versa. Oct 6, 2014 at 10:58
3

It's very simple.
As people have said set is for options and works better because of the limitation. Also set is the historical command that all versions of vi use to set their options. Most (all?) other versions of vi don't have let.

But possibly most important is that set works on all versions of vim, the let command can be omitted when you compile vim. The standard tiny and small builds do this.

If it's missing let gives you the error:
E319: Sorry, the command is not available in this version

Note: if and endif are not implemented either in vim.tiny but in this case the commands do not give an error, instead everything between the two commands is skipped INCLUDING else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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