# Learning the vi Editor/Vim/VimL Script language

* Learning the vi Editor:
* Getting acquainted —
* Basic tasks —
* Making your work easier —
* Advanced tasks —
* Details —
* Vi clones
* (Vim –
* Basic navigation –
* Modes –
* Tips and Tricks –
* Useful things for programmers to know –
* Enhancing Vim –
* Vim on Windows –
* VimL Script language,
* Vile,
* BB vi) —
* vi Reference
Vim script (aka Vimscript, or VimL) is a full feature scripting language, meaning it can solve almost any text processing problem.

## Statements
### Assignment

To set a variable use:
To set a built-in setting you have two options:

## Data types

There are five datatypes:

### Number

A 32 bit signed integer.

### String

A NULL terminated string of 8-bit unsigned characters (bytes). Strings can be created by either ‘'’ or ‘"’ quotes. When using strings quoted with the double quote ‘"’ the text is interpreted i.e. "\n" becomes a new line while strings quoted with a single quote ‘'’ are not interpreted, i.e. '\n' means just that, a backslash and an n. The following two strings are identical:
Any other datatype can be converted into a string using the string function.

### Funcref

A reference to a function. A Funcref can be created from a string by the use of the function function.

### List

An ordered sequence of items.
A list can be created from a string by the use of the split function.

### Dictionary

An associative, unordered array: Each entry has a key and a value.

### Objects

VIM also supports object oriented programming by combining Funcref and Dictionary to an Object:
for more information see Object oriented programming

## Control Structures

The existence of control structures is the main difference between vi's ex commands and vim's scripting language. They make the difference between a simple command set (vi) and a full features script language (vim).

### loop
#### while
#### for

For loops are available from vim 7 onwards. They iterate over List or Directory structures.

### exceptions
## Subprograms
### Simple Subprograms

Like most Shell-Languages all subprograms are stored in separate files which you load either with the source or runtime command. The difference lies in the use of a search path. runtime uses a search path and allows wildcards to find the sub-program while source needs the full path. The following commands do the same - provided that "~/vimfiles" is part of your runtime search path:
For both commands need to add the .vim extension. Since runtime supports both a search path and wildcards more than one match is possible. If you want runtime to load all the matches - and not just the first hit - use runtime!.

### Functions

New with vim 7 is the autoload option for functions. If you name a function Filename#Functionname or Directory#Filename#Functionname then the function will be automatically loaded on first call. The file containing the function must be placed in one of the "autoload" runtime directories and be named "Filename.vim" or "Directory/Filename.vim". This option is especially useful for functions which you don't always need on in Object oriented programming.

### Commands

Command are often used as shortcut for functions and subprograms:

## Object oriented programming

Vim 7 now allows object oriented programming. However, in order to make it real you need to combine several features, namely Dictionaries, Funcrefs and the new function autoload.
The following example class is taken from the gnat compiler plugin for vim. The actual functions implementations have been removed as they are not needed to understand the concept. If you like to have a look at the full version you can download the plugin from vim.org site.

### Step by Step walk-through

We add our new class to a autoload script. That way the class is available when and only when needed:
Each function we define need to be defined with the "dict" attribute. Apart from that they are just normal scripting functions.
The most important step is the composition of the object. In most OO languages this happens automatically - But with vim we have to do this ourselves. For best flexibility the use of a so called constructor function is suggested. The constructor is not marked with "dict":
The constructor creates a dictionary which assigns all the object functions to one element of the dictionary:
We optionally can now add data entries to our object:
If needed additional modifications to the object are also possible. At this stage you can already use the OO-way:
The last operation of the constructor is the return of the newly created object.
It is also possible to defined additional non dict functions. These functions are the equivalent to the "static" or "class" methods of other OO languages.
* Learning the vi Editor:
* Getting acquainted —
* Basic tasks —
* Making your work easier —
* Advanced tasks —
* Details —
* Vi clones
* (Vim –
* Basic navigation –
* Modes –
* Tips and Tricks –
* Useful things for programmers to know –
* Enhancing Vim –
* Vim on Windows –
* VimL Script language,
* Vile,
* BB vi) —
* vi Reference