2021-04-17 14:13:44 +00:00
# Ch26. Vimscript Variable Scopes
2021-01-01 17:56:00 +00:00
Before diving into Vimscript functions, let's learn about the different sources and scopes of Vim variables.
2021-05-29 15:17:31 +00:00
## Mutable and Immutable Variables
2021-01-01 17:56:00 +00:00
You can assign a value to a variable in Vim with `let` :
```
let pancake = "pancake"
```
Later you can call that variable any time.
```
echo pancake
" returns "pancake"
```
`let` is mutable, meaning you can change the value at any time in the future.
```
2021-01-25 21:34:53 +00:00
let pancake = "pancake"
2021-01-01 17:56:00 +00:00
let pancake = "not waffles"
echo pancake
" returns "not waffles"
```
Notice that when you want to change the value of a set variable, you still need to use `let` .
```
let beverage = "milk"
beverage = "orange juice"
" throws an error
```
2021-02-10 20:38:20 +00:00
You can define an immutable variable with `const` . Being immutable, once a variable value is assigned, you cannot reassign it with a different value.
2021-01-01 17:56:00 +00:00
```
const waffle = "waffle"
const waffle = "pancake"
2021-01-25 21:34:53 +00:00
" throws an error
2021-01-01 17:56:00 +00:00
```
2021-01-25 21:34:53 +00:00
## Variable Sources
There are three sources for variables: environment variable, option variable, and register variable.
2021-01-01 17:56:00 +00:00
2021-01-01 17:58:51 +00:00
### Environment Variable
2021-01-01 17:56:00 +00:00
2021-01-25 21:34:53 +00:00
Vim can access your terminal environment variable. For example, if you have the `SHELL` environment variable available in your terminal, you can access it from Vim with:
2021-01-01 17:56:00 +00:00
```
echo $SHELL
" returns $SHELL value. In my case, it returns /bin/bash
```
2021-01-25 21:34:53 +00:00
### Option Variable
2021-01-01 17:56:00 +00:00
2021-01-25 21:34:53 +00:00
You can access Vim options with `&` (these are the settings you access with `set` ).
For example, to see what background Vim uses, you can run:
2021-01-01 17:56:00 +00:00
```
echo & background
" returns either "light" or "dark"
```
Alternatively, you can always run `set background?` to see the value of the `background` option.
2021-01-25 21:34:53 +00:00
### Register Variable
You can access Vim registers (Ch. 08) with `@` .
2021-01-01 17:56:00 +00:00
2021-01-25 21:34:53 +00:00
Suppose the value "chocolate" is already saved in register a. To access it, you can use `@a` . You can also update it with `let` .
2021-01-01 17:56:00 +00:00
```
echo @a
" returns chocolate
let @a .= " donut"
echo @a
" returns "chocolate donut"
```
2021-01-25 21:34:53 +00:00
Now when you paste from register `a` (`"ap`), it will return "chocolate donut". The operator `.=` concatenates two strings. The expression `let @a .= " donut"` is the same as `let @a = @a . " donut"`
2021-01-01 17:56:00 +00:00
## Variable Scopes
2021-01-25 21:34:53 +00:00
There are 9 different variable scopes in Vim. You can recognize them from their prepended letter:
2021-01-01 17:56:00 +00:00
```
g: Global variable
{nothing} Global variable
b: Buffer-local variable
w: Window-local variable
t: Tab-local variable
s: Sourced Vimscript variable
2021-01-25 21:34:53 +00:00
l: Function local variable
2021-01-01 17:56:00 +00:00
a: Function formal parameter variable
v: Built-in Vim variable
```
2021-05-29 15:17:31 +00:00
### Global Variable
2021-01-01 17:56:00 +00:00
When you are declaring a "regular" variable:
```
let pancake = "pancake"
```
`pancake` is actually a global variable. When you define a global variable, you can call them from anywhere.
Prepending `g:` to a variable also creates a global variable.
```
let g:waffle = "waffle"
```
2021-01-25 21:34:53 +00:00
In this case both `pancake` and `g:waffle` have the same scope. You can call each of them with or without `g:` .
2021-01-01 17:56:00 +00:00
```
echo pancake
" returns "pancake"
echo g:pancake
2021-04-16 17:54:19 +00:00
" returns "pancake"
2021-01-01 17:56:00 +00:00
echo waffle
" returns "waffle"
echo g:waffle
" returns "waffle"
```
2021-01-25 21:34:53 +00:00
### Buffer Variable
2021-01-01 17:56:00 +00:00
2021-01-25 21:34:53 +00:00
A variable preceded with `b:` is a buffer variable. A buffer variable is a variable that is local to the current buffer (Ch. 02). If you have multiple buffers open, each buffer will have their own separate list of buffer variables.
2021-01-01 17:56:00 +00:00
In buffer 1:
```
const b:donut = "chocolate donut"
```
In buffer 2:
```
const b:donut = "blueberry donut"
```
2021-01-25 21:34:53 +00:00
If you run `echo b:donut` from buffer 1, it will return "chocolate donut". If you run it from buffer 2, it will return "blueberry donut".
2021-01-01 17:56:00 +00:00
On the side note, Vim has a *special* buffer variable `b:changedtick` that keeps track of all the changes done to the current buffer.
1. Run `echo b:changedtick` and note the number it returns..
2. Make changes in Vim.
3. Run `echo b:changedtick` again and note the number it now returns.
2021-01-25 21:34:53 +00:00
### Window Variable
2021-01-01 17:56:00 +00:00
A variable preceded with `w:` is a window variable. It exists only in that window.
In window 1:
```
const w:donut = "chocolate donut"
```
In window 2:
```
const w:donut = "raspberry donut"
```
On each window, you can call `echo w:donut` to get unique values.
2021-01-25 21:34:53 +00:00
### Tab Variable
2021-01-01 17:56:00 +00:00
A variable preceded with `t:` is a tab variable. It exists only in that tab.
In tab 1:
```
const t:donut = "chocolate donut"
```
In tab 2:
```
const t:donut = "blackberry donut"
```
On each tab, you can call `echo t:donut` to get unique values.
2021-05-29 15:17:31 +00:00
### Script Variable
2021-01-01 17:56:00 +00:00
2021-01-25 21:34:53 +00:00
A variable preceded with `s:` is a script variable. These variables can only be accessed from inside that script.
2021-01-01 17:56:00 +00:00
2021-01-25 21:34:53 +00:00
If you have an arbitrary file `dozen.vim` and inside it you have:
2021-01-01 17:56:00 +00:00
```
let s:dozen = 12
function Consume()
let s:dozen -= 1
echo s:dozen " is left"
endfunction
```
Source the file with `:source dozen.vim` . Now call the `Consume` function:
```
:call Consume()
" returns "11 is left"
:call Consume()
" returns "10 is left"
:echo s:dozen
" Undefined variable error
```
2021-01-25 21:34:53 +00:00
When you call `Consume` , you see it decrements the `s:dozen` value as expected. When you try to get `s:dozen` value directly, Vim won't find it because you are out of scope. `s:dozen` is only accessible from inside `dozen.vim` .
2021-01-01 17:56:00 +00:00
2021-01-25 21:34:53 +00:00
Each time you source the `dozen.vim` file, it resets the `s:dozen` counter. If you are in the middle of decrementing `s:dozen` value and you run `:source dozen.vim` , the counter resets back to 12. This can be a problem for unsuspecting users. To fix this issue, refactor the code:
2021-01-01 17:56:00 +00:00
```
if !exists("s:dozen")
let s:dozen = 12
endif
function Consume()
let s:dozen -= 1
echo s:dozen
endfunction
```
2021-01-25 21:34:53 +00:00
Now when you source `dozen.vim` while in the middle of decrementing, Vim reads `!exists("s:dozen")` , finds that it is true, and doesn't reset the value back to 12.
2021-01-01 17:56:00 +00:00
2021-05-29 15:17:31 +00:00
### Function Local and Function Formal Parameter Variable
2021-01-01 17:56:00 +00:00
2021-01-25 21:34:53 +00:00
Both the function local variable (`l:`) and the function formal variable (`a:`) will be covered in the next chapter.
2021-01-01 17:56:00 +00:00
2021-01-25 21:34:53 +00:00
### Built-in Vim Variables
2021-01-01 17:56:00 +00:00
2021-01-25 21:34:53 +00:00
A variable prepended with `v:` is a special built-in Vim variable. You cannot define these variables. You have seen some of them already.
2021-01-01 17:56:00 +00:00
- `v:version` tells you what Vim version you are using.
- `v:key` contains the current item value when iterating through a dictionary.
- `v:val` contains the current item value when running a `map()` or `filter()` operation.
- `v:true` , `v:false` , `v:null` , and `v:none` are special data types.
There are other variables. For a list of Vim built-in variables, check out `:h vim-variable` or `:h v:` .
2021-05-29 15:17:31 +00:00
## Using Vim Variable Scopes the Smart Way
2021-01-01 17:56:00 +00:00
2021-01-25 21:34:53 +00:00
Being able to quickly access environment, option, and register variables give you a broad flexibility to customize your editor and terminal environment. You also learned that Vim has 9 different variable scopes, each existing under a certain constraints. You can take advantage of these unique variable types to decouple your program.
You made it this far. You learned about data types, means of combinations, and variable scopes. Only one thing is left: functions.