Doing Cool Things with VIM - Yanking
<rant>
The worst thing about the majority of VIM tutorials/books is that they start in the most obvious place. If I’m interested in a text editor and after reading the first chapter of a book all I know how to do is move the cursor around, I’m not going to be that impressed. Lets face it, moving the cursor around in any direction isn’t exactly a groundbreaking feature of VIM.
For a beginner VIM users, there is nothing interesting about how you can use k and j to move the cursor up or down, or the h and l to move the cursor left and right. I think it’s safe to say that, for the first tutorial we can let user use left-arrow instead of h, right arrow-instead of l, up-arrow instead of h and down-arrow instead of j. As users begin to understand the basic concepts behind VIM commands then it is important on it’s important to know that you can move the cursor up with j, down with k to the left with h and to the right with l. But there is no need to bring this up in the first lesson!
The other thing that there is absolutely no need to mention are the nuances of the different modes that VIM has. When you open VIM you’re in command mode, hit i and you can insert text, hit esc or (ctrl-c) and you’re back in command mode. How difficult is it to say that? What more do people need to know to get started.
VIM has tons of cool features, the conceived “steep learning curve” is probably because instead of focusing on the features that separates VIM, each tutorial starts out with a discussion about how to move the cursors using the keys j(down), k(up), l(right, I remember this because I think l should make the cursor go left, but it doesn’t), and h(left).
</rant>
OK, so with that out of the way lets get into cool thing #1
Yanking and Registers
One of the first things that I’ve had people ask when I introduce them to VIM is “How do I copy and paste”. In VIM you copy by “yanking” text into a “registry”. If I want to copy this line I’ll enter command mode (esc) and type yy, this yanks the entire line. Deleting will also yank the line into the default registry. To delete the entire line type dd.
Move your cursor (using the arrow keys or h j k l, or you can move to the end of the line using $ and the beginning of the line using ^) and then type p to put the text you just yanked or deleted into the current location of your cursor.
Now you’ll notice, that only the last thing you yanked (or deleted) is stored in the default register – If you yank or delete something up, the previous text will be replaced. To get around this, VIM provides registers which you can use to store yanked data.
Type a line of text, then move into command mode (esc or ctrl-c) and then type simply type “a^d . The “a specifies that you want to use the register identified by the letter a. ^d says you want to delete the text from the current position to the beginning of the line (^ moves the cursor to the beginning of the line).
Using registers to hold yanked (copied) data, you can have up to 26 pieces of data yanked into registers a-z. Sure beats copying and pasting!
Now if you don’t just want to yank the one line, but that line and the following 3 lines, just type “ay3j . “a specifies the register, y specifies you want to yank and 3j specifies that I want to move the cursor 3 lines down. So as you can see, in VIM most of the more complex commands are derived from simpler commands. 3j will move the cursor down 3 lines, so putting a y before that will yank that text, putting “a before that inserts that text into register a.
OK, now that I mentioned yanking 3 lines down using the j key, I might as well mention that if you want to yank 3 characters to the right you would substitute j (down) for l (right) which would turn the command into y3l. If you want to yank 3 words to the right you could use the e command which moves the cursor to the end of the next word y3e.
This may look complicated, but once you get used to it it becomes second nature.
To get the data out of a register, use the p (put) command again. To specify which register you want to pull the text out of specify that before the p command which leaves you with “ap .
And that’s that! Next time we talk about marks.



June 12th, 2006 at 3:47 am
Cool - never did those commands, and never heard of registers either. Need to remember that.
July 29th, 2006 at 7:13 pm
Just a note: your straight quotation marks were turned into curly ones.
January 5th, 2007 at 4:36 pm
Nice that you skipped all the hjkl stuff, and didn’t start going to huge complicated examples right away.
But what if I want to yank e.g. 63 letters to the right. Sure ay63l will do it, but then I have to count that number by hand. Isn’t there something that corresponds to the shift+arrow selection of many graphical editors?
March 27th, 2007 at 12:40 pm
diehl toyota…
diehl toyota author…
June 8th, 2007 at 1:03 pm
Simon: visual mode - just type vllllly or vwwwwwwwwwwy or vWWWWWWWWy(moving by characters or by words or by WORDS). Or even v}}}}}y for program blocks (for programmers) shift-v is for lines, ctrl+v for blocks, very useful..
June 20th, 2007 at 1:30 am
k n…
shit-happens 3042813 This site contains relevant information about k n….
October 24th, 2007 at 11:19 pm
car tag ms…
news…
October 26th, 2007 at 11:06 pm
gem car e825…
news…
October 30th, 2007 at 3:05 pm
Thank you, I just wanted to give a greeting and tell you I enjoyed reading your material.
November 6th, 2007 at 11:16 pm
intruder luggage oem rack suzuki vs800…
news…
November 8th, 2007 at 5:00 am
plasencia car hire…
news…
February 24th, 2008 at 12:13 am
Howdy! Great site. Great content. Great! I can recommend this site to others!
March 13th, 2008 at 3:54 pm
I came accross this website today searching for any informations. I did not find them, but your site was very interesting.