MyCounter()
let s:counter = s:counter + 1
endfunction
else
function MyCounter()
let s:counter = s:counter - 1
endfunction
endif
endfunction
This defines the MyCounter() function either for counting up or counting down
when calling StartCounting(). It doesn't matter from where StartCounting() is
called, the s:counter variable will be accessible in MyCounter().
When the same script is sourced again it will use the same script variables.
They will remain valid as long as Vim is running. This can be used to
maintain a counter: >
if !exists("s:counter")
let s:counter = 1
echo "script executed for the first time"
else
let s:counter = s:counter + 1
echo "script executed " .. s:counter .. " times now"
endif
Note that this means that filetype plugins don't get a different set of script
variables for each buffer. Use local buffer variables instead |b:var|.
PREDEFINED VIM VARIABLES *vim-variable* *v:var* *v:*
*E963*
The alphabetic list of all builtin variables and details are in a separate
help file: |vvars|.
==============================================================================
4. Builtin Functions *vim-function* *functions*
The Vimscript subsystem (referred to as "eval" internally) provides builtin
functions. Scripts can also define |user-function|s.
See |function-list| to browse functions by topic.
The alphabetic list of all builtin functions and details are in a separate
help file: |builtin-functions|.
==============================================================================
5. Defining functions *user-function*
New functions can be defined. These can be called just like builtin
functions. The function takes arguments, executes a sequence of Ex commands
and can return a value.
You can find most information about defining functions in |userfunc.txt|.
==============================================================================
6. Curly braces names *curly-braces-names*
In most places where you can use a variable, you can use a "curly braces name"
variable. This is a regular variable name with one or more expressions
wrapped in braces {} like this: >
my_{adjective}_variable
When Vim encounters this, it evaluates the expression inside the braces, puts
that in place of the expression, and re-interprets the whole as a variable
name. So in the above example, if the variable "adjective" was set to
"noisy", then the reference would be to "my_noisy_variable", whereas if
"adjective" was set to "quiet", then it would be to "my_quiet_variable".
One application for this is to create a set of variables governed by an option
value. For example, the statement >
echo my_{&background}_message
would output the contents of "my_dark_message" or "my_light_message" depending
on the current value of 'background'.
You can use multiple brace pairs: >
echo my_{adverb}_{adjective}_message
..or even nest them: >
echo my_{ad{end_of_word}}_message
where "end_of_word" is either "verb" or "jective".
However, the expression inside the braces must evaluate to a valid single
variable name, e.g. this is invalid: >
:let foo='a + b'
:echo c{foo}d
.. since the result of expansion is "ca + bd", which is not a variable name.
*curly-braces-function-names*
You can call and define functions by an evaluated name in a similar way.
Example: >
:let func_end='whizz'
:call my_func_{func_end}(parameter)
This would call the function "my_func_whizz(parameter)".
This does NOT work: >
:let i = 3
:let @{i} = '' " error
:echo @{i} " error
==============================================================================
7. Commands *expression-commands*
:let {var-name} = {expr1} *:let* *E18*
Set internal variable {var-name} to the result of the
expression {expr1}. The variable will get the type
from the {expr}. If {var-name} didn't exist yet, it
is created.
:let {var-name}[{idx}] = {expr1} *E689*
Set a list item to the result