# HG changeset patch # User Steve Losh # Date 1353368421 18000 # Node ID efff67c5b7535d01087fb386f939cca56dca292a # Parent 5c84fa9303778712b94deccf9cb827ccea3631dd Proof 24-28. diff -r 5c84fa930377 -r efff67c5b753 chapters/24.markdown --- a/chapters/24.markdown Sat Nov 17 16:48:55 2012 -0500 +++ b/chapters/24.markdown Mon Nov 19 18:40:21 2012 -0500 @@ -54,9 +54,9 @@ This function shows us several things, so let's look at each one individually. The `...` in the function definition tells Vim that this function can take any -number of arguments. This is like a `*args` argument in Python functions. +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". diff -r 5c84fa930377 -r efff67c5b753 chapters/25.markdown --- a/chapters/25.markdown Sat Nov 17 16:48:55 2012 -0500 +++ b/chapters/25.markdown Mon Nov 19 18:40:21 2012 -0500 @@ -5,7 +5,7 @@ you can use. First we'll go over Vim's numeric types. Vimscript has two types of numeric variables: Numbers and Floats. A Number is -a 32 bit signed integer. A Float is, obviously, a floating point number. +a 32 bit signed integer. A Float is a floating point number. Number Formats -------------- @@ -49,7 +49,7 @@ :echo 100.1 Notice that we're using `echo` here and not `echom` like we usually to. We'll -talk why in a moment. +talk about why in a moment. Vim displays "100.1" as expected. You can also use exponential notation. Run this command: diff -r 5c84fa930377 -r efff67c5b753 chapters/26.markdown --- a/chapters/26.markdown Sat Nov 17 16:48:55 2012 -0500 +++ b/chapters/26.markdown Mon Nov 19 18:40:21 2012 -0500 @@ -105,8 +105,8 @@ :::vim :echom "foo\nbar" -Vim will display something like "foo^@bar". When you use `echom` with a String -instead of `echo` Vim will echo the exact characters of the string, which +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". @@ -121,13 +121,13 @@ Vim displays `\n\\`. Using single quotes tells Vim that you want the string *exactly* as-is, with no escape sequences. The one exception is that two single -quotes in a row will produce a single single quote. Try this command: +quotes in a row will produce one single quote. Try this command: :::vim :echom 'That''s enough.' -Vim will display "That's enough." Two single quotes is the *only* sequence that -has special meaning in a literal string. +Vim will display `That's enough.`. Two single quotes is the *only* sequence +that has special meaning in a literal string. We'll revisit literal strings when they become most useful, later in the book (when we dive into regular expressions). diff -r 5c84fa930377 -r efff67c5b753 chapters/27.markdown --- a/chapters/27.markdown Sat Nov 17 16:48:55 2012 -0500 +++ b/chapters/27.markdown Mon Nov 19 18:40:21 2012 -0500 @@ -29,7 +29,7 @@ :::vim :echo split("one two three") -Vim displays "['one', 'two', 'three']". The `split` function splits a String +Vim displays `['one', 'two', 'three']`. The `split` function splits a String into a List. We'll talk about Lists shortly, but for now don't worry too much about them. @@ -39,7 +39,7 @@ :::vim :echo split("one,two,three", ",") -Vim will once again display "['one', 'two', 'three']", because the second +Vim will once again display `['one', 'two', 'three']`, because the second argument to `split` tells it to split the string on the comma character instead of on whitespace. @@ -87,6 +87,13 @@ Exercises --------- +Run `:echo split('1 2')` and `:echo split('1,,,2', ',')`. Do they behave the +same? + +Read `:help split()`. + +Read `:help join()`. + Read `:help functions` and skim the list of built-in functions for ones that mention the word "String". Use the `/` command to make it easier (remember, Vim's help files can be navigated like any other kind of file). There are diff -r 5c84fa930377 -r efff67c5b753 chapters/28.markdown --- a/chapters/28.markdown Sat Nov 17 16:48:55 2012 -0500 +++ b/chapters/28.markdown Mon Nov 19 18:40:21 2012 -0500 @@ -2,8 +2,13 @@ ======= The `execute` command is used to evaluate a string as if it were a Vimscript -command. We saw it in an earlier chapter, but now we're going to take another -look at it because it's used extremely often. Run the following command: +command. We saw it in an earlier chapter, but now that we know a bit more about +Vimscript Strings we're going to take another look. + +Basic Execution +--------------- + +Run the following command: :::vim :execute "echom 'Hello, world!'" @@ -22,31 +27,29 @@ Vim will open the first file in a vertical split to the right of the second file. What happened here? -First, Vim builds the command string by concatenating "rightbelow vsplit -" with the result of the `bufname("#")` call. +First, Vim builds the command string by concatenating `"rightbelow vsplit "` +with the result of the `bufname("#")` call. We'll look at the function more later, but for now just trust that it returns the path of the previous buffer. You can play with it using `echom` if you want to see for yourself. -Once `bufname` is evaluated Vim has a string that looks something like -"rightbelow vsplit bar.txt". The `execute` command evaluates this as -a Vimscript command which opens the split with the file. +Once `bufname` is evaluated Vim the string `"rightbelow vsplit bar.txt"`. The +`execute` command evaluates this as a Vimscript command which opens the split +with the file. Is Execute Dangerous? --------------------- In most programming languages the use of such an "eval" construct to evaluate -strings as program code is frowned upon, to put it lightly. Vimscript's -`execute` command doesn't bear the same stigma for two reasons. +strings as program code is frowned upon (to put it lightly). Vimscript's +`execute` command doesn't have the same stigma for two reasons. First, most Vimscript code only ever takes input from a single person: the user. If the user wants to input a tricky string that will cause an `execute` command -to do something bad, well, it's their computer! - -Contrast this with other languages, where programs constantly take input from -untrusted users. Vim is a unique environment where the normal security concerns -simply aren't common. +to do something bad, well, it's their computer! Contrast this with other +languages, where programs constantly take input from untrusted users. Vim is +a unique environment where the normal security concerns simply aren't common. The second reason is that because Vimscript has sometimes arcane and tricky syntax, `execute` is often the easiest, most straightforward way to get