Day 11: The King of Text Editing in Linux: A Comprehensive Guide to the Vim Editor

Vim (Vi IMproved) is a powerful terminal text editor, serving as a valuable assistant for programmers and system administrators in UNIX/Linux environments. Unlike graphical editors, Vim is keyboard-centric, emphasizing efficiency and precise control.

๐Ÿงฉ 1. What is Vim?

Vim is an enhanced version of the early UNIX editor <span><span>vi</span></span>, focusing on extensibility, customizability, and high efficiency, making it the gold standard for learning Linux editors.

๐Ÿง  2. Core Philosophy of Vim:Mode-based Editing

The biggest difference between Vim and ordinary editors is that it has multiple editing modes.

Mode Name Entry Method Function Description
Normal Mode Vim starts in this mode by default Used for browsing, copying, pasting, deleting, jumping, etc.
Insert Mode Press <span><span>i</span></span> Start entering text
Command Mode Press <span><span>:</span></span> Used for saving, exiting, searching, replacing, etc.
Visual Mode Press <span><span>v</span></span> / <span><span>V</span></span> Select text blocks (character-wise / line-wise)
Replace Mode Press <span><span>R</span></span> Replace existing text

๐Ÿ”ง 3. Basic Usage Flow of Vim (Remember these 3 steps)

  1. Enter Vim

bashvim file # If the file does not exist, a new file will be created.

2. Enter Insert Mode to input content

  • Press <span><span>i</span></span> to enter Insert Mode (insert)

  • Input text

  • Press <span><span>Esc</span></span> to exit Insert Mode and return to Normal Mode

  • Save and Exit

    • Input <span><span>:w</span></span> to save (write)

    • Input <span><span>:q</span></span> to exit (quit)

    • Input <span><span>:wq</span></span> to save and exit

    • Input <span><span>:q!</span></span> to force exit without saving

    ๐Ÿ›  4. Common Vim Command Quick Reference

    ๐Ÿ“Œ In Normal Mode:

    Action Command Description
    Move Cursor <span><span>h, </span></span><code><span><span>j</span></span>, <span><span>k</span></span>, <span><span>l</span></span> Left, down, up, right
    Quick Jump <span><span>gg to the beginning, </span></span><code><span><span>G</span></span> to the end Quickly jump through text
    Delete Character <span><span>x to delete the current character</span></span>
    Delete a Line <span><span>dd</span></span> Delete the entire line
    Copy a Line <span><span>yy</span></span>
    Paste <span><span>p (after current line); P (before current line)</span></span>
    Undo <span><span>u</span></span>
    Redo <span><span>Ctrl + r</span></span>
    Search <span><span>/keyword + Enter, </span></span><code><span><span>n</span></span> to find the next
    Replace <span><span>:s/oldword/newword/</span></span> Replace the first match in the current line
    Replace All Matches in Current Line <span><span>:s/oldword/newword/g</span></span>
    Replace Throughout the File <span><span>:%s/oldword/newword/g</span></span>

    โœ๏ธ 5. How to Enter Insert Mode

    Command Meaning
    <span><span>i</span></span> Insert before the current character
    <span><span>I</span></span> Insert at the beginning of the current line
    <span><span>a</span></span> Insert after the current character
    <span><span>A</span></span> Insert at the end of the current line
    <span><span>o</span></span> Create a new line below the current line and enter Insert Mode
    <span><span>O</span></span> Create a new line above the current line and enter Insert Mode

    ๐Ÿ“ 6. What is the <span><span>.swp</span></span> file in Vim?

    When you open a file, Vim automatically creates a hidden temporary swap file ๐Ÿง  <span><span>.swp</span></span> in the same directory. The purpose of this file is:

    • To serve as a buffer backup;

    • To prevent data loss during sudden power outages/crashes;

    • To provide file conflict detection (when you try to open the same file with two instances of Vim).

    โ—๏ธ Note:

    • When exiting normally (<span><span>:wq</span></span>), the <span><span>.swp</span></span> file will be automatically deleted;

    • If you exit abnormally (e.g., forced shutdown), the <span><span>.swp</span></span> file will remain;

    • When you open the file again, you will be prompted to recover the previous editing state.

    ๐Ÿ’ก How to Clear:

    • After confirming, you can manually delete it:

    bashrm .file.swp

    ๐Ÿงช 7. Example Usage of Vim

    bashvim hello.txt# i                   # Enter Insert Mode, input content# ESC                 # Return to Normal Mode# :wq                 # Save and exit

    ๐Ÿง  8. Learning Suggestions for Beginners

    1. Do not fear the black screen, all operations are at your fingertips;

    2. Memorize basic mode switching:

    • <span><span>i</span></span> to enter input, <span><span>Esc</span></span> to exit, <span><span>:wq</span></span> to save;

  • Practice more: You can use Vim’s built-in tutorial

  • bashvimtutor

    ๐Ÿงญ 9. Advanced Techniques and Plugins (for further learning)

    • Plugin systems (Vundle, Plug)

    • Syntax highlighting, auto-indentation

    • Configuration file <span><span>.vimrc</span></span>

    • Multi-file editing:<span><span>:e</span></span>, <span><span>:sp</span></span>, <span><span>:vs</span></span>

    • Multi-window operations:<span><span>Ctrl+w</span></span> combination keys

    ๐ŸŽ“ Conclusion: Learning Vim is the first step to mastering Linux

    Vim is not just an editor; it is a โ€œhigh-efficiency way of thinking.โ€ At first, it may seem difficult to get used to, but with a few days of persistence, you will find its logic very elegant, and once proficient, you will achieve unprecedented operational efficiency!

    Leave a Comment