After spending five minutes with this Vim tutorial, you’ll walk away with the basic knowledge to edit text files using only your keyboard.
There are a few simple commands that you must remember in order to use Vim. To keep this tutorial as simple as possible, you’ll learn these commands only and nothing more.
Let’s get right into the tutorial.
How to Use Vim
Let’s says there’s a file that we want to edit called test.txt (right click and Save Link As to download this file and follow along).
To open this file with Vim, simple issue the following command in Terminal or your command prompt.
vim test.txt
This will open the Vim text editor program.
Using Insert Mode to Edit a File
Let’s make a couple changes to this file. First, let’s add your name to the third line. To do this, we must first enter insert mode. To enter insert mode, simple hit the i
key on your keyboard. Notice how the lower left corner of your window now says -- INSERT --
which means you’re in insert mode.
Now you can use your arrow keys to move the cursor down the third line and over to the period. Type your name here. Notice how although the cursor is on the period, the text is inserted before the period.
Lastly, let’s remove the last line in the file. Move the cursor down one line and all the way to the right with your arrow keys. This time, make sure the cursor goes to the right of the period.
Now you can simply use the backspace key on your keyboard to remove each character in this line. Make sure you hit the backspace key one last time to move the cursor up to the end of the third line to completely eliminate the last line.
How to Save a File in Vim
Now that we are satisfied with our changes, we will want to save them. To save a file in Vim, we must first get out of insert mode by hitting the Esc
key. Notice how the -- INSERT --
indication goes away.
Next, let’s enter command line mode by typing :
like this. Notice how a color appears in the bottom left corner of your window.
Here we have a few options. We can either save the file, save the file and exit Vim, or discard our changes and exit Vim. Here’s how you can accomplish each.
:w
save your file without exiting Vim:wq
save your file and exit Vim:q!
don’t save your file and exit Vim
We will want to save the file and exit Vim so type :wq
and hit enter to execute this command.
Basic Vim Commands Summary
There are essentially two modes that you must know in order to use Vim at a basic level.
- Insert Mode to edit a file. You can access insert mode with
i
- Command Line Mode to save a file and exit VIM. You can access command line mode with
:
If you get confused as to which mode you’re in, you can always hit the Esc
key to leave whatever mode you’re currently in.
When you are in insert mode, you can freely use you keyboard arrow keys to move around the file and type text as you normally would in a word processor.
When you are in command line mode, your three basic options are to:
-
- Save your file without exiting Vim with
:w
- Save your file and exit Vim with
:wq
- Discard changes and exit Vim with
:q!
- Save your file without exiting Vim with
If you can remember these simple commands and keyboard shortcuts, you will be able to edit any file from the command line with Vim.
2 Responses
Thank you so much! I had no idea how to use Vim, and was confused. This blog post helped me so much!
Awesome, so happy to help 🙂