Cyrus Stoller home about consulting

Getting started with vim

Choosing a text editor is intensely personal; it’s all about preference. This post isn’t meant to persuade you that vim is better than other text editors. Instead, view this post as a practical guide for getting started with vim if you decide that you want to give it a try.

As background, I’ve tried a lot of text editors. In college, I used emacs for my CS courses. And later, I switched to TextMate and then Sublime v2 and v3 during the long TextMate v2 release cycle. With friends trying to convince me to try VS Code, I decided to give vim another try instead. The goal of this post is help you to start feeling productive using vim. I’m not a vim expert, but understanding the commands described below has helped me understand why people get so excited about vim.

Getting started

I started by completing the vim adventures game, which piqued my interested. I was surprised by how much faster I could move by avoiding the arrows on my keyboard. The game does a great job of making the process of learning a new way of thinking about text fun and engaging. Next I read through vimtutor and this high level tutorial on how to get started with vim, both of which helped me see how to do most of the things I took for granted when using emacs. But even with these resources, it was hard to internalize such a different way of interacting with text. I wish someone would have shown me this cheatsheet and this poster earlier. Both were valuable resources that helped me to avoid the temptation of going back to one of the other text editors that I’m more familiar with.

It’s true that the vim docs are surprisingly detailed and clear when you type :help, but I was looking for a more structured approach to my learning than people just telling me to RTFM. So, that’s what I’ve tried to put together here. This is far from comprehensive, but it should be enough to help you feel comfortable with the basics and help you understand what makes vim different from other text editors.

Let’s get started. I’d recommend starting by using macvim, so you can still use the mouse and other operating system shortcuts. As you get more comfortable, I’d recommend transitioning to using vim directly in the terminal so that your experience is the same when you’re using vim on your local machine and when using it over ssh, but don’t feel a need to rush that.


Part 0: Concepts and notation

For this tutorial, I’ll use the following notation:

Modes

In vim there are 4 main modes that I use:

  1. Normal mode - this is what sets vim apart from other text editors and what I’ll be focusing on in this tutorial. This is the default mode that vim opens with and the mode that I’ll assume you will spend the majority of your time in. If you’re ever unsure about how to get back to normal mode, press <ESC>.
  2. Insert mode - this is the typing experience that most people are accustomed to. When you press letters, they show up on the screen. Below I’ll describe the many ways to enter insert mode from normal mode.
  3. Visual mode - this is where you can use the navigation from normal mode to select text. This is probably the mode that I understand how to make use of the least so far.
    • Visual v - typical text selection
    • Visual Line V - select one line at a time
    • Visual Block <C-v> - select one column at a time
  4. Commandline mode - this is where you can search and replace, save and quit, and execute other commandline utilities. Generally to enter commandline mode, type :.

File I/O and closing vim

Now that you have a basic understanding of how to open and close vim, I’ll explain how to get started in four parts. First, we’ll go over basic navigation, then we’ll go over how to arrange your workspace, how to manipulate text, and finally how to install plugins.

This is the end of Part 0 on concepts and notation.


Part 1: Basic navigation

To get started, you need to know how to move the cursor in normal mode. This is well covered in the vim adventures and in :h navigation.

Cursor movement

Search by character

Search by pattern

Scrolling

Scrolling relative to the cursor

Transitioning to insert mode from normal mode

This is the end of Part 1 on basic navigation.


Part 2: Making changes

To make the most of normal mode in vim, you need to compose operators and motions. In general, motions are what we discussed above, but there are a couple more useful ones that I’ll share once we’ve covered the common operators.

[operator] [count] [motion]

Before learning about operators, let’s look at who we can use this with the motions we already know. If I type 3j, vim will interpret this as jjj, in other words moving down three lines. Similarly, if I type 5w, vim will move forward 5 words. And to move forward to the second “x”, I would type 2fx. At this point, I had my first “aha!” moment. Even with just this amount of knowledge under my belt, I was able to move around text files much faster than before.

Operators can be thought of as verbs in language that describes how you want to edit text. For more information go to :h operator.

Now that you know about operators, you can combine them to uncover the power of normal mode. For example, to delete 3 words, you would type d3w. But, if you’re combining operators with motions, there are two more types of motions at your disposal.

