Programming

Vim : let과 set의 차이점은 무엇입니까?

procodes 2020. 6. 24. 21:02
반응형

Vim : let과 set의 차이점은 무엇입니까?


vim 편집기 let차이점은 무엇입니까 set?

나는 항상 왜 둘 다 존재하는지 궁금했습니다.

또한 역사적 배경을 듣고 싶습니다.


:set설정하기위한 옵션을 , :let(A)에 값을 할당하는 변수 .

옵션의 값은 앞에 붙은 옵션의 이름에 연결됩니다 &(이 &option-name구문은 "일반적인"변수와 매우 유사하게 동작합니다). 따라서 다음은 동일합니다.

:set  tw=40
:let &tw=40

그러나 예를 들어 전역 변수 foo ( :let g:foo=50)에 50을 할당하는 것은 :set명령 으로 수행 할 수 없습니다 (g : foo는 변수이며 옵션이 아니기 때문).

일부 옵션은 부울과 같습니다. 이들을 설정할 때 값이 필요하지 않습니다 ( :set noic및 반대 :set ic).


옵션은 옵션에 특화된보다 사용자 친화적 인 인터페이스입니다

예 :

:verbose set

유효한 모든 옵션을 표시합니다.

:set tw=40

속기의 역할을합니다 set textwidth=40

:set wrap&

옵션 기본값을 설정합니다wrap

:set nowrap

옵션을 해제합니다

:set wrap!

전환 옵션을

가장 중요한 것은,

:setTab # 탭 완성을 얻으려면!

로 쉽게 위의 몇 가지를 달성 할 수 있습니다 let.


사람들이 작성한 것에 대해 확장 :let하면서 변수의 값을 옵션에 할당하는 데 사용할 수 있지만 무언가 :set할 수 없다는 것을 알았습니다 . 예를 들어,이 기능을 사용하여 let전역 변수의 값을 할당 orig_tw받는 textwidth옵션 :

" 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>

:set옵션으로 만 작동하며 sehe의 답변 은 좋은 사용법 예제를 보여줍니다.

:let반면에 할 수있는 거의 모든 것을 :set할 수 있습니다. 값을 할당 할 수 있습니다

  • 변수, 예를 들어 let vi = 'vim'
  • 옵션, 예 : let &tw = 40
  • 레지스터, 예. let @a = $HOME . '/vimfiles'
  • 환경 변수, 예. let $NOTHING = 'NOTHING'

또 다른 주요 차이점은 오른쪽 :let표현식 이라는 점입니다. 즉, 위의 레지스터 예제에서 볼 수 있듯이 문자열 연결 및 산술 연산 (예 :)과 같은 작업을 수행 할 수 있습니다 let &tw = 40 + 60. 또한 문자열 인 경우 값을 인용해야합니다. :set반면에 값을 그대로 읽습니다.

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.


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.

참고URL : https://stackoverflow.com/questions/9990219/vim-whats-the-difference-between-let-and-set

반응형