Vim is a free and powerful text editor that comes with your Mac. In this tutorial, I will show you the basics of this text editor.
You can open files in Vim like any command line editor.
If the file you want to edit is life.md, type in the Terminal window:
Vim will open in that terminal with that file loaded. Vim is a terminal program, not a graphical system program.
Basics of Vim Modes
Vim has four modes: Normal, Insert, Visual, and Command. Each mode shows it’s name at the bottom left of the status bar in the program.
When you start Vim, it’s in Normal mode. You can use all the command keys to navigate around the file and start editing. When you exit any of the other modes, Vim goes back to the Normal mode.
Vim is in Insert mode with the use of the a, A, i, I, o, and O commands. Once in Insert mode, the editor will stay in that mode until you press an Esc key. Every other key pressed is directly inserted into the file at the current cursor location.
Visual mode happens when you use a v, V, and Ctrl-v commands from Normal mode. In Visual mode, you can select text. As you use a navigation command, the area from the beginning of Visual mode to when you exit Visual mode is the selected text.
Anytime you use the : command in Normal mode, you will enter into Command mode. In Command mode, you can perform complex editing functions, file actions, or shell actions. Command mode is the only mode that does not display anything on the status line, but the command entered gets placed under the status line with anything else typed and the cursor.
Saving Files, and Closing Vim
In Normal mode, you can type ZZ to save everything and exit. You can also save the file with :w!. The : will put you into Command Mode, the w will write the file, and the ! forces the operation to write without questions. Or, you can type :wq or :wq!. The q quits the editor. You can also use :q! to quit without saving.
Basic Cursor Movements
In Normal mode, you move around the file and make specific edits to the file. The h key moves the cursor to the left. The l key moves the cursor to the right. The j key moves the cursor down a line, while the k key moves the cursor up a line. To move to the next word, use the w command. The previous word command is b.
If you want to move more than one space, word, or line at a time, type a number first and then the direction key. The cursor will move in that direction that number of times. For example, if you type 10j, the cursor will move down 10 lines.
By using the Command mode, you can switch the line numbering to absolute or relative:
- Absolute numbering mode is the normal: each line with a unique number in sequential order.
- Relative numbering mode shows the number of lines away from the current edit line.
To have absolute line numbering, you can use the :set number command. To not show line numbers, you use the :set nonumber command.
To set Relative numbering, type :set relativenumber. To put it back to Absolute numbering, type :set norelativenumber.
By setting both modes using :set number and :set relativenumber, your Vim will then show relative numbers for all but the current line. The current edit line will show it’s absolute numbering.
By using Relative numbering mode, you can quickly see the number of lines to move using the j or k commands. For example, to move to the line with List, you would press 2j.
To move to the beginning of a line, use the 0 (that’s a zero) command. To move to the end of a line, use the $ command. The gg command will move the cursor to the beginning of the file, while the G command will move to the end of the file.
The .vimrc File
You might prefer to always have the Relative line numbering, but it’s hard to always set it when starting Vim. That’s where the Vim configuration file is useful. In the terminal at your home directory, type
The .vimrc file is Vim’s configuration file. Any command you type in command mode can be added to this file. It will be ran every time Vim is started. In that file, use the i command to start inserting text. Now add the these lines and save it:
set number set relativenumber set hlsearch
Now, every time you open Vim it will have the mixed Absolute and Relative line numbering mode set with all search results highlighted as well. Highlighted search is useful in the next section. There is much more you can do with the .vimrc file, but that will have to wait for another tutorial.
Search and Replace
You can search by using the / command in Normal mode. By typing /This, you will see all of the This words highlighted as below.
By typing n, your cursor goes to the next occurrence of the search pattern. By using N, you can go back to a previous occurrence. The pattern given after the / can be any regular expression. Read the article Advanced Search and Replace With RegEx to understand regular expressions better.
In order to replace text, you have to make use of Command mode. In Command mode, the s command is for substitution in the current line, %s for substitution in the whole file, and <begin>,<end>s for substituting from <begin> line number to the <end> line number.
The format is /<search pattern>/<replace pattern>/gi where the <search pattern> and the <replace pattern> are standard regular expressions. In the example above, I am replacing every existence of This with That. The i after the g makes the search case insensitive. An I would make the search case sensitive. The g makes the substitution global in the line. Without the g, it performs the substitution once per line.
To insert text to the left of the current cursor location, use the i command. The a command inserts to the right of the current cursor location. The I command inserts to the front of the line, while A inserts to the end of the line.
The o command inserts a whole new line after the line the cursor is on and puts the editor into Insert mode at the beginning of that line. The O does the same, but adds the line above the current cursor location.
To delete characters, use the d command and then a direction to delete the character in that direction, or the space bar to delete the character under the cursor. If you prefix with a number, then Vim will delete that number characters in the specified direction. The dd command will delete the current line. The D command will delete everything from the current cursor location to the end of the line.
The x command will delete the cursor character. The X command deletes before the cursor. Both the x and X commands will take a number prefix to perform the action that number of times.
Copy, Cut, and Paste
When you press v in Normal mode, Visual mode begins. All cursor movements cause a selection from the beginning of Visual mode. When you have a selection, using the y command yanks or copies the selected text. Move to a new location and use the p command to paste after the cursor, while the P command pastes before the cursor.
After making a selection, the x command will delete the selection. Using the d command will cut the section so you can paste with the p command.
In order to select a block of text, you start with the <ctrl>-v command. The V command starts Visual mode selecting by lines and not characters.
Practice Makes Perfect
Now that you know the basic commands used in Vim, you need to practice. Vim Adventure is a great way to practice the Vim commands. It’s an adventure game that gets you practicing the different Vim commands in order to explore the adventure world.
Simple Tutorials Vim Reference is a cheat sheet to help you remember the most used Vim commands. Most of all, keep practicing and you will be able to master this simple, but powerful, editor.