Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vimscript variable


May 24, 2021 Vim


Table of contents


So far we've finished talking about single-line commands. V im scripts will be used as a scripting language in a third of the chapters later in the book. This part won't learn as quickly as what you learned earlier, but it's the last part of the book that will cover all the aspects of creating a plug-in.

Here we go. T he first thing we need to know is the variables. Execute the following command.

:let foo = "bar"
:echo foo

Vim displays bar foo is now a variable, and we assign it "bar" Now execute these commands:

:let foo = 42
:echo foo

Vim 42 we assign foo as integer 42

From these small examples, it seems that Vim scripts are dynamic types. This is not the case, we will explain later.

As an option for variables

You can set the option as a variable with a special syntax. Execute the following command:

:set textwidth=80
:echo &textwidth

Vim displays 80 To add a symbol before the name & Vim that you are referencing this option, rather than using a variable with exactly the same name.

Let's see how Vim handles the Boolean option. Execute the following command:

:set nowrap
:echo &wrap

Vim shows 0 . Then try these options:

:set wrap
:echo &wrap

This time Vim will show 1 T hese outputs clearly indicate that Vim 0 as "false" and 1 as "true". We can go a step further and assume that Vim treats all non-0-value integers as "truthy", and that's true.

We can also use the let command to set the option for variables. Execute the following command:

:let &textwidth = 100
:set textwidth?

Vim textwidth=100

Now set can set the options, why should we use let Execute the following command:

:let &textwidth = &textwidth + 10
:set textwidth?

This time Vim shows textwidth=110 W hen you set an option with set you can only set a constant value for it. When you let and set it as a variable, you can use all the power of the Vim script to determine its value.

Local options

If you want to use an option as a variable to set its local value instead of the global value, you need to prefix the variable name.

Open two files in two separate windows. Execute the following command:

:let &l:number = 1

Then switch to another file, and then execute the following command:

:let &l:number = 0

Note that the first window will have a line number, while the second does not.

Register as a variable

You can also read and set the register as a variable. Execute the following command:

:let @a = "hello!"

Now place the cursor somewhere in the text and tap "ap T his command tells Vim to "paste the contents a here." W e set the contents of this register, so Vim will hello! Paste into your text.

You can also read the contents of the register. Execute the following command:

:echo @a

Vim hello! .

Select a word in your file and copy it y then execute the following command:

:echo @"

Vim outputs the word you just copied. " are "unnamed" registers, where text that does not specify a register is placed at the time of replication.

Do a search /someword and then execute the following command:

:echo @/

Vim outputs the search mode you just used. This way you can program to read and modify the current search mode, which can sometimes be useful.

Practice

Check your ~/.vimrc file and replace some of these set setlocal commands with their let form. Remember that the Boolean option still needs to be set to a value.

Try setting a Boolean option to a value other than 0 and 1, such as wrap W hat happens when you set it to a different number? What happens if you set it to a string?

Go back to ~/.vimrc and recover all the modifications. Never set when the set is let is let difficult to read.

Read :help registers see a list of registers that you can read and write.