284f1adbf039

Variables.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sun, 09 Oct 2011 22:38:43 -0400
parents 55de4dec73e1
children 07cfd2d5141f
branches/tags (none)
files chapters/19.markdown outline.org

Changes

--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/chapters/19.markdown	Sun Oct 09 22:38:43 2011 -0400
@@ -0,0 +1,98 @@
+Variables
+=========
+
+Up to this point we've covered single commands.  For the next third of the book
+we're going to look at Vimscript as a *programming language*.  This won't be as
+instantly gratifying as the rest of what we've learned, but it will lay the
+groundwork for the last part, which walks through creating a full-fledged Vim
+plugin from scratch.
+
+The first thing we need to talk about are variables.
+
+Run the following commands:
+
+    :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:
+
+    :let foo = 42
+    :echo foo
+
+Vim will display "42", because we've reassigned `bar` to the integer "42".  From
+this we can see that Vimscript is dynamically typed.
+
+Options as Variables
+--------------------
+
+You can read and set *options* as variables by using a special syntax.  Run the
+following commands:
+
+    :set textwidth=80
+    :echo &textwidth
+
+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.
+
+Let's see how Vim works with boolean options.  Run the following commands:
+
+    :set nowrap
+    :echo &wrap
+
+Vim displays "0".  Now try these commands:
+
+    :set wrap
+    :echo &wrap
+
+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's reasonable to assume
+that Vim treats *any* non-zero integer as "truthy", and this is indeed the case.
+
+We can also *set* options as variables.  Run the following commands:
+
+    :let &textwidth = 100
+    :set textwidth?
+
+Vim will display "textwidth=100".
+
+Why would we want to do this when we could just use `set`? Run the following
+commands:
+
+    :let &textwidth = &textwidth + 10
+    :set textwidth?
+
+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.
+
+Local Options
+-------------
+
+If you want to set the *local* value of an option as a variable, instead of the
+*global* value, you need to prefix the variable name.
+
+Open two files in separate splits.  Run the following command:
+
+    :let &l:number = 1
+
+Now switch to the other file and run this command:
+
+    :let &l:number = 0
+
+Notice that the first window has line numbers and the second does not.
+
+Exercises
+---------
+
+Go through your `~/.vimrc` file and change some of the `set` and
+`setlocal` commands to their `let` forms.  Remember that boolean options still
+need to be set to something.
+
+Try setting a boolean option like `wrap` to something other than zero or one.
+What happens when you set it to a different number?  What happens if you set it
+to a string?
+
+Go back through your `~/.vimrc` file and undo the changes.  You should never use
+`let` if `set` will suffice -- it's harder to read.
--- a/outline.org	Sun Oct 09 19:18:29 2011 -0400
+++ b/outline.org	Sun Oct 09 22:38:43 2011 -0400
@@ -16,9 +16,9 @@
 ** DONE operator-pending maps
 ** DONE more operator-pending maps
 ** DONE basic status lines
-** TODO a word on shortened command names
+** DONE a word on shortened command names
 * part 2 - programming in vimscript
-** variables
+** TODO variables
 ** variable scopes
 ** conditionals
 ** comparisons