Monday, June 10, 2013

Vim Motion Commands

Hello again. Time for my first real "tech" post. For my first post, I feel that I should write about the one tool that I use for every software project, every programming language, and even writing notes and documents. Which tool is that? Well, as implied by the title, this tool is my favorite text editor. Vim.

DISCLAIMER: I am not going to participate here in the "holy wars" between all the text editors out there. I have not tried many alternatives, although I have tried a few. There are many really great text editors out that, and I encourage you to try a few. I happen to use vim because I like it. End of story.

For this post, I would like to discuss vim motion commands. These simple shortcuts are key to dramatically increasing my text editing speed. Let me give you a little run-down of what I'm talking about.

At the most basic level, motion commands are just simply keyboard shortcuts which can be used to navigate around in a file. If you've used vim at all, you'll know about the basic left/right/up/down commands, respectively, the keys 'h', 'l', 'k', and 'j'. These basic motions are supplemented by a great deal of very useful commands which allow you to move around much more quickly. Here are a few examples which I use daily:
  • w - move to the beginning of the next word
  • W - move to the character just after the next space (subtly different from w)
  • b - move to the beginning of the previous word
  • B - move to the character just after the previous space (subtly different from b)
  • % - move to the matching character. I.e. if I am currently on a '(' character, move to the matching ')'
  • t<character> - move to the character just before the next `<character>` (i.e. the command 't,' would move to just before the next comma)
  • f<character> - similar to the above command, but move the cursor to `<character>`, rather than the character just before it
And there are many more that are useful as well.

Now the real point of this post is this: these characters are not only useful for moving around in a file. Most of the time, I'm using these motions in conjunction with other commands. For example, if I want to delete everything up to the next comma, I will simply type 'dt,'. Broken down, this means "delete to ,". Simple, eh? Now, if I want to delete everything up to and including the comma, I will instead type 'df,'. This means "delete find ,". Doesn't make quite as much sense when you say it out loud, but you get the picture.

Finally, there are certain motion commands that only really make sense in the context of performing some command on the text, but they are extremely helpful. Let me give you an example.

Say I want to replace all the text inside of the current parentheses (i.e. my cursor is between a pair of parentheses). I could go back to the opening parenthesis, type 'ct)' (change to ) and start typing right? Well, yeah that would work. But another way to do it is to type 'ci('. This means "change inner (". This will remove everything inside the parentheses and place your cursor in between them in insert mode. Give it a try. It's pretty neat.

Of course, there are many variants of this. Some examples to give you a taste:
  • ciw - change inner word
  • da( - delete outer (  --  no, that's not a typo. the letter is an a, but I sorta pretend in my head that it stands for outer. Hey, whatever works, right?
  • vi{ - visual select inner {
And so on.

So there are a few thoughts on vim motion commands. Sorry if this first post has ended up being a little lengthy. Still getting use to the whole blogging thing. But let me stress this: learning to use vim motion commands will dramatically increase your workflow. No more holding down the arrow keys to move over to where you want to go. No more wasting time fussing with the mouse trying to select exactly what you want, accidentally deleting the opening parenthesis by mistake, having to type it back in, etc. Learning vim motion commands is definitely worth taking the time to do.

How about you? Any vim motion commands or other neat tricks that speed up your workflow? Leave a comment below with your thoughts (assuming that by the time you read this I've figured out how to enable comments...)

No comments:

Post a Comment