那么,let和set的区别是什么呢?

时间:2021-02-05 13:25:24

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

在vim编辑器中let和set的区别是什么?

I've always wondered why both of them exist?

我一直想知道为什么它们都存在?

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

另外,我很想听听它的历史背景。

5 个解决方案

#1


108  

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

:set用于设置选项,例如:为变量赋值。

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).

但是,例如,将50分配给全局变量foo(:让g:foo=50)不能用一个:set命令来实现(因为g:foo是一个变量,而不是一个选项)。

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

一些选项是布尔型的。在设置这些值时,不需要任何值(如:set noic和相反的:set ic)。

#2


15  

Set is a more user-friendly interface specialized for options

Set是一个更友好的界面,专门用于选项。

E.g.

如。

:verbose set

to display all options in effect.

显示所有选项的效果。

:set tw=40

Will work as a shorthand for set textwidth=40

是否可以作为设置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!

:setTab #以获得标签完成!

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

上述的很少能够(很容易地)实现。

#3


7  

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:

详述人们所写的内容:让我注意到,它可以用来为一个变量赋值给一个选项:set不能做。例如,该函数使用let将全局变量orig_tw中的值赋给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>

#4


2  

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

:set only works with options, and sehe's answer showcase some good use examples。

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

:让另一只手可以做几乎所有的事情:set可以做,也可以做更多。它可以赋值给。

  • a variable, e.g. let vi = 'vim'
  • 变量,例如,vi = 'vim'
  • an option, e.g. let &tw = 40
  • 一个选项,例如:let &tw = 40。
  • a register, e.g. let @a = $HOME . '/vimfiles'
  • 注册,例如:让@a = $HOME。' / vimfiles '
  • an environment variable, e.g. let $NOTHING = 'NOTHING'
  • 环境变量,例如:$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.

另一个主要的区别是:let是一个表达式,意思是你可以做一些事情,比如字符串连接(如上面的例子所示)和算术运算(例如:let &tw = 40 + 60)。这也意味着如果它是字符串,就必须引用它的值。:另一只手按逐字读值。

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)

它更容易使用:设置选项,即使:我们也可以做大部分的事情,这里有一些比较使用sehe的例子(“n/a”表示没有办法用:let)

  • :verbose set vs n/a (don't think there's another way to list all options)
  • :verbose set对n/a(不要认为有另一种方式列出所有选项)
  • :set tw=40 vs :let &tw = 40 (yes, you can use the same shorthand in let too)
  • :设置tw=40 vs:let &tw =40(是的,你也可以用同样的速记法)
  • :set wrap& vs n/a
  • :设置包装和n / a
  • :set nowrap vs :let &wrap = 0 (for boolean options, 0 is false and 1 is true)
  • :设置nowrap vs:let &wrap = 0(对于布尔选项,0为假,1为真)
  • :set wrap! vs :let &wrap = !&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)
  • 打印选项的值::设置formatoptions?vs:echo &formatoptions(不像集合那样打印值)
  • assigning to multiple options at the same time:

    同时分配多个选项:

    :set et sw=4 sts=4 
    

    vs

    vs

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

    设置全局选项:setglobal et vs let &g:et = 1。

  • set local option: setlocal et vs let &l:et = 1
  • 设置本地选项:setlocal et vs let &l:et = 1。

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

h:设置和:h:让我们了解更多细节。


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.

:set只与选项一起工作,但是语法要简单得多。:让工作不仅包括选项,还包括变量、寄存器和环境变量。不像:集合,右手边的:让是一个表达式。

#5


1  

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.

很简单。正如人们所说的,set是用来选择的,并且因为有限制而更好地工作。还设置了所有版本的vi用来设置选项的历史命令。其他版本的vi是不允许的。

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.

但可能最重要的是,在vim的所有版本上都设置了这个集合,在编译vim时,let命令可以省略。标准的小型和小型的构建可以做到这一点。

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

如果它丢失了,请给出错误:E319:对不起,这个版本中没有这个命令。

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.

