DEV Community

Cover image for Understanding the Efficiency of vim
Pankaj Gupta
Pankaj Gupta

Posted on

Understanding the Efficiency of vim

Highlights of vim

  • Ubiquitous — Shipped with every NIX Platform.
  • Highly customizable — Can be tailored to your needs.
  • Lightning-fast.
  • Insanely efficient.

Basics

Before we start learning how vim works, I just want to mention some commands so that people who are new to vim can use it.

# Open a file in vim
vim textfile.text
# Come out of vim
[Esc]:q # quit without saving
[Esc]:wq # save and quit
[Esc]:q! # discard changes and quit
# Navigation
h => left
j => down
k => up
l => right
# Copy
[Esc]v[h|j|k|l]y # h,j,k,l to highlight a block
# Cut
[Esc]v[h|j|k|l]d # h,j,k,l to highlight a block
# Paste
[Esc]p
Enter fullscreen mode Exit fullscreen mode

Why vim

What distinguishes vim from other editors? It’s a modal editor that makes it so efficient. Unlike any other editors, that only have one mode which is insertion mode, you are constantly typing and streaming text into the buffer.

Now, for writing this is great, however, as programmers, what we care about most isn’t constant flow of text but editing and navigating. I think the best analogy to explain this would be:

You can imagine programmers as painters and painters don’t spend their time continuously doing one brush stroke, they prepare the canvas, mix up the colors, and then they do individual discrete strokes.

Vim achieves this with different modes. Here are the four modes to get started with vim.

4 different modes of vim

1. Normal mode

Here, we interact with text and tell it what to do. When we launch vim, this is the default mode.

Most people are used to just typing, but in this mode, we don’t insert text into the buffer, we do other types of activities like navigation. We can get to this mode by pressing Esc. This is the default mode in vim.

2. Insert mode

You can get into this mode using i: insert, a: append, c: change. This is the mode most people are used to. So, remember, if you open vim and want to type something, switch to the insert mode using the mentioned keystrokes.

3. Visual mode

This is just like normal mode, except it works with highlighted blocks of texts. Another problem people face is copying and cutting text, you can do that in this mode. You can get here from normal mode using V, v, .

4. Command-line mode

This is where you find texts, exit vim, navigate files, and whatnot.


Operators, Text Objects, and Motions

To use vim, you have to think in terms of Operators, text objects, and motions, then you will unleash the real power of vim.

1. Operators

These are the verbs of the vim language. They specify actions to perform on your text objects or motions and most of them are easy to remember.

Sample Operators

2. Text objects

These are the actual visual text on your screen.

Sample Text Objects

Again, very simple to remember: aw: a word. Now, here we can combine these with operators to see some powerful actions. Here are a few of my favorites:

Combined Operators and Text Objects

3. Motions

Motions are all about efficient navigation through the file that you’ve opened. Here are some of the most useful motions:

Common Motions

Let me explain how this works, suppose you press +, this would take you to the first non-blank character of the next line, but if you press 10+, it’ll take you to the first non-blank character of the 10th line from the current position.

Let me give you some more examples to make things clear.

Example Motions


Putting It All Together

[count][operator][text objects/ motions]
Enter fullscreen mode Exit fullscreen mode

This is the template for using all the vim commands. Your imagination is your limit. Here is a list of commands you can look at.

Here is the list of commands that I use the most:

My most used commands


Conclusion

This guide is just the beginning into the world of vim, but this guide here provides a basic understanding of how vim works, which is enough to get you excited about vim.


Resources

YouTube

Top comments (0)