The vi editor is a powerful text editor used in Unix-like operating systems, including Linux. It’s known for its efficiency and versatility, although it has a steep learning curve for beginners.
Here’s an overview of vi:
1. Modes:
Insert Mode – Allows you to insert and edit text.
Command Mode – Used for executing commands and navigating the file.
Visual Mode – Used to highlight and edit text in bulk.
2. Basic Navigation:
h, j, k, l – Move left, down, up, right respectively.
Arrow keys – Same as h, j, k, l.
:0 (zero) Move to the beginning of the current line.
:$ Move to the end of the current line.
:gg Move to the beginning of the file.
:G Move to the end of the file.
3. Basic Editing:
i – Enter insert mode before the cursor.
a – Enter insert mode after the cursor.
o – Open a new line below the current line and enter insert mode.
O – Open a new line above the current line and enter insert mode.
x – Delete the character under the cursor.
dd – Delete the current line.
yy – Copy (yank) the current line.
p – Paste the copied or deleted text after the cursor.
4. Saving and Exiting:
:w – Save changes to the file.
:q – Quit vi (only if no changes have been made).
:q! – Quit vi without saving changes.
:wq or ZZ Save changes and quit.
5. Search and Replace:
/pattern – Search forward for a pattern.
?pattern – Search backward for a pattern.
n – Repeat the last search in the same direction.
N – Repeat the last search in the opposite direction.
:s/pattern/replace – Replace the first occurrence of a pattern with a replacement.
:s/pattern/replace/g – Replace all occurrences of a pattern with a replacement.
6. Miscellaneous:
u – Undo the last change.
Ctrl + r – Redo the last undone change.
:set number – Display line numbers.
:set nonumber – Hide line numbers.
The vi editor is a versatile and powerful text editor that provides extensive features for editing text files in Linux. While it has a steep learning curve, mastering vi can greatly enhance your productivity and efficiency when working with text files in a terminal environment. Practice and familiarity with vi will gradually make you more proficient in using this essential tool for Linux system administration and development tasks.