Written by Amar Panjwani
Technical Writer
This tutorial describes how to open, edit, and save a plain text file using the Vim text editor in a command line interface (CLI).
Vim offers a rich selection of keyboard-based shortcuts that make editing, inserting, deleting, and searching for specific use-cases speedy and efficient.
Derived from the Vi text editor, Vim stands for "Vi IMproved".
Notable upgrades include:
- Multi-Level Undo
- Multiple Windows Mode
- Syntax Highlighting
Vim has two main modes of operation: insert and command.
In insert mode, anything typed by the user is output to the opened file. However, the user can't give shortcut commands to alter text or return to the command line.
For example, a user would use insert mode to type up a paragraph. However, a user would and could not use insert mode to give a command like dl
to delete the line the cursor is on.
Press i
to enter insert mode.
Press ESC
to exit insert mode and enter command mode.
Note: Insert mode can be verified if an "-- INSERT --" message is shown in the bottom-left hand corner of the screen.
In command mode, anything typed by the user is processed as shortcut commands to alter the contents of the file in some shape or form. While in command mode, Vim does not input plain text into the file itself.
For example, a user would use command mode to input a command like dl
to delete the line the cursor is on. A user would not use command mode to type up a paragraph.
- Create a new file and open it in Vim.
- a. Type
vim note.txt
- a. Type
- Enter input mode.
- a. Press the
i
key.
- a. Press the
Note: The -- INSERT -- status message in the bottom-left corner signals that you can now insert text.
- Write in some text.
- a. Type in a message (Example: I love Linux.)
- Exit insert mode.
- a. Press the
ESC
key.
- a. Press the
Note: The -- INSERT -- message has now dissapeared.
- Save the file (write it to disk).
- a. Type
:w
+ENTER
- a. Type
- Exit (quit) Vim and return back to the command line.
- a. Type
:q
+ENTER
- a. Type
Note: Alternatively, typing :wq
+ ENTER
writes the file and quits to the command line is one step.
The best thing about Vim are its shortcuts.
Warning: Vim is case-sensitive, meaning it interprets the same letter as two different commands based on upper or lower casing.
While navigation using the directional cursor keys or mouse is supported, it is recommended to use the following letter keys for quicker access to keyboard-based shortcuts:
Event | Command |
---|---|
Up | k |
Left | h |
Down | j |
Right | l |
Scroll up half a window | CTRL-U |
Scroll down half a window | CTRL-D |
Move the cursor over the desired text and use the following:
Note: The following commands can also be prepended with a number for a multiplied effect.
Event | Command |
---|---|
Delete character | x |
Delete word | dw |
Delete line | dd |
Undo last operation | u |
Redo last operation | CTRL-R or :redo + ENTER |
Event | Command |
---|---|
Insert blank line above and enter insert mode | O |
Insert blank line below cursor and enter insert mode | o |
Event | Command |
---|---|
Force quit without saving | :q! + ENTER |
Display line numbers | :set number + ENTER |
Turn off line numbers | :set nonumber + ENTER |
View help guides | :help |
Move the cursor to the...
Event | Command |
---|---|
next occurrence of a specific character on the current line | f + character |
previous occurrence of a specific character on the current line | F + character |
beginning of the next word | w |
beginning of the previous word | b |
end of the next word | e |
beginning of the next sentence | ) |
beginning of the current sentence | ) |
beginning of the next paragraph | } |
beginning of the current paragraph | { |
top line of the screen | H |
middle line of the screen | M |
bottom line of the screen | L |
Event | Command |
---|---|
Find the next occurrence of a string | / + string + ENTER |
Find the previous occurrence of a string | ? + string + ENTER |
Repeat the last search | n |
Repeat the last search in the opposite direction | N |
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
Copyright ©️ 2021-2022