Photo by Glenn Carstens-Peters on Unsplash
Understanding the Efficiency of vim
Why is everyone always talking about vim? Learn the basics of vim to finally start using it
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
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.
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, <Ctrl-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.
2. Text objects
These are the actual visual text on your screen.
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:
Remember you have to be in normal mode to use these hotkeys.
3. Motions
Motions are all about efficient navigation through the file that you’ve opened. Here are some of the most useful 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.
Putting It All Together
[count][operator][text objects/ motions]
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:
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.