David Roussov
David Roussov

Reputation: 195

How do you call a Vim built-in function?

In section 4 of the Vim reference manual, it lists a number of builtin functions:

4. Builtin Functions                                    *functions*

See |function-list| for a list grouped by what the function is used for.

(Use CTRL-] on the function name to jump to the full explanation.)

USAGE                           RESULT  DESCRIPTION     ~

abs({expr})                     Float or Number  absolute value of {expr}
acos({expr})                    Float   arc cosine of {expr}
add({object}, {item})           List/Blob   append {item} to {object}
and({expr}, {expr})             Number  bitwise AND
append({lnum}, {text})          Number  append {text} below line {lnum}

How do I call these functions? How do I for example get the absolute value of a number?

Upvotes: 2

Views: 1495

Answers (3)

romainl
romainl

Reputation: 196476

The reference manual is borderline useless without the foundations provided by the user manual.

Functions are introduced in chapter 41 of the user manual:

:help usr_41.txt

Upvotes: 1

Luc Hermitte
Luc Hermitte

Reputation: 32926

First, note there is no visible difference between user functions and built-in functions from a usage point of view.

Functions that return nothing are meant to be called with :call -- note, that they will still always return 0.

The other functions return expressions. These expressions can be

  • used to build more complex expressions
    • as they are passed around to other functions
    • as they concatenated, added, multiplied...
    • as they are used as index getline('.')[col('.')-1]
  • passed to vim ex-commands that could be used to
    • :echo an expression
    • assign an expression into a variable with :let
    • :execute another vim ex-command, .e.g :exe line('$')/2

Upvotes: 0

bk2204
bk2204

Reputation: 76409

You can call a built-in function like so, with the call command, or in an expression:

let x = abs(-2)
" or, for functions where you're not interested in the return value:
:call clearmatches()

Upvotes: 3

Related Questions