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:
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 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 allows you to visually select a block of text, and then execute Vim commands on that block.
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.
||Quit and discard changes.|
||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,
insert. Here I will also note two other related commands I use frequently.
||Enter into insert mode.|
||Add a new line beneath the cursor, and enter insert mode.|
||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. 🤣
This stop provides a list of commands to help delete unwanted text. Each command is executed from command mode.
||Delete one character ahead of the cursor.|
||Delete the current line.|
Remember, in Vim copy = "yank".
So, for copy and paste, think
||Yank the current line.|
||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
This may not seem like a beginner Vim command, but I believe it's essential to get the most out of Vim.
||Search for occurrences of "search_term" in the current file.|
||After searching, move to the next occurrence.|
Search and replace
||Replace "old" with "new" across the whole file.|
||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:
||Delete 10 lines.|
||Delete 5 characters ahead of the cursor.|
||Paste something 100 times.|