A Vim Journey: From Basic Editor to Productivity Powerhouse


I have been using Vim for several years now, but how I use it is very barebones. I only treat it as a basic text editor that I have to use when I SSH into a server because there is no GUI available. I’m often frustrated by how clunky it feels compared to modern IDEs and text editors, I don’t even know how to do basic text manipulation without resorting to the mouse.

However, I recently decided to embark on a journey to truly learn Vim and harness its full potential. Little did I know, Vim is much more than just a text editor; it’s a powerful tool that can significantly enhance productivity once mastered.

In this post, I will share my journey of learning Vim, the structure of Vim’s commands, and how to use it to its full potential. I will also share some tips and tricks that I have learned along the way. Let’s get started.

Mode

Vim has 4 modes:

  • Normal mode: The default mode. You can move around the file, delete text, and perform other operations. This is where you spend most of your time in Vim.
  • Insert mode: You can type text into the file. This is where I spent most of my time before I learned Vim properly and being frustrated by how clunky it is.
  • Visual mode: You can select text and perform operations on it. Think of it like taking a mouse and selecting text.
  • Command-line mode: You can enter commands to perform operations on the file.

Visual mode example: Press v to start visual mode, then use arrow keys or motions to select text. Once selected, you can press d to delete, y to yank (copy), or c to change the selection.

Command-line mode example: Press : to enter command-line mode, then type commands like :w to save, :q to quit, or :s/foo/bar/g to substitute text in the file.

Next we will explore why normal mode is where you spend most of your time in Vim. Since this is the default mode, understanding its language is key. To use it effectively, you need to learn the structure of Vim’s commands.

Structure of Vim’s Commands

When you Google how to do something in Vim, you will often see commands that look so cryptic and hard to remember. For example, to delete everything in a file, you will see the command ggdG. At first glance, it looks like a random string of characters that you have to memorize. However, once you understand the structure of Vim’s commands, it becomes much easier to remember and use them effectively.

Vim commands are structured in a way that is both powerful and efficient. The basic structure of a Vim command is as follows:

[count][operator][motion]

Let’s break down each component:

  • Operator (Verb): This specifies the action you want to perform like delete, change, or yank.
  • Motion (Noun): This specifies the text object or movement over which the operator should act. Delete how much? Change where? Yank what?
  • Count (Number): This is an optional number that can be placed before an operator or a motion to repeat it that many times.

Now let’s dive into each component in detail.

Operator (Verb)

An operator is a verb that specifies the action you want to perform. Common operators include:

  • d - delete
  • c - change (delete and then enter insert mode)
  • y - yank (copy)
  • p - paste (Note: This is a standalone command, it doesn’t take a motion like the others)

There are many more operators, but I don’t want to overwhelm you. You can always look up the documentation for more operators. A tip to remember them is try to map them to the first letter of the action. For example, d for delete, c for change, and y for yank.

Motion (Noun)

This is what makes Vim so powerful. A motion is a noun that specifies the text object or movement over which the operator should act. After you have mastered motion you will want to apply Vim philosophy to other editors and wonder why you didn’t learn Vim sooner.

Common motions include:

  • w - move to the beginning of the next word
  • b - move to the beginning of the previous word
  • e - move to the end of the current word
  • 0 - move to the beginning of the line
  • $ - move to the end of the line
  • gg - move to the beginning of the file
  • G - move to the end of the file

Other useful navigation commands:

  • /pattern - search for text in the file
  • n / N - go to next/previous search result
  • Ctrl-d / Ctrl-u - move half a page down/up

There are many more motions, but again, I don’t want to overwhelm you. This is what you learn gradually as you use Vim more and more, not trying to memorize everything at once.

Motions can also be combined with a (around) and i (inside) to specify text objects. For example:

  • aw - a word (including the space after it)
  • iw - inside a word (excluding the space after it)

This makes editing a large block of text very fast. For example, you have a variable:

let message = "Hello, World!";

How do you change the string content inside message? The old inefficient way is to move your cursor to the start of the double quotes, enter insert mode, hold backspace or delete key to delete message, type the new variable name, and then exit insert mode. This is slow, I will show you a faster way.

First, use f" to move the cursor to the first double quote (f is a navigation key to move around fast, I will talk about them in the next section). Then, use ci" (change inside double quotes) to change inside the double quotes. This will delete everything inside the double quotes and put you in insert mode. Now you can type the new variable value and exit insert mode. You saved a lot of time holding arrow keys and backspace/delete keys.

In the above example, I used f as a navigation key to move the cursor quickly. There are many navigation keys in Vim that can help you move around the file quickly without using the arrow keys. Some common navigation keys include:

  • f<char> - move to the next occurrence of <char> in the current line
  • t<char> - move to just before the next occurrence of <char> in the current line
  • ; - repeat the last f or t command
  • , - repeat the last f or t command backwards

Count (Number)

This one is quite simple. A count is an optional number that can be placed before an operator or a motion to repeat it that many times. For example, 3dw will delete the next 3 words.

The “Safety Net” and The “Dot”

Before we practice, there are two crucial features you must know:

  • Undo/Redo: Made a mistake? Press u in Normal mode to undo. Press Ctrl-r to redo.
  • The Dot Command (.): This repeats your last change. If you just deleted a word with dw, pressing . will delete the next word. It’s incredibly powerful when combined with motions.

Putting It All Together

Let’s try combining these concepts with a real example. Imagine we have this JavaScript function:

function greet(name) {
	const message = "Hello, World!";
	console.log(message);
	return message;
}

Instead of just reading, open Vim and try these edits with me:

  1. Change the message: Move to the line with message. Press f" to jump straight to the quote, then ci" to change inside them. Type “Hi, Alice!” and hit <Esc>.
  2. Delete the log: Move to the console.log line and press dd. Gone instantly.
  3. Copy the body: Place your cursor inside the function braces {}. Press yi{ to yank everything inside. Move to the end and press p to paste it.
  4. Delete multiple words: Go to the return line. Press 2dw to delete the next two words at once.

See how fluid that feels? You’re speaking a language of edits rather than dragging a mouse cursor.

Conclusion

This is getting quite long, so I will stop here for now to let you have time to sink in what you have learned so far. In the next post, we will dive deeper into advanced usage of visual and command-line modes. Stay tuned!