Skip to content

Commit

Permalink
Proof 24-28.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjl committed Nov 19, 2012
1 parent a0763a6 commit a46fadc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
4 changes: 2 additions & 2 deletions chapters/24.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ Javascript and Python. Run the following commands:
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".
Expand Down
4 changes: 2 additions & 2 deletions chapters/25.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Now it's time to start taking a closer look at the different types of variables
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
--------------
Expand Down Expand Up @@ -49,7 +49,7 @@ Floats can also be specified in multiple ways. Run the following command:
: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:
Expand Down
10 changes: 5 additions & 5 deletions chapters/26.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ replaced with a newline. Now try running this command:
:::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".

Expand All @@ -121,13 +121,13 @@ sequences. Run the following command:

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).
Expand Down
11 changes: 9 additions & 2 deletions chapters/27.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Run the following command (note that it's an `echo` and not an `echom`):
:::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.

Expand All @@ -39,7 +39,7 @@ Run the following command:
:::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.

Expand Down Expand Up @@ -87,6 +87,13 @@ scripts.
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
Expand Down
31 changes: 17 additions & 14 deletions chapters/28.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ Execute
=======

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!'"
Expand All @@ -22,31 +27,29 @@ following command:
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
Expand Down

0 comments on commit a46fadc

Please sign in to comment.