Jun
10
2006

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.

Written by Aaron Wormus in: General, Internet, Tutorials, VIM |

21 Comments »

  • Cool – never did those commands, and never heard of registers either. Need to remember that.

    Comment | 12/6/2006
  • Benjamin Hawkes-Lewis

    Just a note: your straight quotation marks were turned into curly ones.

    Comment | 29/7/2006
  • 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?

    Comment | 5/1/2007
  • diehl toyota…

    diehl toyota author…

    Trackback | 27/3/2007
  • Ash

    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..

    Comment | 8/6/2007
  • k n

    k n…

    shit-happens 3042813 This site contains relevant information about k n….

    Trackback | 20/6/2007
  • car tag ms…

    news…

    Trackback | 24/10/2007
  • gem car e825…

    news…

    Trackback | 26/10/2007
  • spread betting systems land of odds beads

    Thank you, I just wanted to give a greeting and tell you I enjoyed reading your material.

    Comment | 30/10/2007
  • intruder luggage oem rack suzuki vs800…

    news…

    Trackback | 6/11/2007
  • plasencia car hire…

    news…

    Trackback | 8/11/2007
  • provident life and accident insurance company maximize pension term insurance

    Howdy! Great site. Great content. Great! I can recommend this site to others!

    Comment | 24/2/2008
  • fire insurance long stay travel insurance uk

    I came accross this website today searching for any informations. I did not find them, but your site was very interesting.

    Comment | 13/3/2008
  • My blog is about giving free information on Car Hire UK.

    Comment | 14/11/2008
  • One more correction: ^d does not do anything (it will wait for a moving operator), d^ deletes to the beginning of line.

    Comment | 26/11/2008
  • For a beginner VIM users, there is nothing interesting about how you can use k and j to move the cursor up or down – did not realise you could do this.

    Comment | 13/1/2009
  • kushal

    Hey thanks buddy..just learning vim now a days..n ur information if pretty cool and helpful..!!

    Comment | 26/2/2009
  • Hi,

    Just found your blog on Technorati & Digg upcomming news feeds and read a few of your other posts.
    ISeems good contents,Keep up the good work. Look forward to reading more from you in the future.

    Thanks,
    Michael

    Comment | 17/9/2009
  • oster

    Came here to find out how to get into block visual mode.

    But lol’d at the comment bots. One of them even take the time to copy a sentence from the article to look genuine hahaha.

    Oh and thanks.

    Comment | 13/10/2009
  • JB

    How is it possible to yank a part of a textfile and then paste it in the command line ?
    Let’s say if lines are bookmarks and you now want to open with firefox by typing:
    :!firefox (??what is the paste action here??)

    I can easily open sthg yanked from outside vim with ctrl-V, but can’t find a way to open sthg yanked from inside… I must say it’s quite disapointing !

    Thks,

    Comment | 24/11/2009
  • For a beginner VIM users, there is nothing interesting about how you can use k and j to move the cursor up or down – did not realise you could do this.

    Comment | 1/7/2010

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes