|internal-variables|. The most often
used ones are:
b:name variable local to a buffer
w:name variable local to a window
g:name global variable (also in a function)
v:name variable predefined by Vim
DELETING VARIABLES
Variables take up memory and show up in the output of the ":let" command. To
delete a variable use the ":unlet" command. Example: >
:unlet s:count
This deletes the script-local variable "s:count" to free up the memory it
uses. If you are not sure if the variable exists, and don't want an error
message when it doesn't, append !: >
:unlet! s:count
When a script has been processed to the end, the local variables declared
there will not be deleted. Functions defined in the script can use them.
Example:
:if !exists("s:call_count")
: let s:call_count = 0
:endif
:let s:call_count = s:call_count + 1
:echo "called" s:call_count "times"
The "exists()" function checks if a variable has already been defined. Its
argument is the name of the variable you want to check. Not the variable
itself! If you would do this: >
:if !exists(s:call_count)
Then the value of s:call_count will be used as the name of the variable that
exists() checks. That's not what you want.
The exclamation mark ! negates a value. When the value was true, it
becomes false. When it was false, it becomes true. You can read it as "not".
Thus "if !exists()" can be read as "if not exists()".
What Vim calls true is anything that is not zero. Zero is false.
Note:
Vim automatically converts a string to a number when it is looking for
a number. When using a string that doesn't start with a digit the
resulting number is zero. Thus look out for this: >
:if "true"
< The "true" will be interpreted as a zero, thus as false!
STRING VARIABLES AND CONSTANTS
So far only numbers were used for the variable value. Strings can be used as
well. Numbers and strings are the basic types of variables that Vim supports.
The type is dynamic, it is set each time when assigning a value to the
variable with ":let". More about types in |41.8|.
To assign a string value to a variable, you need to use a string constant.
There are two types of these. First the string in double quotes: >
:let name = "peter"
:echo name
< peter ~
If you want to include a double quote inside the string, put a backslash in
front of it: >
:let name = "\"peter\""
:echo name
< "peter" ~
To avoid the need for a backslash, you can use a string in single quotes: >
:let name = '"peter"'
:echo name
< "peter" ~
Inside a single-quote string all the characters are as they are. Only the
single quote itself is special: you need to use two to get one. A backslash
is taken literally, thus you can't use it to change the meaning of the
character after it.
In double-quote strings it is possible to use special characters. Here are
a few useful ones:
\t <Tab>
\n <NL>, line break
\r <CR>, <Enter>
\e <Esc>
\b <BS>, backspace
\" "
\\ \, backslash
\<Esc> <Esc>
\<C-W> CTRL-W
The last two are just examples. The "\<name>" form can be used to include
the special key "name".
See |expr-quote| for the full list of special items in a string.
==============================================================================
*41.3* Expressions
Vim has a rich, yet simple way to handle expressions. You can read the
definition here: |expression-syntax|. Here we will show the most common
items.
The numbers, strings and variables mentioned above are expressions by
themselves. Thus everywhere an expression is expected, you can use a number,
string or variable. Other basic items in an expression are:
$NAME environment variable
&name option
@r register
Examples: >
:echo "The value of 'tabstop' is" &ts
:echo "Your home directory is" $HOME
:if @a > 5
The &name form can be used to save an option value, set it to a new value,
do something and restore the old value. Example: >
:let save_ic = &ic
:set noic
:/The Start/,$delete
:let &ic