This type of motion can be used with words, brackets, parenthesis, quotes, etc… So you could type ci" to change the text between two " marks, assuming that the cursor in the text between the quotes, or yi( to yank the text inside a pair of parentheses. Once I started to get comfortable with this, I started to understand why people get obsessive about learning vim.

Copy and paste

At this point in my learning, I could quickly move around a document, but I couldn’t move text around. Vim has a more advanced version of copy and paste in that you can have many registers that can store text. In other words, you’re not constrained to a single clipboard like you are in a typical text editor. But, to get you started here’s how you can accomplish the familiar version of copy and paste.

Quick changes to text

Autocomplete

Following a code path

This is the end of Part 2 on making changes.


Part 3: Arranging your workspace

A buffer is the in-memory text of a file.
A window is a viewport on a buffer.
A tab page is a collection of windows.

As described in the vim docs :h window, these are the three main concepts that need to be understood to arrange your workspace.

Buffers

Vim allows you to open multiple files at a time, each being put into its own buffer. Before I understood how to work with buffers, I would quit vim each time I needed to work on a different file, which was highly inefficient. Even though I could move around files faster than I had been able to before, the benefits didn’t seem worth it.

Windows

Tabs

You can use tabs like in other text editors (e.g. Sublime). To learn more about how to reorder tabs, check out :h tabmove.

This is the end of Part 3 on arranging your workspace.


Part 4: Manipulating text

Common manipulations

Registers

Like the kill ring in emacs, vim allows users to hold multiple strings of text in registers to be used later. There are registers for every character on the keyboard, but a couple of them have special properties. For more info, read :h registers, but the basics are:

Macros

Macros allow you to program an arbitrary sequence of keystrokes that you can repeat whenever you’d like, allowing you to repeat complicated text manipulations on multiple lines of text in quick succession. Since macros are just recorded keystrokes, you can make revisions to the macro and then store it back into the same register.

For me, macros are one of the key features that sets vim apart. Because they’re so quick and easy to write, I use them regularly, allowing me to accurately refactor code and make what would otherwise be tedious changes without mistakes.

Repeated commands

Abbreviations

This is a great way to avoid having to type long words and to automatically correct common misspellings in vim. To learn more about this read :h abbreviations.

Marks

Marks allow you to quickly move the cursor back to a specified location. Lower case letters are used for marks that are local to a particular file, while capitalized letters are used for global marks. For more information go to :h mark-motions.

Commenting multiple lines of code

One of the harder parts of transitioning to vim has been getting used to the fact that there isn’t a built-in command for commenting multiple lines of text like CMD+/l in TextMate or Sublime. Here’s how I comment out multiple lines of code in a language like Ruby.

  1. Go to the column I want to insert my comment character(s) in (e.g. in Ruby, a #).
  2. <C-v> to switch into visual mode
  3. Select as many lines as I’d like to comment out
  4. I to switch into insert mode
  5. Type the comment character(s), in this case #
  6. <ESC> to return to normal mode

To uncomment multiple lines of code I use a similar process:

  1. Go to the column with the comment characters
  2. <C-v> to switch into visual mode
  3. Select as many of the comment characters as I want to delete
  4. x to delete the selected characters and return to normal mode

This works well for many languages, but is a pain when trying to comment code in languages like HTML/XML that require opening and closing tags. There are vim plugins to make this process easier, but this is the best solution I’ve found for plain vim.

Shell commands

It was a pleasant surprise for me to learn that I can use shell commands to edit text in vim. For example, if I want to sort an unordered list into alphabetical order, I can do the following:

  1. Select the lines that I’d like to modify: enter visual mode by using V and then move the cursor until the desired lines are highlighted
  2. Press ! followed by the shell command to execute a shell command with the selected lines as an input. In this example, I would type ! sort

Spellcheck

When editing markdown files, it’s nice to be able to check that there aren’t any spelling errors. To do this, you can use the following:

To learn more about spell check in vim, read :h spell.

Discovering new commands

This is the end of Part 4 on manipulating text.


Part 5: My vimrc

For those who are unfamiliar, you specify your vim customizations in a file called .vimrc that you put in your home directory. While I’m still far from an expert on how to customize vim, I have made some modifications that have eased my transition process and saved me from unnecessary keystrokes. For convenience, I keep my vimrc on github, so it’s the same on every computer and server I use. Instead of just copying mine, I’d recommend building your own from scratch. The process will help you to solidify your understanding of how vim works.

There are three main things that I do in my vimrc.

  1. Setting variables
    • For example: I’ve added set hlsearch to highlight all of the search results.
  2. Mapping keystrokes
    • For example: I’ve added inoremap <C-e> <esc>$a to add some of my emacs muscle memory to insert mode.
  3. Installing plugins to bring some of the niceties that I’ve come to expect from other text editors, like colorschemes and automatching parentheses.

Be patient as you build your vimrc; it’s a highly iterative process that’ll never really be finished. I’d recommend asking others what they’ve put in theirs and searching through github for sample vimrc files. It’s a great way to learn.

Once you’ve established certain settings like your textwidth, you can make use of commands like gq, which reformats your text to fit within a specified number of columns. I used to grumble about how tedious it is to make text fit within the 79 character limit to comply with the team styleguide, but with this command, it’s just a matter of a few keystrokes.

This is the end of Part 5 on my vimrc.


Part 6: Installing plugins

Like with other text editors, vim has a vibrant community of plugins. These range from easier methods of file navigations (e.g. NERDTree, and Ctrl-p) to colorschemes to syntax highlighting. As with building your vimrc, I’d recommend adding them slowly, instead of adding them all at once. To manage the process of installing, updating, and removing plugins, I’d recommend that you use a plugin manager. I happened to settle on Vundle, but there are others (e.g. Pathogen, Plug, and NeoBundle) that I’m sure work just as well. Each allows you to define the plugins you want to use in your vimrc file.

As with many things on the internet, there’s basically a plugin for anything you can imagine. Before you spend too long customizing your vimrc, check that a plugin doesn’t already exist. I often add a new plugin when I’m starting to work on a programming project in a new programming language so I can pick up syntax highlighting. For example, after installing typescript-vim and tsuquyomi, getting started with typescript was pretty painless.

This is the end of Part 6 on installing plugins.


Conclusion

I hope that this post has helped you understand how to think about vim and given you an overview of the things you’ll need to learn to start feeling productive using it. Be patient; the learning curve is pretty steep. While I still have a long way to go, I feel like the effort has been worth it. If you have suggestions for things I can clarify or things that I should add, please get in touch. Happy hacking.

Category Tutorial