Top Tricks to Excel at the Linux VI Editor :

The VI editor is a powerful text editor available on almost all Unix-like operating systems, including Linux. It is renowned for its efficiency and versatility, especially among system administrators and developers. Despite its steep learning curve, mastering VI can greatly enhance your productivity when editing files on a Linux system.

What is the VI Editor ?

VI (pronounced "vee-eye") stands for "Visual Interface." It is a modal text editor, which means it operates in different modes for inserting text, executing commands, and other operations. VI has been around since the 1970s and is part of the POSIX standard, making it available by default on most Unix systems.

Key Concepts of the VI Editor :

Modes :

VI operates in three primary modes:

  1. Normal Mode : This is the default mode when you open a file. In this mode, you can navigate the text, delete text, copy text, and more.

  2. Insert Mode : This mode is for inserting and editing text. You can enter this mode from Normal mode by pressing i, a, or other keys.

  3. Command-Line Mode : This mode is for executing commands to save, quit, search, and more. You enter this mode by pressing : from Normal mode.

Navigation :

Navigating within VI can be done using several keys:

  • h, j, k, l : Move the cursor left, down, up, and right.

  • 0 : Move to the beginning of the line.

  • $ : Move to the end of the line.

  • w : Move forward one word.

  • b : Move backward one word.

  • G : Move to the end of the file.

  • gg : Move to the beginning of the file.

Editing :

Editing text in VI involves switching between Normal mode and Insert mode:

  • i : Enter Insert mode before the cursor.

  • a : Enter Insert mode after the cursor.

  • o : Open a new line below and enter Insert mode.

  • O : Open a new line above and enter Insert mode.

Deleting and Copying :

VI provides commands for deleting, copying (yanking), and pasting text:

  • x : Delete the character under the cursor.

  • dd : Delete the current line.

  • d$: Delete from the cursor to the end of the line.

  • yy : Copy (yank) the current line.

  • yw : Yank the word under the cursor.

  • p : Paste after the cursor.

  • P : Paste before the cursor.

Searching :

You can search for text within a file using the following commands:

  • /pattern : Search forward for pattern.

  • ?pattern : Search backward for pattern.

  • n : Repeat the search in the same direction.

  • N : Repeat the search in the opposite direction.

Saving and Quitting :

To save your changes and exit VI, you use Command-Line mode:

  • :w : Save the file.

  • :q : Quit VI.

  • :wq : Save and quit.

  • :q! : Quit without saving.

  • :w filename : Save the file under a different name.

Commands of VI Editor :

Here’s a comprehensive list of common VI commands:

Navigation Commands :

bashCopy codeh      # Move left
j      # Move down
k      # Move up
l      # Move right
0      # Move to the beginning of the line
$      # Move to the end of the line
w      # Move forward one word
b      # Move backward one word
G      # Move to the end of the file
gg     # Move to the beginning of the file

Inserting Text :

bashCopy codei      # Insert before the cursor
a      # Insert after the cursor
o      # Open a new line below
O      # Open a new line above

Editing Text :

bashCopy codex      # Delete character under the cursor
dd     # Delete the current line
d$     # Delete from cursor to the end of the line
yy     # Yank (copy) the current line
yw     # Yank the current word
p      # Paste after the cursor
P      # Paste before the cursor

Searching Text :

bashCopy code/pattern   # Search forward for 'pattern'
?pattern   # Search backward for 'pattern'
n          # Repeat the search in the same direction
N          # Repeat the search in the opposite direction

Saving and Quitting :

bashCopy code:w         # Save the file
:q         # Quit VI
:wq        # Save and quit
:q!        # Quit without saving
:w filename # Save the file under a different name

Practical Examples :

Here are some practical examples to help you get started:

  1. Opening and Editing a File :

     bashCopy codevi myfile.txt
    
    • Enter Insert mode by pressing i.

    • Type your text.

    • Press Esc to return to Normal mode.

    • Save and quit by typing :wq.

  2. Deleting Text :

    • Delete a line by moving the cursor to the line and pressing dd.

    • Delete from the cursor to the end of the line by pressing d$.

  3. Copying and Pasting Text :

    • Yank a line by pressing yy.

    • Move the cursor to the desired location and press p to paste after the cursor.

  4. Searching for Text :

    • Search for a pattern by typing /pattern and pressing Enter.

    • Press n to move to the next occurrence.

Conclusion :

The VI editor is a powerful and efficient text editor available on almost all Unix-like systems. By understanding its modal nature and mastering its commands, you can significantly enhance your productivity in text editing tasks. With practice, the VI editor becomes an invaluable tool in your Linux toolkit. Happy editing! ✏️

Got questions or need further clarification? Drop a comment below. Happy redirecting and streamlining your Linux journey! πŸš€

Thank You πŸ™β€οΈπŸ˜Š.

0
Subscribe to my newsletter

Read articles from Raj Kumar Behera directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Raj Kumar Behera
Raj Kumar Behera

A πŸš€ Passionate Linux and Cloud Computing Student . 🌐 Enthusiast in DevOps and System Administration πŸ§‘β€πŸ’».