Home Explore Blog CI



neovim

5th chunk of `runtime/doc/usr_41.txt`
d68c6f8311090e9da8b4ee9f16e203ff6741d2de088f46da0000000100000fa7
 then "if", but without the need for an
extra ":endif".
   A useful example for your vimrc file is checking the 'term' option and
doing something depending upon its value: >

	:if &term == "xterm"
	:  " Do stuff for xterm
	:elseif &term == "vt100"
	:  " Do stuff for a vt100 terminal
	:else
	:  " Do something for other terminals
	:endif


LOGIC OPERATIONS

We already used some of them in the examples.  These are the most often used
ones:

	a == b		equal to
	a != b		not equal to
	a >  b		greater than
	a >= b		greater than or equal to
	a <  b		less than
	a <= b		less than or equal to

The result is one if the condition is met and zero otherwise.  An example: >

	:if v:version >= 700
	:  echo "congratulations"
	:else
	:  echo "you are using an old version, upgrade!"
	:endif

Here "v:version" is a variable defined by Vim, which has the value of the Vim
version.  600 is for version 6.0.  Version 6.1 has the value 601.  This is
very useful to write a script that works with multiple versions of Vim.
|v:version|

The logic operators work both for numbers and strings.  When comparing two
strings, the mathematical difference is used.  This compares byte values,
which may not be right for some languages.
   When comparing a string with a number, the string is first converted to a
number.  This is a bit tricky, because when a string doesn't look like a
number, the number zero is used.  Example: >

	:if 0 == "one"
	:  echo "yes"
	:endif

This will echo "yes", because "one" doesn't look like a number, thus it is
converted to the number zero.

For strings there are two more items:

	a =~ b		matches with
	a !~ b		does not match with

The left item "a" is used as a string.  The right item "b" is used as a
pattern, like what's used for searching.  Example: >

	:if str =~ " "
	:  echo "str contains a space"
	:endif
	:if str !~ '\.$'
	:  echo "str does not end in a full stop"
	:endif

Notice the use of a single-quote string for the pattern.  This is useful,
because backslashes would need to be doubled in a double-quote string and
patterns tend to contain many backslashes.

The 'ignorecase' option is used when comparing strings.  When you don't want
that, append "#" to match case and "?" to ignore case.  Thus "==?" compares
two strings to be equal while ignoring case.  And "!~#" checks if a pattern
doesn't match, also checking the case of letters.  For the full table see
|expr-==|.


MORE LOOPING

The ":while" command was already mentioned.  Two more statements can be used
in between the ":while" and the ":endwhile":

	:continue		Jump back to the start of the while loop; the
				loop continues.
	:break			Jump forward to the ":endwhile"; the loop is
				discontinued.

Example: >

	:while counter < 40
	:  call do_something()
	:  if skip_flag
	:    continue
	:  endif
	:  if finished_flag
	:    break
	:  endif
	:  sleep 50m
	:endwhile

The ":sleep" command makes Vim take a nap.  The "50m" specifies fifty
milliseconds.  Another example is ":sleep 4", which sleeps for four 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"

Title: Vim Logic Operations, Looping, and Executing Expressions
Summary
This section delves into Vim's logic operations, including comparisons for numbers and strings, with considerations for the 'ignorecase' option and case-sensitive matching. It discusses the use of '=~' and '!~' for pattern matching. It also covers more looping constructs using ':continue' and ':break' within ':while' loops, and introduces the ':sleep' command for pausing execution. Finally, it explains how to execute expressions using ':execute' to build and run commands dynamically, and briefly mentions ':normal' for executing Normal mode commands.