Basic vim commands

Basic Vim commands

Because you've accidentally opened Vim and don't know how to exit
Tyler Tyler (270)
0

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!

Posted in these interests:

vim
PRIMARY
23 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.

Search
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.
Tyler's profile pictureTyler
Joined in 2015
Software Engineer and creator of howchoo.
Related to this guide:
Vim undo branches
You probably already know how to use vim's basic undo and redo features. For a quick refresher, read this short tutorial on how to use vim redo and undo.
Dayne's profile picture DayneView
In these interests: vim
3 ways to use tabs in vim
Vim tabs can be a great way to visualize which files you currently have open. Many vim users get bogged down by them and either end up working too hard to use them or give up altogether.
Dayne's profile picture DayneView
In these interests: vim
Vim redo and undo
Learn how to undo and redo changes using vim. If you are brand new to vim, learn more about the basics in this intro to vim guide.
Dayne's profile picture DayneView
In these interests: vim
People also read:
Once you learn to think in text objects, you'll never be the same
Vim is a powerful tool. I often paste raw data into Vim in order to format it in a specific way, and sometimes I need to clean up the file by removing many blank lines.
One of the strengths of Vim is that it's highly customizable. And while remapping keys is possible, we often want to create shortcuts without changing the default key bindings.
If you find yourself highlighting text in Vim with your mouse to copy and paste it elsewhere, stop. There's a better way, using the yank command, to copy text into your clipboard on macOS or Windows.
Vim is amazing. Well, Python is doing all the work, but I still like Vim. Learn how to format JSON in Vim like a pro with this short guide! tl;dr Paste your json, and run this in vim normal mode. :%!
Tabs are evil. This guide will show you how to convert tabs to spaces in Vim.
Need to change the case of characters to all caps or all lowercase? This is easily done using Vim. This guide will show you how to change the case of characters in Vim.
Posted in these interests:
vim
vim
23 guides 16 subscribers 
PRIMARY
Explore
Discuss this guide: