vi Text Editor #
vi is a powerful and widely-used text editor in the Unix/Linux world. Vim, which stands for Vi IMproved, is an extended version of vi with additional features, making it even more powerful. Many Linux distributions come with Vim installed by default, and you can still access it using the vi
command.
Opening & Closing Files #
To open a file with Vim, simply type vi
followed by the file name:
vi myfile.txt
If the file does not exist, Vim will create a new one with the specified name once you save your changes.
To close Vim, press the Esc
key (to ensure you’re in Command mode), then type :q
and hit return. If you have unsaved changes, Vim will prevent you from closing. To close without saving, type :q!
. To save your changes before closing, type :wq
.
Vim modes #
Vim operates in different modes designed for specific tasks:
Command mode → This is the default mode when you open Vim. You can navigate, delete, copy, paste, and perform other editing tasks in this mode. Press Esc to return to Command mode from Insert mode.
Insert mode → In this mode, you can insert text into the file. Press i to enter Insert mode from Command mdoe.
Vim commands #
Navigating #
Commands | Description |
---|---|
h j k and l | Navigate left, down, up, and right |
gg | Move to the beginning of the file |
G | Move to the end of the file |
:123 | Jump to line 123 |
75% | Jump to line located at 75% of the file |
Editing #
Commands | Description |
---|---|
i | Enter Insert mode before the current cursor position |
a | Enter Insert mode after the current cursor position |
o | Insert a new line below the current line and enter Insert mode |
O | Insert a new line above the current line and enter Insert mode |
dd | Delete the current line |
yy | Copy the current line |
p | Paste the copied content after the current line |
P | Paste the copied content before the current line |
u | Undo the last change |
Searching and Replacing #
Commands | Description |
---|---|
/pattern | Search for a pattern in the file (press n to move to the next match and N to move to the previous match) |
:%s/old/new/g | Replace all occurrences of “old” with “new” in the entire file |
:s/old/new/g | Replace all occurrences of “old” with “new” in the current line |
Saving and Exiting #
Commands | Description |
---|---|
:w | Save |
:q | Quit (will not close if there are unsaved changes) |
:q! | Force quit without saving changes |
:wq | Save changes and quit |
:x or ZZ | Save changes (if any) and quit |
This is just a brief introduction to Vim. There are many more features and commands available.