Home Explore Blog CI



neovim

6th chunk of `runtime/doc/usr_41.txt`
142a2f6973a9af4006f3e51eea129845b052641bb27e775a0000000100000fa0
 seconds.

Even more looping can be done with the ":for" command, see below in |41.8|.

==============================================================================
*41.5*	Executing an expression

So far the commands in the script were executed by Vim directly.  The
":execute" command allows executing the result of an expression.  This is a
very powerful way to build commands and execute them.
   An example is to jump to a tag, which is contained in a variable: >

	:execute "tag " .. tag_name

The ".." is used to concatenate the string "tag " with the value of variable
"tag_name".  Suppose "tag_name" has the value "get_cmd", then the command that
will be executed is: >

	:tag get_cmd

The ":execute" command can only execute colon commands.  The ":normal" command
executes Normal mode commands.  However, its argument is not an expression but
the literal command characters.  Example: >

	:normal gg=G

This jumps to the first line and formats all lines with the "=" operator.
   To make ":normal" work with an expression, combine ":execute" with it.
Example: >

	:execute "normal " .. normal_commands

The variable "normal_commands" must contain the Normal mode commands.
   Make sure that the argument for ":normal" is a complete command.  Otherwise
Vim will run into the end of the argument and abort the command.  For example,
if you start Insert mode, you must leave Insert mode as well.  This works: >

	:execute "normal Inew text \<Esc>"

This inserts "new text " in the current line.  Notice the use of the special
key "\<Esc>".  This avoids having to enter a real <Esc> character in your
script.

If you don't want to execute a string but evaluate it to get its expression
value, you can use the eval() function: >

	:let optname = "path"
	:let optval = eval('&' .. optname)

A "&" character is prepended to "path", thus the argument to eval() is
"&path".  The result will then be the value of the 'path' option.
   The same thing can be done with: >
	:exe 'let optval = &' .. optname

==============================================================================
*41.6*	Using functions

Vim defines many functions and provides a large amount of functionality that
way.  A few examples will be given in this section.  You can find the whole
list below: |function-list|.

A function is called with the ":call" command.  The parameters are passed in
between parentheses separated by commas.  Example: >

	:call search("Date: ", "W")

This calls the search() function, with arguments "Date: " and "W".  The
search() function uses its first argument as a search pattern and the second
one as flags.  The "W" flag means the search doesn't wrap around the end of
the file.

A function can be called in an expression.  Example: >

	:let line = getline(".")
	:let repl = substitute(line, '\a', "*", "g")
	:call setline(".", repl)

The getline() function obtains a line from the current buffer.  Its argument
is a specification of the line number.  In this case "." is used, which means
the line where the cursor is.
   The substitute() function does something similar to the ":substitute"
command.  The first argument is the string on which to perform the
substitution.  The second argument is the pattern, the third the replacement
string.  Finally, the last arguments are the flags.
   The setline() function sets the line, specified by the first argument, to a
new string, the second argument.  In this example the line under the cursor is
replaced with the result of the substitute().  Thus the effect of the three
statements is equal to: >

	:substitute/\a/*/g

Using the functions becomes interesting when you do more work before and
after the substitute() call.


FUNCTIONS						*function-list*

There are many functions.  We will mention them here, grouped by what they are
used for.  You can find an alphabetical list here: |builtin-function-details|.
Use CTRL-] on the function name to jump to detailed help on it.

String manipulation:					*string-functions*
	nr2char()		get a

Title: Executing Expressions, Using Functions, and Available Functions
Summary
This section covers executing expressions and using functions in Vim scripts. It explains how to use ':execute' to run dynamically constructed commands, including combining it with ':normal' for Normal mode commands. It introduces the eval() function for evaluating a string as an expression. The section then explores Vim's built-in functions, particularly focusing on the ':call' command and examples of functions like search(), getline(), substitute(), and setline(). Finally, it provides a comprehensive overview of function categories, including string manipulation functions.