注意:如果和endif不是在vim中实现的。在这种情况下,命令不会产生错误,而是跳过了这两个命令之间的所有内容,包括其他命令。

#1


108  

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

:set用于设置选项,例如:为变量赋值。

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).

但是,例如,将50分配给全局变量foo(:让g:foo=50)不能用一个:set命令来实现(因为g:foo是一个变量,而不是一个选项)。

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

一些选项是布尔型的。在设置这些值时,不需要任何值(如:set noic和相反的:set ic)。

#2


15  

Set is a more user-friendly interface specialized for options

Set是一个更友好的界面,专门用于选项。

E.g.

如。

:verbose set

to display all options in effect.

显示所有选项的效果。

:set tw=40

Will work as a shorthand for set textwidth=40

是否可以作为设置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!

:setTab #以获得标签完成!

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

上述的很少能够(很容易地)实现。

#3


7  

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:

详述人们所写的内容:让我注意到,它可以用来为一个变量赋值给一个选项:set不能做。例如,该函数使用let将全局变量orig_tw中的值赋给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>

#4


2  

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

:set only works with options, and sehe's answer showcase some good use examples。

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

:让另一只手可以做几乎所有的事情:set可以做,也可以做更多。它可以赋值给。

  • a variable, e.g. let vi = 'vim'
  • 变量,例如,vi = 'vim'
  • an option, e.g. let &tw = 40
  • 一个选项,例如:let &tw = 40。
  • a register, e.g. let @a = $HOME . '/vimfiles'
  • 注册,例如:让@a = $HOME。' / vimfiles '
  • an environment variable, e.g. let $NOTHING = 'NOTHING'
  • 环境变量,例如:$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.

另一个主要的区别是:let是一个表达式,意思是你可以做一些事情,比如字符串连接(如上面的例子所示)和算术运算(例如:let &tw = 40 + 60)。这也意味着如果它是字符串,就必须引用它的值。:另一只手按逐字读值。

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)

它更容易使用:设置选项,即使:我们也可以做大部分的事情,这里有一些比较使用sehe的例子(“n/a”表示没有办法用:let)

  • :verbose set vs n/a (don't think there's another way to list all options)
  • :verbose set对n/a(不要认为有另一种方式列出所有选项)
  • :set tw=40 vs :let &tw = 40 (yes, you can use the same shorthand in let too)
  • :设置tw=40 vs:let &tw =40(是的,你也可以用同样的速记法)
  • :set wrap& vs n/a
  • :设置包装和n / a
  • :set nowrap vs :let &wrap = 0 (for boolean options, 0 is false and 1 is true)
  • :设置nowrap vs:let &wrap = 0(对于布尔选项,0为假,1为真)
  • :set wrap! vs :let &wrap = !&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)
  • 打印选项的值::设置formatoptions?vs:echo &formatoptions(不像集合那样打印值)
  • assigning to multiple options at the same time:

    同时分配多个选项:

    :set et sw=4 sts=4 
    

    vs

    vs

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

    设置全局选项:setglobal et vs let &g:et = 1。

  • set local option: setlocal et vs let &l:et = 1
  • 设置本地选项:setlocal et vs let &l:et = 1。

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

h:设置和:h:让我们了解更多细节。


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.

:set只与选项一起工作,但是语法要简单得多。:让工作不仅包括选项,还包括变量、寄存器和环境变量。不像:集合,右手边的:让是一个表达式。

#5


1  

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.

很简单。正如人们所说的,set是用来选择的,并且因为有限制而更好地工作。还设置了所有版本的vi用来设置选项的历史命令。其他版本的vi是不允许的。

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.

但可能最重要的是,在vim的所有版本上都设置了这个集合,在编译vim时,let命令可以省略。标准的小型和小型的构建可以做到这一点。

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

如果它丢失了,请给出错误:E319:对不起,这个版本中没有这个命令。

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.

注意:如果和endif不是在vim中实现的。在这种情况下,命令不会产生错误,而是跳过了这两个命令之间的所有内容,包括其他命令。