The vi editor is a powerful text editor available in most Unix-like operating systems, including Linux. It’s often considered a standard tool for editing text files in the command line interface. Here’s a basic tutorial to get you started with vi:
What is Vim?
Vim is an acronym for vi improved. It is a free and open-source cross-platform text editor. It was first released by Bram Moolenaar in 1991 for UNIX variants.
Vim is based on the original Vi editor, which was created by Bill Joy in 1976. In the 90’s, it started becoming clear that Vi was lacking in some features when compared with the Emacs editor. So Bram implemented many missing features and released it under the name Vim.
Starting the vi Editor
The following table lists out the basic commands to use the vi editor −
Understanding Modes:
Vim has different modes for different tasks. The three main modes are:
- Normal mode: For navigation and executing commands.
- Insert mode: For inserting and editing text.
- Visual mode: For selecting blocks of text.
Navigation:
h, j, k, l: Move left, down, up, and right respectively. |
w, b: Move forward and backward by a word. |
0, $: Move to the beginning and end of a line. |
gg, G: Move to the beginning and end of the file. |
Editing:
i: Enter insert mode before the cursor. |
a: Enter insert mode after the cursor. |
o, O: Insert a new line below or above the current line. |
x: Delete the character under the cursor. |
dd: Delete the current line |
yy: Yank (copy) the current line. |
p: Paste the yanked or deleted text |
Saving and Exiting:
:w: Save the file. |
:q: Quit Vim. |
:q!: Quit Vim without saving changes. |
:wq or :x: Save and quit. |
Visual Mode:
When editing text with Vim, visual mode can be extremely useful for identifying chunks of text to be manipulated.
Vim’s visual mode has three versions: character, line, and block. The keystrokes to enter each mode are:
v: Enter visual mode. |
V: Enter visual line mode (select entire lines). |
Ctrl+v: Enter visual block mode (select rectangular blocks). |
Practice:
Vim is all about muscle memory. Practice regularly to get comfortable with its commands and shortcuts.
Remember, learning Vim takes time and patience. Don’t get discouraged if you find it challenging at first. Start with basic commands, and gradually incorporate more advanced ones as you become more comfortable. Happy editing!