Strong opinions, weakly held

Tag: Vim

The grammar of Vim

Like a lot of people, I have gone retro when it comes to editors, using Vim for most of my day to day work. I’ve been doing most of my development these days in a terminal window, logged directly into a VM where I test my code. For more, see this article.

I have been a somewhat casual vi user for at least 15 years, but using it for all my development work has made it more important to vastly improve my Vim skills in a short time. The best article on Vim I read was Yan Pritzker’s Learn to speak vim – verbs, nouns, and modifiers! It totally revolutionized the way I thought about Vim.

For my first 15 years with Vim I tried to learn it the same way you might learn a foreign language from a phrasebook. I had learned everything in a disconnected way. Use j to move down a line. Use cw to change a word. Use yy to copy the current line into a buffer. You can get by this way, but it’s a very simplistic approach.

Advanced Vim users understand that Vim commands are a language unto themselves, and that the key to being productive is understanding Vim’s grammar. Pritzker accurately identifies that grammar as consisting of the following parts of speech: verbs, nouns, and modifiers.

The nouns usually represent positions within the document. For example, 0 represents the beginning of the current line. $ represents the end of the current line. j represents the next line in the file. When you use most nouns by themselves in command mode, the implied verb is “go to”. So if you just type j you move to the next line. There are some exceptions. For example, in command mode, s means substitute a character. In other contexts, it’s a noun representing a sentence.

You can usually supply a quantity along with a noun. So 5j refers to the five following lines, and typing it will move your cursor down 5 lines if you don’t supply a verb. Likewise, the w noun represents a word, and moves to the next word to the right if you use it by itself. 3w moves three words to the right. The biggest problem with the way most people learn Vim is that people learn these things as “movement commands” rather than as nouns.

There are a couple of special nouns that I don’t see a lot of people using — f and t. f means “find the next occurrence” and t means “til the next occurrence” and both of them are applied to the next character you type. So typing f_ means “find the next underscore” (on the current line). t_ means “til the next underscore,” the cursor will stop on the character before the next underscore. (You can use F and T to search backward rather than forward.)

These are helpful for navigation, but they’re even more helpful when you’re constructing editing commands, because they can be used as a noun.

Now let’s talk verbs. The big three are y, d, and c. y is yank (copy). d is delete. c is change. Combining a noun and a verb applies the verb to the noun. That’s the essential grammar of a Vim command. cw changes the current word. c0 changes the text between the cursor and the beginning of the line. d$ deletes to the end of the line.

Let’s look at f a bit more deeply. Say you have a variable named my_integer_value and you want to change the name to my_string_value. If you move to the beginning of the name and type cw you’ll change the entire variable name. Instead you can type fi to move to the “i” in “integer”. Then type ct_ (change up to the next underscore) and the word “integer” will be deleted. You can then type in “string” instead.

Lastly there are modifiers. Modifiers change how nouns are understood. The two big ones are i (inside) and a (around). Here’s a simple example. If your cursor is in the middle of a word and you use the command cw it will change all the characters from the cursor position to the end of the word. If you type ciw it will change the entire word. You can also use paired characters as nouns with i. So to change the contents of quotation marks, you can type ci". Or to copy whatever is inside a pair of parentheses, you can type yi(. If you want to yank the parentheses themselves, you can use a instead — ya(.

This is just the tip of the iceberg, there’s a lot more. But once you know how to put these commands together, you’ll have a much easier time understanding the Vim tips you see online. Suddenly you’re expanding your vocabulary rather than learning a new language.

For example, once you understand the Vim as a language, you can get a lot out of Andrei Zmievski’s Vim for Programmers presentation, which is a whirlwind tour through PHP commands.

Vim is weird enough that it can seem like learning it is probably not worth the effort, but it’s an incredibly powerful tool in the right hands. The problem is that to even get started, you have to learn the difference between the various modes and how to switch between them. You also need to learn the basic methods for navigating through a document. If you’re used to a normal GUI editor, that can be really frustrating. Once you’ve mastered the basics, though, learning Vim’s grammar will really accelerate your efforts to make the most of it.

Navigating among tabs in Vim

Last week I mentioned that the only thing I was struggling with in my transition to Vim for editing code was juggling multiple open files at once. Switching from one file to another is really easy, especially if you’re working on a Rails app and using rails.vim. There are a number of really nice navigation features built in that make it easy to open any file from your project and to switch among related files.

This morning I noticed that these navigation commands all have “open in new tab,” “open in new split window,” and “open in new window” variations. So to open a model called “store.rb”, in command mode you just type:

:Rmodel store

To open that model in a new tab, you type:

:RTmodel store

Navigating among tabs using the keyboard is easy as well:

<tab number>gt

So, to switch to the third tab, you just type “3gt”. In command mode you can also just type “gt” to move to the next tab.

Suddenly, my problems are solved.

I’m also getting the hang of split windows, which I should have mastered years ago. They have two advantages over tabs, the first being that they make it easy to look at multiple files at once. The second is that you can use them in terminal sessions as well. Another nice thing about split windows is that you can split your view of one file so that you can look at one section of the file while you work on another.

I think the real lesson here is that not taking the time to master your tools is false economy.

Back in the saddle with Vim

I’ve been a vi user for many, many years. In fact, I used to program in C on a MUD using ed (vi is the fancy version of ed). However, it’s been a long time since I used vi as my editor of choice for development work. I use it for hacking scripts, editing files on servers, and general purpose text editing in a terminal window, but most of the time I use Eclipse for Java and TextMate for pretty much everything else.

A lot of people in the Rails community have migrated over to vim (and MacVim) in recent years, and I know there’s a very active community of programmers who are working hard to make it a better tool for developers. Anyway, I wanted to try out the Solarized color scheme people were talking about and Vim looked like the best option, so I decided to dive in, at least for Rails development.

I’ve been really impressed. Tim Pope’s rails.vim library for Rails developers is really, really good. The navigations features in particular are worth the price of entry. Then there’s the inherent goodness of Vim itself. Vim is difficult to approach initially, but it’s an incredibly powerful tool in the hands of someone who really knows how to use it. Even though I have been using it for a long time, I don’t really consider myself to be a Vim power user by any stretch of the imagination, because I’ve mainly used it to edit small files, one at a time.

Using Vim for project work is completely different, and that’s the area where my Vim skills need work. TextMate provides you with a file drawer with all of the files in your project, easy to use search across all the files in a project, and the ability to easily open multiple files in a tabbed editor and switch between them. The next step is to learn the tricks to accomplish the same things using Vim. Anyone seen a good tutorial?

Update: Just for fun, here’s my .vimrc.

Vim love

Joe Gregorio has written a short review of Vim as an alternative to Emacs. It’s interesting to read his thoughts on Vim (and the vi mode of operation in general), as they seem so natural to me now that I can hardly think philosophically about them. Even though I used Emacs for coding for quite awhile, for everyday editing I’ve depended on Vim for years. It launches quickly, has syntax highlighting for many more types of files than I would expect, and once you’ve mastered it, is absurdly powerful.

© 2017 rc3.org

Theme by Anders NorenUp ↑