← Impulso Blog

Be productive with Vim - Part 1: Essential commands

In this post, we summarize the essential, most basic commands in Vim, the no-frills editor that comes installed on most operating systems.

Laptop displaying code next to a window and a lit lamp.

Editorial translation by Impulso.

Vim

Vim is a text editor that runs right in the terminal, and over the past few weeks I’ve been learning a bit about how to use it. I decided to write this post to help what I’m learning stick, and also because there’s so little good material on the subject in Portuguese. What’s more, I’m writing it directly in Vim using Markdown, and it’s been a pretty interesting experience right from the start, even though I’m still less productive in Vim than in other full-featured, powerful editors. Still, there are good reasons to learn how to use it.

This article focuses on the Vim commands and features I already know and that could be considered “basic.”

I’ll link to the upcoming articles here so you can use this as a table of contents:

How to get the most out of this series

I suggest opening the editor, adding a bit of content, and trying out each command and feature described here. Things will go more smoothly that way, and what you learn is sure to stick better.

The main benefits of Vim

I see two main benefits to using Vim day to day:

  1. Vim runs in the terminal, so it’s a bare-bones and therefore lightweight editor. That’s great if you’re just getting started and might not have a powerful computer to work with. It helps keep memory and CPU usage down.
  2. Vim comes preinstalled on the major operating systems, and it’s usually already your system’s default editor (the one that opens when you edit configuration files by default).

But let’s get to the good stuff…

Using Vim

Essential commands

Let’s start with the essential commands. These are the ones I already knew before I started learning Vim in earnest: opening, creating, saving, and discarding changes. If you already know how to do those things, you can skip to the next section.

Create a file:

In your terminal, go to the directory of your choice and type:

$ vim meudia.txt

You can name your file whatever you like. meudia is just an example.

Enter INSERT mode

To enter INSERT mode, where you can add, delete, and edit the file’s contents as you’re used to doing, press the I key on your keyboard.

Type whatever you like just to see how it works. Write a few paragraphs, add some punctuation, and you’ll see that everything works as you’d expect. When you’re done typing, press Esc (Escape) to return to normal mode.

That brings us to a core concept in Vim. Vim works through commands you enter by pressing keys. The command I takes you into INSERT mode, and there are plenty of other commands. In normal mode, you can’t just type text. That might sound like a drawback now, but once you get good at Vim, you’ll find that when editing files, you use commands far more often than you type text.

Save the file

To close the file, type:to enter command-line mode, then use the command q to close it. If you do that now, though, Vim will tell you that the file has unsaved changes and that you need to save or discard them before you can quit.

To discard your changes, add the! character to the end of the command q, giving you:q!. You’ll notice that in the editor, an exclamation mark (!) is a kind of negation. To help it stick, think of it as “negating the changes.” This command lets you quit the editor without saving your edits.

But… what about saving, huh? Aren’t you going to tell us? Of course I am, and it’s easy.:w is the command to save your changes, so:wq saves your work and quits the editor. Think of it as “Write and Quit,” meaning write (save) your changes and close the application.

Oh, I haven’t mentioned yet that to run the command, you need to press Enter (or Return if you’re using an Apple computer).

Next chapter

As you’ve probably noticed by now, this post is going to get pretty long. So I decided to split it into chapters to make it easier to read and look things up.

In the next chapter, we’ll talk about navigation commands, also known as motions.

That’s all, folks! For now…

Related articles