tools/vim
Learn Vimscript The Hard Way - 20. Variable Scoping
seul chan
2020. 3. 25. 22:47
Variable Scoping
전 장 19. Variable에서 본 것처럼 Vimscript의 변수는 Python이나 Ruby 같은 동적 타입 언어와 거의 흡사하게 동작한다.
하지만 vimscript의 scoping
은 기대와 조금 다르게 동작한다.
:let b:hello = "world"
:echo b:hello
위 명령어를 실행해보면 정상적으로 world
가 반환된다.
이제 다른 버퍼를 열어서 (:tabnew
나 :new
) 다시 echo 명령어를 실행해보자.
:new
:echo b:hello
정의되지 않은 변수라는 에러가 반환된다.
E121: Undefined variable: b:hello
b:
prefix를 변수와 함께 사용하면 현재 버퍼에서만 local va Variable로 정의된다.
다른 scope들도 있지만 해당 장에서는 설명하지 않고 다른 장에서 설명할 예정이라고 함
Exercises
- Skim over the list of scopes in :help internal-variables. Don't worry if you don't know what some of them mean, just take a look and keep them in the back of your mind.
:let
으로 생성되고:unlet
으로 제거- scoping 목록은 다음과 같음
|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.
- 각 스코핑에 대한 자세한 설명이 나와있다. (skim하라고 하였으니 대충 훑어보었다)