274674dc2025

MOAR.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Tue, 25 Oct 2011 00:31:58 -0400
parents ae64d9c11fe7
children 2bc5902b5738
branches/tags (none)
files chapters/30.markdown chapters/31.markdown preface.markdown

Changes

--- a/chapters/30.markdown	Tue Oct 25 00:00:19 2011 -0400
+++ b/chapters/30.markdown	Tue Oct 25 00:31:58 2011 -0400
@@ -1,2 +1,48 @@
 Execute Normal!
 ===============
+
+Now that we've seen `execute` and `normal!` we can talk about a common Vimscript
+idiom.  Run the following command:
+
+    :execute "normal! gg/foo\<cr>dd"
+
+This will move to the top of the file, search for the first occurrence of "foo",
+and delete the line that contains it.
+
+Previously we tried to use `normal!` with a search command but couldn't enter
+the return needed to actually perform the search.  Combining `normal!` with
+`execute` fixes that problem.
+
+`execute` lets you build commands programatically, so you can use Vim's normal
+string escape sequences to generate the non-printing characters you need.  Try
+the following command:
+
+    :execute "normal! mqA;\<esc>`q"
+
+What does this do?  Let's break it apart:
+
+* `:execute "normal! ..."`: run the sequence of commands as if they were entered
+  in normal mode, ignoring all mappings, and replacing escape sequences with
+  their results.
+* `mq`: store the current location in mark "q".
+* `A`: move to the end of the current line and enter insert mode after the last
+  character.
+* `;`: we're now in insert mode, so just put a literal semicolon in the file.
+* `\<esc>`: this is a string escape sequence which resolves to a press of the
+  escape key, which takes us out of insert mode.
+* ```q``: return to the exact location of mark "q".
+
+It looks a bit scary but it's actually quite useful: it puts a semicolon at the
+end of the current line while leaving your cursor in place.  A mapping for this
+could come in handy if you forget a semicolon when writing Javascript, C, or any
+other language with semicolons as statement terminators.
+
+Exercises
+---------
+
+Read `:help expr-quote` again (you've seen it before) to remind yourself how to
+use string escapes to pass special characters to `normal!` with `execute`.
+
+Put down this book for a while before you go on to the next chapter.  Get
+a sandwich or a cup of coffee, feed your pets if you have them, and relax for
+a bit before continuing.
--- a/chapters/31.markdown	Tue Oct 25 00:00:19 2011 -0400
+++ b/chapters/31.markdown	Tue Oct 25 00:31:58 2011 -0400
@@ -46,11 +46,11 @@
     :execute "normal! gg/print\<cr>"
 
 This will go to the top of the file and perform a search for print, putting us
-at the first match.  It does this using `:execute "normal! ..."` which we've
-seen in previous chapters.
+at the first match.  It does this using `:execute "normal! ..."` which we
+saw in the previous chapter.
 
-To get to the second match in the file you can just tack more commands onto the
-string.  Run this command:
+To get to the second match in the file you can just add more commands onto the
+end of the string.  Run this command:
 
     :execute "normal! gg/print\<cr>n"
 
@@ -172,10 +172,10 @@
 
 Add another mapping that will clear the match (perhaps `<leader>W`).
 
-Add a normal mode mapping that will automatically insert `\v` for you whenever
-you begin a search.  If you're stuck remember that Vim's mappings are extremely
-simple and you just need to tell it which keys to press when you use the mapped
-key.
+Add a normal mode mapping that will automatically insert the `\v` for you
+whenever you begin a search.  If you're stuck remember that Vim's mappings are
+extremely simple and you just need to tell it which keys to press when you use
+the mapped key.
 
 Add the `hlsearch` and `incsearch` options to your `~/.vimrc` file, set however
 you prefer.
--- a/preface.markdown	Tue Oct 25 00:00:19 2011 -0400
+++ b/preface.markdown	Tue Oct 25 00:31:58 2011 -0400
@@ -6,9 +6,10 @@
 That text gets turned into numbers and those numbers bump into other numbers
 and *make things happen*.
 
-To get our ideas out of our heads and the chunks of text we call "programs" we
-use text editors.  Full-time programmers will easily spend thousands of hours
-of their lives interacting with their text editor doing many things:
+To get our ideas out of our heads and create the chunks of text we call
+"programs" we use text editors.  Full-time programmers will easily spend tens of
+thousands of hours of their lives interacting with their text editor doing many
+things:
 
 * Getting raw text from brains into computers.
 * Correcting mistakes in that text.
@@ -25,11 +26,26 @@
 Along the way I'll also mention things that aren't strictly about Vimscript, but
 are more about learning and being more efficient in general.  Learning Vimscript
 isn't going to help you much if you wind up fiddling with your editor all day
-instead of working, so you must strike a balance.
+instead of working, so you have to strike a balance.
+
+The style of this book is a bit different from most other books about
+programming languages.  Instead of simply presenting you with facts about how
+Vimscript works, it guides you through typing in commands to see what they do.
+
+Frequently the book will lead you into dead ends before explaining the "right
+way" to solve a problem.  Most other books don't do this, or only mention the
+sticky issues *after* showing you the solution.
+
+This isn't how things typically happen in the real world, though.  Most of the
+time you'll be whipping up a quick piece of Vimscript and run into a quirk of
+the language that you'll need to figure out.  By stepping through this process
+in the book instead of glossing over it I hope to get you used to the process of
+dealing with Vimscript's silliness so you're ready when you find other edge
+cases.  Practice makes perfect.
 
 Each chapter of the book focuses on a single topic.  They're short but packed
 with information, so don't just skim them.  If you really want to learn the most
-you can from this book, you need to *type in* all of the commands.
+you can you need to *type in* all of the commands.
 
 You may already be an experienced programmer who's used to reading code and
 understanding it straight away.  If so: it doesn't matter.  Learning Vim and
@@ -39,19 +55,22 @@
 
 **Do *all* the exercises.**
 
-Vimscript is old and has a lot of dusty corners and twisty hallways.  One
-configuration option can change how the entire language works.
-
-By typing *every* command in *every* lesson exercise and doing *every* exercise
-you'll discover problems with your Vim build or configuration on the simpler
-commands, which will be easier to diagnose and fix.
+There are two reasons this is so important.  First, Vimscript is old and has
+a lot of dusty corners and twisty hallways.  One configuration option can change
+how the entire language works.  By typing *every* command in *every* lesson
+exercise and doing *every* exercise you'll discover problems with your Vim build
+or configuration on the simpler commands, which will be easier to diagnose and
+fix.
 
 Second, Vimscript *is* Vim.  To save a file in Vim, you type `:write` (or `:w`
 for short) and press return.  To save a file in a Vimscript, you use `write`.
-Mastering a text editor means developing muscle memory, which you simply can't
-get from just reading.
+Many of the Vimscript commands you'll learn can be used in your day-to-day
+editing as well, but they're only helpful if they're in your muscle memory,
+which simply doesn't happen from just reading.
 
 I hope you'll find this book useful.  It's *not* meant to be a comprehensive
 guide to Vimscript.  It's meant to get you comfortable enough with the language
-to read other people's code (with regular side-trips to `:help`) and recognize
-some common pitfalls.
+to write some simple plugins, read other people's code (with regular side-trips
+to `:help`), and recognize some of the common pitfalls.
+
+Good luck!