# HG changeset patch # User Steve Losh # Date 1365040415 14400 # Node ID f09f87e10570c9dc3ad636d60da3e62a39aeea15 # Parent 4fa876d7523c20ecae2788d45ee994387327dda5 Proof 19-26. diff -r 4fa876d7523c -r f09f87e10570 chapters/19.markdown --- a/chapters/19.markdown Wed Apr 03 21:40:17 2013 -0400 +++ b/chapters/19.markdown Wed Apr 03 21:53:35 2013 -0400 @@ -14,14 +14,14 @@ :let foo = "bar" :echo foo -Vim will display "bar". `foo` is now a variable, and we've assigned it -a string: "bar". Now run these commands: +Vim will display `bar`. `foo` is now a variable, and we've assigned it +a string: `"bar"`. Now run these commands: :::vim :let foo = 42 :echo foo -Vim will display "42", because we've reassigned `foo` to the integer "42". +Vim will display `42`, because we've reassigned `foo` to the integer `42`. From these short examples it may seem like Vimscript is dynamically typed. That's not the case, but we'll talk more about that later. @@ -36,7 +36,7 @@ :set textwidth=80 :echo &textwidth -Vim will display "80". Using an ampersand in front of a name tells Vim that +Vim will display `80`. Using an ampersand in front of a name tells Vim that you're referring to the option, not a variable that happens to have the same name. @@ -46,13 +46,13 @@ :set nowrap :echo &wrap -Vim displays "0". Now try these commands: +Vim displays `0`. Now try these commands: :::vim :set wrap :echo &wrap -This time Vim displays "1". This is a very strong hint that Vim treats the +This time Vim displays `1`. This is a very strong hint that Vim treats the integer `0` as "false" and the integer `1` as "true". It would be reasonable to assume that Vim treats *any* non-zero integer as "truthy", and this is indeed the case. @@ -64,7 +64,7 @@ :let &textwidth = 100 :set textwidth? -Vim will display "textwidth=100". +Vim will display `textwidth=100`. Why would we want to do this when we could just use `set`? Run the following commands: @@ -73,7 +73,7 @@ :let &textwidth = &textwidth + 10 :set textwidth? -This time Vim displays "textwidth=110". When you set an option using `set` you +This time Vim displays `textwidth=110`. When you set an option using `set` you can only set it to a single literal value. When you use `let` and set it as a variable you can use the full power of Vimscript to determine the value. @@ -105,14 +105,14 @@ Now put your cursor somewhere in your text and type `"ap`. This command tells Vim to "paste the contents of register `a` here". We just set the contents of -that register, so Vim pastes "hello!" into your text. +that register, so Vim pastes `hello!` into your text. Registers can also be read. Run the following command: :::vim :echo @a -Vim will echo "hello!". +Vim will echo `hello!`. Select a word in your file and yank it with `y`, then run this command: diff -r 4fa876d7523c -r f09f87e10570 chapters/20.markdown --- a/chapters/20.markdown Wed Apr 03 21:40:17 2013 -0400 +++ b/chapters/20.markdown Wed Apr 03 21:53:35 2013 -0400 @@ -12,7 +12,7 @@ :let b:hello = "world" :echo b:hello -As expected, Vim displays "world". Now switch to the other buffer and run the +As expected, Vim displays `world`. Now switch to the other buffer and run the `echo` command again: :::vim diff -r 4fa876d7523c -r f09f87e10570 chapters/21.markdown --- a/chapters/21.markdown Wed Apr 03 21:40:17 2013 -0400 +++ b/chapters/21.markdown Wed Apr 03 21:53:35 2013 -0400 @@ -45,7 +45,7 @@ : echom "ONE" :endif -Vim will display "ONE", because the integer `1` is "truthy". Now try these +Vim will display `ONE`, because the integer `1` is "truthy". Now try these commands: :::vim @@ -53,7 +53,7 @@ : echom "ZERO" :endif -Vim will *not* display "ZERO" because the integer `0` is "falsy". Let's see how +Vim will *not* display `ZERO` because the integer `0` is "falsy". Let's see how strings behave. Run these commands: :::vim @@ -80,8 +80,8 @@ :echom "10hello" + 10 :echom "hello10" + 10 -The first command causes Vim to echo "10", the second command echoes "20", and -the third echoes "10" again! +The first command causes Vim to echo `10`, the second command echoes `20`, and +the third echoes `10` again! After observing all of these commands we can draw a few informed conclusions about Vimscript: @@ -109,7 +109,7 @@ : echom "finally!" :endif -Vim echoes "finally!" because both of the previous conditions evaluate to zero, +Vim echoes `finally!` because both of the previous conditions evaluate to zero, which is falsy. Exercises diff -r 4fa876d7523c -r f09f87e10570 chapters/22.markdown --- a/chapters/22.markdown Wed Apr 03 21:40:17 2013 -0400 +++ b/chapters/22.markdown Wed Apr 03 21:53:35 2013 -0400 @@ -12,7 +12,7 @@ : echom "foo" :endif -Vim will, of course, display "foo". Now run these commands: +Vim will, of course, display `foo`. Now run these commands: :::vim :if 10 > 2001 @@ -29,7 +29,7 @@ : echom "second" :endif -Vim displays "second". Nothing surprising here. Let's try comparing strings. +Vim displays `second`. Nothing surprising here. Let's try comparing strings. Run these commands: :::vim @@ -39,7 +39,7 @@ : echom "two" :endif -Vim echoes "two". There's still nothing surprising, so what was I going on +Vim echoes `two`. There's still nothing surprising, so what was I going on about at the beginning of the chapter? Case Sensitivity @@ -97,7 +97,7 @@ : echom "second" :endif -Vim displays "first" because `==?` is the "case-insensitive no matter what the +Vim displays `first` because `==?` is the "case-insensitive no matter what the user has set" comparison operator. Now run the following commands: :::vim @@ -108,7 +108,7 @@ : echom "two" :endif -Vim displays "two" because `==#` is the "case-sensitive no matter what the user +Vim displays `two` because `==#` is the "case-sensitive no matter what the user has set" comparison operator. The moral of this story is that you should *always* use explicit case sensitive diff -r 4fa876d7523c -r f09f87e10570 chapters/23.markdown --- a/chapters/23.markdown Wed Apr 03 21:40:17 2013 -0400 +++ b/chapters/23.markdown Wed Apr 03 21:53:35 2013 -0400 @@ -31,7 +31,7 @@ :::vim :call Meow() -Vim will display "Meow!" as expected. +Vim will display `Meow!` as expected. Let's try returning a value. Run the following commands: @@ -46,7 +46,7 @@ :echom GetMeow() Vim will call the function and give the result to `echom`, which will display -"Meow String!". +`Meow String!`. Calling Functions ----------------- @@ -61,7 +61,7 @@ :call Meow() :call GetMeow() -The first will display "Meow!" but the second doesn't display anything. The +The first will display `Meow!` but the second doesn't display anything. The return value is thrown away when you use `call`, so this is only useful when the function has side effects. @@ -81,7 +81,7 @@ :::vim :echom Meow() -This will display two lines: "Meow!" and "0". The first obviously comes from +This will display two lines: `Meow!` and `0`. The first obviously comes from the `echom` inside of `Meow`. The second shows us that if a Vimscript function doesn't return a value, it implicitly returns `0`. Let's use this to our advantage. Run the following commands: diff -r 4fa876d7523c -r f09f87e10570 chapters/24.markdown --- a/chapters/24.markdown Wed Apr 03 21:40:17 2013 -0400 +++ b/chapters/24.markdown Wed Apr 03 21:53:35 2013 -0400 @@ -14,7 +14,7 @@ :::vim :call DisplayName("Your Name") -Vim will display two lines: "Hello! My name is:" and "Your Name". +Vim will display two lines: `Hello! My name is:` and `Your Name`. Notice the `a:` in the name of the variable that we passed to the `echom` command. This represents a variable scope, which we talked about in an earlier @@ -56,12 +56,12 @@ The `...` in the function definition tells Vim that this function can take any number of arguments. This is like a `*args` argument in a Python function. -The first line of the function echoes the message `a:0` and displays "2". When +The first line of the function echoes the message `a:0` and displays `2`. When you define a function that takes a variable number of arguments in Vim, `a:0` will be set to the number of extra arguments you were given. In this case we -passed two arguments to `Varg` so Vim displayed "2". +passed two arguments to `Varg` so Vim displayed `2`. -The second line echoes `a:1` which displays "a". You can use `a:1`, `a:2`, etc +The second line echoes `a:1` which displays `a`. You can use `a:1`, `a:2`, etc to refer to each extra argument your function receives. If we had used `a:2` Vim would have displayed "b". @@ -83,7 +83,7 @@ :call Varg2("a", "b", "c") -We can see that Vim puts "a" into the named argument `a:foo`, and the rest are +We can see that Vim puts `"a"` into the named argument `a:foo`, and the rest are put into the list of varargs. Assignment @@ -111,7 +111,7 @@ :call AssignGood("test") -This time the function works, and Vim displays "Yep". +This time the function works, and Vim displays `Yep`. Exercises --------- diff -r 4fa876d7523c -r f09f87e10570 chapters/25.markdown --- a/chapters/25.markdown Wed Apr 03 21:40:17 2013 -0400 +++ b/chapters/25.markdown Wed Apr 03 21:53:35 2013 -0400 @@ -15,12 +15,12 @@ :::vim :echom 100 -No surprises here -- Vim displays "100". Now run this command: +No surprises here -- Vim displays `100`. Now run this command: :::vim :echom 0xff -This time Vim displays "255". You can specify numbers in hex notation by +This time Vim displays `255`. You can specify numbers in hex notation by prefixing them with `0x` or `0X`. Now run this command: :::vim @@ -33,8 +33,8 @@ :echom 017 :echom 019 -Vim will print "15" for the first command, because "17" in octal is equal to -"15" in decimal. For the second command Vim treats it as a decimal number, even +Vim will print `15` for the first command, because `17` in octal is equal to +`15` in decimal. For the second command Vim treats it as a decimal number, even though it starts with a `0`, because it's not a valid octal number. Because Vim silently does the wrong thing in this case, I'd recommend avoiding @@ -51,24 +51,24 @@ Notice that we're using `echo` here and not `echom` like we usually to. We'll talk about why in a moment. -Vim displays "100.1" as expected. You can also use exponential notation. Run +Vim displays `100.1` as expected. You can also use exponential notation. Run this command: :::vim :echo 5.45e+3 -Vim displays "5450.0". A negative exponent can also be used. Run this command: +Vim displays `5450.0`. A negative exponent can also be used. Run this command: :::vim :echo 15.45e-2 -Vim displays "0.1545". The `+` or `-` before the power of ten is optional. If +Vim displays `0.1545`. The `+` or `-` before the power of ten is optional. If it's omitted then it's assumed to be positive. Run the following command: :::vim :echo 15.3e9 -Vim will display "1.53e10", which is equivalent. The decimal point and number +Vim will display `1.53e10`, which is equivalent. The decimal point and number after it are *not* optional. Run the following command and see that it crashes: :::vim @@ -84,7 +84,7 @@ :::vim :echo 2 * 2.0 -Vim displays "4.0". +Vim displays `4.0`. Division -------- @@ -94,14 +94,14 @@ :::vim :echo 3 / 2 -Vim displays "1". If you want Vim to perform floating point division one of the +Vim displays `1`. If you want Vim to perform floating point division one of the numbers needs to be a Float, which will cause the other one to be coerced to a Float as well. Run this command: :::vim :echo 3 / 2.0 -Vim displays "1.5". The "3" is coerced to a Float, and then normal floating +Vim displays `1.5`. The `3` is coerced to a Float, and then normal floating point division is performed. Exercises diff -r 4fa876d7523c -r f09f87e10570 chapters/26.markdown --- a/chapters/26.markdown Wed Apr 03 21:40:17 2013 -0400 +++ b/chapters/26.markdown Wed Apr 03 21:53:35 2013 -0400 @@ -9,7 +9,7 @@ :::vim :echom "Hello" -Vim will echo "Hello". So far, so good. +Vim will echo `Hello`. So far, so good. Concatenation ------------- @@ -20,7 +20,7 @@ :::vim :echom "Hello, " + "world" -What happened? Vim displayed "0" for some reason! +What happened? Vim displayed `0` for some reason! Here's the issue: Vim's `+` operator is *only* for Numbers. When you pass a string to `+` Vim will try to coerce it to a Number before performing the @@ -29,8 +29,8 @@ :::vim :echom "3 mice" + "2 cats" -This time Vim displays "5", because the strings are coerced to the numbers "3" -and "2" respectively. +This time Vim displays `5`, because the strings are coerced to the numbers `3` +and `2` respectively. When I said "Number" I really *meant* Number. Vim will *not* coerce strings to Floats! Try this command to see prove this: @@ -38,8 +38,8 @@ :::vim :echom 10 + "10.10" -Vim displays "20" because it dropped everything after the decimal point when -coercing "10.10" to a Number. +Vim displays `20` because it dropped everything after the decimal point when +coercing `10.10` to a Number. To combine strings you need to use the concatenation operator. Run the following command: @@ -47,7 +47,7 @@ :::vim :echom "Hello, " . "world" -This time Vim displays "Hello, world". `.` is the "concatenate strings" +This time Vim displays `Hello, world`. `.` is the "concatenate strings" operator in Vim, which lets you combine strings. It doesn't add whitespace or anything else in between. @@ -56,7 +56,7 @@ :::vim :echom 10 . "foo" -Vim will display "10foo". First it coerces `10` to a String, then it +Vim will display `10foo`. First it coerces `10` to a String, then it concatenates it with the string on the right hand side. Things get a bit stickier when we're working with Floats, though. Run this command: @@ -99,13 +99,13 @@ :::vim :echo "foo\nbar" -This time Vim will display two lines, "foo" and "bar", because the `\n` is +This time Vim will display two lines, `foo` and `bar`, because the `\n` is replaced with a newline. Now try running this command: :::vim :echom "foo\nbar" -Vim will display something like "foo^@bar". When you use `echom` instead of +Vim will display something like `foo^@bar`. When you use `echom` instead of `echo` with a String Vim will echo the *exact* characters of the string, which sometimes means that it will show a different representation than plain old `echo`. `^@` is Vim's way of saying "newline character". @@ -145,7 +145,7 @@ : echo "no" :endif -Vim will display "no". If you're wondering why this happens you should reread +Vim will display `no`. If you're wondering why this happens you should reread the chapter on conditionals, because we talked about it there. Exercises