May 24, 2021 Vim
Next we'll talk about string types. Since Vim is used to edit text, you will deal with this type frequently.
Do the following code:
:echom "Hello"
Vim outputs
Hello
So far, so far.
In everyday programming you often need to connect strings together. Execute the following command:
:echom "Hello, " + "world"
What happened?
Somehow, Vim shows
0
That's the problem:
+
operator, just, applies to numeric values. W
hen you take a string as
+
argument to , Vim tries to cast it to a Number before performing the addition.
In the 21-chapter exercise, did you really drink beer?
:echom "3 mice" + "2 cats"
This time Vim shows
5
because the string is converted to
3
2
When it comes to "Number," I explicitly mean the type _Number type. V im does not cast strings as Float types! To prove my case, try the following command:
:echom 10 + "10.10"
Vim
20
because when
10.10
into a Humber, the content after the dot is discarded.
You need to use a link operator to connect strings. Follow these commands:
:echom "Hello, " . "world"
This time Vim shows
Hello, world
.
i
s the Connection String operator in Vim, which can be used to connect strings.
It doesn't insert spaces or anything in between.
Vim will be
.
C
ast on both sides.
Try this command:
:echom 10 . "foo"
Vim will
10foo
F
irst it
10
into a string, and then it connects it to the string on the right. B
ut when it comes to the Float type, it's a bit bad.
To execute this command:
:echom 10.1 . "foo"
This time Vim threw out a mistake claiming that we were treating Float as String. Vim is happy for you to think of String as Float when you add, but you're happy to think of Float as String when you connect strings.
The end of the story tells us one thing: Vim is javascript-like: it sometimes allows you to sloppyly treat type differences, but Chemo does: sooner or later it's going to pay back because it comes out and mixes. (because it will come back to bite you at some point)
When you write Vimscript, make sure you clearly write down the type of each variable. I f you need to change the variable type, you have to change it explicitly using a function, even if that is not necessary. Don't rely on Vim's cast, after all, there's no regret medicine in the world.
Like most programming languages, Vimscript allows you to use escape strings in strings to represent characters that you can't beat. Follow these commands:
:echom "foo \"bar\""
The
\"
will be replaced with double quotes as you wish. E
scape strings will do as you wish in most cases.
Execute the following command:
:echom "foo\\bar"
Vim
foo\bar
because
\\
an escape string that represents '', just like most programming languages.
Now execute the following command (note that
echo
not
echom
:echo "foo\nbar"
This time Vim will show two lines,
foo
bar
because
\n
be replaced with a new line.
Now try the following command:
:echom "foo\nbar"
Vim displays weird information @bar like
foo^@bar
W
hen you use
echom
echo
on a string, Vim outputs the extra characters in the string, which means that
echom
echo
^@
break" is represented in Vim.
Vim also allows you to use "string liters" (such as r"" in Python) to avoid the abuse of escape strings. Follow these commands:
:echom '\n\\'
Vim
\n\\
. U
sing single quotes tells Vim that you want the string to
be WYSIWYG,
regardless of the escape string. O
ne exception is that two consecutive single quotes in a row produce a single quote.
Try this command for some sort of escape:
:echom 'That''s enough.'
Vim will
That's enough.
. .
Two single quotation marks are special sequences of strings that are literally "unique".
Later in the book, we'll revisit more of the literal content of strings. (Then we'll be stuck in regular expressions)
You may want to know how Vim treats
if
in if statements.
Execute the following command:
:if "foo"
: echo "yes"
:else
: echo "no"
:endif
Vim
no
If you don't understand why, you should reread the chapter on conditional statements, because we've discussed it there.
Read
:help expr-quote
. V
iew a list of escape strings that are allowed in a typical Vim string.
Find out how to insert a tab character.
Try to find a way to insert a tab character using an escape string.
Tip: Read
:help i_CTRL-V
Read:
:help literal-string