Learn and use vim

Vim is a powerful text editor that is highly customizable and extensible, but can be rather demanding to get used to. Should you learn to use it?

The vim editor

Vi is an ancient text editor that dates back to the late 1970s, but is still standing strong today. Numerous programmers and system administrators use vi, or its improved version, vim as the primary text editor on a daily basis. Vim was estimated to be the 3rd most popular text editor in the 2015 Stackoverflow develop survey.

Vim (I will be using vim to refer to vi and vim hereafter since the later is more modern and more popular nowadays) is highly customizable and extensible, but with a reputation of having a rather steep learning curve. The same can also be said to its long-time "arch-enemy" Emacs. It is true that it requires quite some practices to get accustomed to the vim keys and the vim way’s doing things. But once you pass the initial difficult stage, the vim keys will be "hardwired" into your fingers, and become part of the muscle memory, that you sometimes would unintentionally try to use the vim keybindings in other softwares. In fact, some firefox/chrome plugins offer vim keybindings that allow you to navigate around your browser using vim keys. Many tiling window managers default their navigation keybindings to be consistent with vim keys. Some other text editors/IDEs support either natively, or via plugins, vim key mappings, such as Sublime Text, Emacs, visual Code and Jupyter Notebook. It is a really efficient mode of text editing design that can boost your typing/editing speed considerably, (even if you use a Mac butterfly keyboard).

There are numerous tutorials online that guide the new users into vim, some in the form of interactive games. I don’t think I can do any better than them, so I will not attempt to write an introductory tutorial myself. I would suggest click into the first few results in your search engine and go through them. The very 1st one will be inevitably more difficult, the 2nd one easier, and 3rd one even so. Make yourself a cheat sheet as you go through the materials, preferably in vim of cause, even if there are already many cheat sheets on the Internet. Try to use vim for your text editing tasks when you are not in a rush. Keep practicing for a month, I believe that would be enough to get you on track.

I feel that this post would be too boring without some demonstrations. So here are a few screencasts showing the vim editing in action.

Vim editing demonstrations

The action + modifier "grammar" of vim

Vim has many action keys as editing commands, for instance, d for delete, c for change, y for yank, t for (move the cursor) till something, v for visual selection, etc.. And such action commands can be used in combination with a modifier that defines how the action should be performed, like an adverb modifies/defines a verb.

This video clip shows such action + modifier "grammar" in action:

  • The ci( sequence is: c (for change something) + i (for inside something) + ( (the parentheses). So it translates to "change things inside the parentheses".
  • Similarly, the ca` sequence is "change things around the pair of ` symbols. The difference is that a (for around) is used rather than i (for inside).
  • After some editing, the lines get a bit messed up. I used a gp} sequence to re-wrap the lines. The length of the wrapped lines is defined by the textwidth variable, which you can customize in the .vimrc configuration file.
  • Then I moved the cursor into the paragraph, and used cap to delete the entire paragraph. To read the sequence, c is again change, a for around, and p for paragraph.
  • Then I moved the cursor to the beginning of Line 387, and used vt( to do a visual selection from the cursor position till the opening parenthesis ( . After being selected, a single x deletes the selected texts.
Video Clip1. Action + modifier grammar of vim.

The "g" command

This video clip shows the action of the g (global) command. There are a number of lines with the pattern <a id="orgxxxxxxx"></a>. I noticed this when editing the markdown export from the org file (a specific type of mark-up file type supported by the Emacs editor. The detail is not important here.)

Instead of moving around the file and deleting such lines one-by-one, I used a g command to delete them all in one go:


The syntax of g command is:


Therefore, the pattern here is ^<a.*a>$, a regex expression that defines a line pattern that starts with a <a, followed by whatever things (.*), and ended with a>.

Lastly, the d command deletes all matching lines.

Video Clip 2. Use the “g” (global) command to delete all matching lines.


Sometimes we want to align up some texts for better readability. In this example, I used a vim plugin, i.e. tabular to achieve this. You can specify a character to align against, in this case, the = symbol.

Video Clip 3. Align up lines using the `tabular` plugin.


Macro allows one to record and repeat some actions. In this case, I have a list of emoji definitions. What I’d like to do is to delete the Unicode and the parentheses (e.g. (U+1F600)), and add 4 spaces between the emoji and the description.

To do this, I moved the cursor to the 1st character of the 1st line. Then I typed qa in normal mode to start recording the actions to the register a. Then I did a sequence of actions to format the 1st line into the desired layout, before placing the cursor to the beginning of the next line. Then q in normal mode stops recording. Finally, the :%normal @a command is used to repeat the recorded action till the end of the file.

Video Clip 4. Use macro to repeat custom actions.


This video clip demonstrations the usage of snippets. If you find yourself repeatedly typing in some code blocks that follow a given pattern, you can use a snippet system to make it into a snippet/template, and bind that to a shorthand for quick insertions. The snippet plugin I used is called Ultisnips, which also supports definitions of dynamic snippets. Notice when I type and expand the ;readvar1 snippet key, it expands into a code block where the variables have a suffix 1. If I used ;readvar3 instead, those variables will all have a 3 suffix. Ultisnipts allows one to embed Python code in the snippet definition, whereby giving great customization power to the user.

Video Clip 5. Use snippets for quick code insertion.

Should I learn vim?

I regard learning to use vim as a kind of long-term investment. Its steep learning curve means that it won’t help your coding/writing work positively in the 1st week, or even the 1st month with vim. It is not difficult to learn, but requires your constant practices. Just like you learned to type on a keyboard, once you pass the muscle memory stage, your fingers will start to communicate with the vim keys directly, letting you focus on your thoughts and ideas. Then you can explore the vim customizations and plugins. There are a huge number of plugins designed to ease all sorts of aspects of your vim usage.

Vim is a big subject, one learns something new about vim along the way even after years of daily usage. Vim has its own programming language, vimscript, which can be used to program its own functionality. You can even call Python code in vimscript if it feels easier to reason about the logic in Python (vimscript code can be quite cryptic).

If the time and the dedicated practices sound too luxurious a requirement to you, and you want some good text editor that you can get on going right away, then vim, and for the same reason Emacs, are not the best choices. Sublime Text, Visual Code or Notepad++ are probably more suitable alternatives. When you are not in hurry and have some free time for some mental gyms, vim is a good subject to pick. I learned to use Emacs during the lockdown time of the Covid-19 breakout, and now I’m using both vim and Emacs in my everyday work.

Leave a Reply