Jun
10
2006
15

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.
(more…)

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

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