Basic Vim Commands

Because you've accidentally opened Vim and don't know how to exit
John John (304)
5 minutes

What is vim?

Vim is a text editor. What distinguishes Vim from other text editors you've probably used is that is it is a command line text editor. While there are GUI versions of Vim, it's most powerful when as a TUI (text user interface) application. This basically means that Vim runs inside your Terminal and is controlled by text commands from your keyboard, not from your mouse.

Fans of Vim enjoy it for three main reasons: ubiquity, efficiency, and it’s better than emacs.; meaning, it's everywhere and it helps you work faster. The latter is true because it encourages the user to control the application from home row, meaning your hands never leave the keyboard.

The main problem with Vim is that there is a steep learning curve. Generally, new users open Vim, can't figure out how to type, and then can't figure out how to quit! Getting started with Vim requires memorization (or easy access) to a handful of commands. Hence, I've written this guide.

Stick with it

It's worth it. You might feel overwhelmed by the plethora of features, but don't be. Vim efficiency follows the Pareto Principle (80/20 rule). The majority of efficiency gains are achieved through a small percentage of the features. This guide is designed to get you from zero to "over the hump". And from experience, if you commit to Vim, there's a 1-2 week hump before you start feeling comfortable. So stick with it!

If you're totally new, check out our guide on how to get started with Vim.

Posted in these interests:
h/vim23 guides

Before we get started, we must discuss modes. There are a few modes in Vim: insert mode, command mode, and visual mode.

To enter insert mode, you simply type i while in command mode. To go back to command mode, hit esc (the escape key). It's that simple.

To enter visual mode, you guessed it: v.

Command mode

You can think of command mode as the default mode. Typically, when you open Vim, you open to command mode, and this is where most of the interesting work is done. Most of the commands you'll see in this guide are to be performed while in command mode.

Insert mode

Insert mode lets you type. New users will spend much of their time in insert mode because this is what they're familiar with. But the more you learn Vim, the more you'll work from command mode. Insert mode will be used solely for adding text. Then almost everything else will be done from command mode.

Visual mode

Visual mode allows you to visually select a block of text, and then execute Vim commands on that block.

vim exit meme

Chances are, if you've jumped right into Vim without any guidance, you got frustrated very quickly. Then you tried to exit.

Here are the most common ways to exit Vim.

Command Description
:q Quit.
:q! Quit and discard changes.
:wq Write and quit (or save and quit).

Notice the colon prefixing both commands. These commands must be run in command mode.

As discussed in step one, there are multiple Vim modes. Vim is (typically) opened in command mode, and to start entering text, you'll need to enter insert mode.

This one is easy to remember, i for insert. Here I will also note two other related commands I use frequently.

Command Description
i Enter into insert mode.
o Add a new line beneath the cursor, and enter insert mode.
A Put the cursor at the end of the line, and enter insert mode.

Once in insert mode, your keyboard begins to act like a keyboard again; things you type actually show up on the screen. 🤣

Beginners will want to navigate using the arrow keys, and while this works the more efficient way to navigate is to use h, j, k, and l. Navigation is one of the most common things you'll do, so these keys allow you to stay on home row.

Direction Key
left h
down j
up k
right l

Bottom line, this takes a while to develop muscle memory, but once you do, you'll rejoice. I recommend Vim adventures to help learn Vim navigation.

Fortunately, there are a few commands that help us navigate more than one character or line at a time.

Command Description
gg Move to the top of the file.
G `Move to the end of the file.
ctrl-d Move down a half page.
ctrl-u Move up a half page.

This stop provides a list of commands to help delete unwanted text. Each command is executed from command mode.

Command Description
x Delete one character ahead of the cursor.
dd Delete the current line.

Remember, in Vim copy = "yank".

So, for copy and paste, think y and p.

Command Description
yy Yank the current line.
p Paste the last copied at the cursor.

To copy specific text, press v to enter Visual mode. Use the navigation keys to select the text you want to copy. Then press y.

This may not seem like a beginner Vim command, but I believe it's essential to get the most out of Vim.

Command Description
/search_term Search for occurrences of "search_term" in the current file.
n After searching, move to the next occurrence.
Search and replace
Command Description
:s/old/new/g Replace "old" with "new" across the whole file.
:s/old/new/gc Same as above, but confirm each time before replacing. Press "y" to replace.

I won't go into more detail here, but you can do MUCH more with this command once you learn how regular expressions work.

This isn't exactly a command, but I want to share a pattern that is used frequently in Vim. To repeat a command n times, you can often type n before the command.

Here are a few examples:

Command Description
10dd Delete 10 lines.
5x Delete 5 characters ahead of the cursor.
100p Paste something 100 times.
John John (304)
10 minutes

Learning vim can be quite intimidating, but it's well worth it. Once you master vim, you'll be editing text more efficiently than ever.