# HG changeset patch # User Steve Losh # Date 1318214984 14400 # Node ID 07cfd2d5141f5d370b4992ac2f82caf578a4b3f4 # Parent 284f1adbf03910ce4a78aa3e7abebe8e4de1eeaf Register variables. diff -r 284f1adbf039 -r 07cfd2d5141f chapters/19.markdown --- a/chapters/19.markdown Sun Oct 09 22:38:43 2011 -0400 +++ b/chapters/19.markdown Sun Oct 09 22:49:44 2011 -0400 @@ -64,7 +64,7 @@ :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 +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 @@ -83,6 +83,37 @@ Notice that the first window has line numbers and the second does not. +Registers as Variables +---------------------- + +You can also read and set *registers* as variables. Run the following command: + + :let @a = "hello!" + +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. + +Registers can also be read. Run the following command: + + :echo @a + +Vim will echo "hello!". + +Select a word in your file and yank it with `y`, then run this command: + + :echo @" + +Vim will echo the word you just yanked. The `"` register is the "unnamed" +register, which is where text you yank without specifying a destination will go. + +Perform a search in your file with `/someword`, then run the following command: + + :echo @/ + +Vim will echo the search pattern you just used. This lets you programmatically +read and modify the current search pattern, which can be very useful at times. + Exercises ---------