A Weblog of Artem Dudarev

Powers of Vim

ADM-3A Keyboard
ADM-3A Keyboard Photo by Kent Chen.

Do you want to process words or edit text?

I use vim for more than 12 years, most of the time as my primary editor for text and code. This post is a summary of a short master class for Donetsk Coffee’n’code meeting in December 2013. More experienced vim users should probably go directly to the end to check collection of links.

Power of Modes

Quite often I find that it is much more efficient to focus on one task than to multitask and achieve nothing.

For example, here is one singletasking lifehack I use: In a given time, I typically participate in several projects. Last year, I found that it was very effective to create different accounts in my computer OS for such different roles. It did help to make processes much more efficient.

One of the most important concepts for beginners starting to use vim is that working with it we are also switching modes. In a given moment, we are writing, moving arround, searching and so on. When you embrace this separation into modes it does make text editing more effective.

When we just open vim we are in normal (moving) mode that allows us to move around efficiently. Learn the basic for moving by typing vimtutor from command line.

Here are some keys that I currently use for navigation in addition to standard ones quite often: G to go to the end of file, gg to go to the beginning of file, zz for centering.

Also remember to navigate by words (w, b) or other text concepts and with search of the current word (*, #) and search in general (/), which can be considered as a separate mode.

Tags are a standard feature of vim. Typically I create Makefile command for each project I work on that creates tags with ctags -R .. Navigation between tags: Ctrl-], Ctrl-T, :ts. Here and in many other cases :help tags is your friend.

The easiests way to go into insert (text entering) mode is with i key, one can go back with Esc, but it is very naive to use just that option. One can start editing from the beginning of line (I), from the end (A), create an empty line after and before current line and start editing (O, o), these options are very useful in many cases.

Editing Language

Do not try to imediately memorize as many keys as possible. Start with very minimum. After some time you will get familiar with more. Try to observe ineficiencies in your use and search for solutions.

Many vim keys are mnemonic, and after some time they will “make sense”. For example, similar to A that appends text at the end of the line, there is a that appends after the cursor.

Normal mode is not only for navigation but is also good for editing with concepts (words, lines, text objects, paragraphs, marks, motions etc.) And the way one communicates this editing commands is very similar to a language. Vim grammar has a fixed word order:

<command count><verb><motion count><modifier><object>

Counts and modifiers are not always relevant. So for the beginning you might want to keep a simplier picture in mind:

<verb><object>

The major verbs are y, d, and c (for yank, delete, and change).

Some motions objects are 0, $ for beginning and end of line; f<character> and t<character> for find the character and till the character. Normal search with / operator also can be used.

Text objects can be words (w), brackets ([, (), tags (t) etc. Modifiers are i and a to indicated if we take inner part of the object or the whole object.

Some examples:

  • ct) - change till the closing parenthesis
  • ci] - change inside square brackets
  • da] - delete square brackets
  • "+yit - copy what’s inside a tag to a + buffer that corresponds to system clipboard
  • dG - delete till the end of file

If you install surround plugin:

  • ys3w' - surround 3 words with single parenthesis

And here example in visual (yet another mode) is appropriate: <Ctrl-V><select text with motion commands>S" - surround it with double parenthesis.

The Wisdom of Crowds

Speaking of plugins.

The way plugins are organized is very different nowadays that it was ten years ago. Plugin managers such as Vundle and Pathogen are used by many.

I keep my settings and plugins in Github repo. You may find many more repositories like that. The process of setting up everything for a new account is described in another blog post.

Here are some plugins that I would like to mention (in the order of use frequency):

  • CtrlP - fuzzy file finder
  • Fugitive - git wrapper
  • NERDTree - file system tree explorer
  • Tagbar - displays tags in a side window for easy navigation
  • Emmet - improves HTML workflow

As with keys, do not try to install many plugins at once and start using all of them. Go incrementally.

The post above is a summary of a short talk about vim. If you’d like to go deeper, check out links below. They are a compilation of vim-related links from my Wiki, Twitter, Delicious. They are in no particular order. More important in my opinion are on top and highlighted in bold.

Tutorials

Cheetsheets

In general I would not recommend to use cheetsheets for learning vim, this one though is well organized.

Personal Experiences

Video

Python

Other

Comments