keeps on running, you can
interrupt it by pressing CTRL-C (CTRL-Break on MS-Windows).
The ":echo" command prints its arguments. In this case the string "count is"
and the value of the variable i. Since i is one, this will print:
count is 1 ~
Then there is the ":let i += 1" command. This does the same thing as
":let i = i + 1". This adds one to the variable i and assigns the new value
to the same variable.
The example was given to explain the commands, but would you really want to
make such a loop, it can be written much more compact: >
:for i in range(1, 4)
: echo "count is" i
:endfor
We won't explain how |:for| and |range()| work until later. Follow the links
if you are impatient.
FOUR KINDS OF NUMBERS
Numbers can be decimal, hexadecimal, octal or binary.
A hexadecimal number starts with "0x" or "0X". For example "0x1f" is decimal
31.
An octal number starts with "0o", "0O" or a zero and another digit. "0o17" is
decimal 15.
A binary number starts with "0b" or "0B". For example "0b101" is decimal 5.
A decimal number is just digits. Careful: don't put a zero before a decimal
number, it will be interpreted as an octal number!
The ":echo" command always prints decimal numbers. Example: >
:echo 0x7f 0o36
< 127 30 ~
A number is made negative with a minus sign. This also works for hexadecimal,
octal and binary numbers. A minus sign is also used for subtraction. Compare
this with the previous example: >
:echo 0x7f -0o36
< 97 ~
White space in an expression is ignored. However, it's recommended to use it
for separating items, to make the expression easier to read. For example, to
avoid the confusion with a negative number above, put a space between the
minus sign and the following number: >
:echo 0x7f - 0o36
==============================================================================
*41.2* Variables
A variable name consists of ASCII letters, digits and the underscore. It
cannot start with a digit. Valid variable names are:
counter
_aap3
very_long_variable_name_with_underscores
FuncLength
LENGTH
Invalid names are "foo.bar" and "6var".
These variables are global. To see a list of currently defined variables
use this command: >
:let
You can use global variables everywhere. This also means that when the
variable "count" is used in one script file, it might also be used in another
file. This leads to confusion at least, and real problems at worst. To avoid
this, you can use a variable local to a script file by prepending "s:". For
example, one script contains this code: >
:let s:count = 1
:while s:count < 5
: source other.vim
: let s:count += 1
:endwhile
Since "s:count" is local to this script, you can be sure that sourcing the
"other.vim" script will not change this variable. If "other.vim" also uses an
"s:count" variable, it will be a different copy, local to that script. More
about script-local variables here: |script-variable|.
There are more kinds of variables, see |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.