History Report a problem
Article Edit this page Discussion

Write your own Vim function

From Vim Tips Wiki

Jump to: navigation, search
 

Tip 32Previous TipNext Tip

Created: March 7, 2001 Complexity: advanced Author: slimzhao Minimum version: 5.7 Karma: 169/58 Imported from: Tip#32


Here are some quick rules for writing vim-script, compared with the C language and the bash shell.

Contents

[edit] A function name must start with an uppercase letter.
hex2dec is invalid
Hex2dec is valid

C and bash allow both lowercase and uppercase.

[edit] How to reference function parameters.
fu! Hex2dec(var1, var2)
  let str=a:var1
  let str2=a:var2

You must prefix a parameter name with "a:" (argument).

A function cannot change a parameter (let a:var1=1 is invalid).

In C, "a:" is not used, and a parameter is writable.

See :help a:1

[edit] How to implement a variable number of parameters.
fu! Hex2dec(fixedparam, ...)
  • a:0 is the number of extra parameters "..." used to call the function.
  • a:1 is the first extra parameter.

For example:

:Hex2dec("asdf", 4,5,6)

gives a:0 = 3, a:1 = 4, a:2 = 5, a:3 = 6.

See :help a:0 and also :help a:000

[edit] Where is the vim-library?

Vim has its own function library, see :help functions

[edit] Can I use the += or ++ operators?
  • += exists in Vim since version 7.0
  • ++ does not
[edit] How to use a variable.
let var1=value
let var2=var1

Same as C, except you must use the let keyword.

See :help :let, and :help expression

[edit] Can any ex-mode command be used in a function?

Yes — each line can be an ex command.

[edit] Can a function call itself (recurse)?

Yes — but be careful to avoid an infinite loop.

[edit] Can a function call another function?

Yes — just like C.

[edit] Must I compile the function?

No, you needn't and you can't.

In Vim, enter the following command to source your script:

:so filename_containing_script

Now you can call the function.

If wanted, the ':so' (source) statement can be in your vimrc file.

[edit] Does Vim have integer or float or other data types?

No. Like Perl, the type of a Vim variable is determined by its context.

let a=1
let a=a."asdf"
echo a    (displays '1asdf')
let a=1
let a=a+2
echo a    (displays '3')
[edit] Must I append a ';' to every statement?

No, never do that.

If you want to combine several statements in a single line, use '|'.

';' is required in C, and optional in bash for each statement in a line.

[edit] References

See also

[edit] Comments


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Terminator Wiki
Legend of Zelda Wiki
Flash Gordon
Everquest II Wiki
Yo-Yo Wiki
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Write your own Vim function"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation