Introduction to Terminal Editors: Basic Operations of Ubuntu vi/vim

In the Ubuntu system, the terminal is an important tool for efficient work, and vi/vim, as a classic text editor under the terminal, is almost a necessary skill for every Linux user. Although its interface is simple and even a bit “retro”, it is powerful and does not rely on a graphical interface, making it highly suitable for text editing in command-line environments. This article will start from the most basic operations to help you quickly get started with vi/vim.

I. Opening and Exiting vi/vim

Opening a file with vi/vim is straightforward; simply enter the following command in the terminal:

vim filename

If the file does not exist, a new file will be automatically created; if the file already exists, it will be opened directly.

Exiting the Editor

After opening a file, press the Esc key to ensure you are in Command Mode (the default mode), then use the following commands to exit:

  • :q: Exit the editor (only valid if the file has not been modified).
  • :q!: Force exit without saving changes (use with caution!).
  • :wq: Save and exit (most commonly used).
  • :w filename: Save the file with a specified name (without exiting the editor).
  • :x: Save and exit (same functionality as :wq but safer).

II. Three Core Modes: Command Mode, Insert Mode, and Bottom-Line Mode

The core of vi/vim is mode switching, where different modes handle distinct functions. Beginners often get confused by mode switching, so here are the three most commonly used modes:

1. Command Mode (Default Mode)

You enter Command Mode automatically after opening a file. Here, you can move the cursor, delete content, copy/paste, etc., but you cannot directly input text.

Cursor Movement

  • h: Move left by one character
  • j: Move down one line
  • k: Move up one line
  • l: Move right by one character
  • 0: Move to the start of the line (the number 0)
  • $: Move to the end of the line
  • Ctrl + f: Scroll down one page
  • Ctrl + b: Scroll up one page

Basic Editing Operations (in Command Mode)

  • Deletion:
  • x: Delete the current character under the cursor (if deleted by mistake, press u to undo).
  • dd: Delete the entire current line (after deletion, press p to paste below the cursor).
  • dw: Delete the word after the cursor (ensure the cursor is at the start of the word).

  • Copy/Paste:

  • yy: Copy the entire current line (press p to paste after the cursor).
  • 3yy: Copy the next 3 lines (the number indicates the count).

  • Undo and Redo:

  • u: Undo the last operation.
  • Ctrl + r: Redo (recover after undo).

2. Insert Mode (Inputting Text)

In Command Mode, press the following keys to enter Insert Mode, where the cursor will change to a vertical line | and you can directly input text:

  • i: Insert text before the cursor (most commonly used).
  • a: Insert text after the cursor.
  • o: Create a new line below the current line and insert text.
  • O: Create a new line above the current line and insert text.

Exit Insert Mode: Press Esc to return to Command Mode.

3. Bottom-Line Mode (Executing Commands)

In Command Mode, press the : key to enter Bottom-Line Mode, where the cursor will appear at the bottom of the screen, and you can execute commands for saving, exiting, searching, etc.

Common Bottom-Line Commands

  • :w: Save current changes (without exiting).
  • :wq: Save and exit (most commonly used).
  • :q!: Force exit (discard changes).
  • :/keyword: Search for a keyword (press n for next, N for previous).
  • :set nu: Show line numbers (helpful for positioning).
  • :set nonu: Hide line numbers.

III. Quick Practice: From Creating a New File to Saving

Let’s practice basic operations with a small example:

  1. Create a new file: Enter vim test.txt in the terminal to enter Command Mode.
  2. Enter Insert Mode: Press i, and a vertical line | will appear before the cursor. Input text (e.g., Hello, Ubuntu!).
  3. Move the cursor: Press j to move down one line, then press a to insert text after the cursor (e.g., This is a test file.).
  4. Delete errors: Press Esc to return to Command Mode, move the cursor to the error character, and press x to delete.
  5. Copy and paste: Move the cursor to the first line, press yy to copy the entire line, then press p to paste below.
  6. Save and exit: Press Esc to return to Command Mode, then enter :wq to save and exit.

IV. Common Issues and Tips for Beginners

  1. How to undo a mistake?
    Press u to undo the last step; to recover, press Ctrl + r.

  2. How to quickly jump to a specific line?
    First press Esc to enter Command Mode, then press number + G (e.g., 5G to jump to the 5th line).

  3. Need to temporarily view a file without modifying it?
    Use vim -R filename to open the file in read-only mode to avoid accidental changes.

  4. Distinguish between vi and vim?
    Vim is an enhanced version of vi, supporting syntax highlighting, code completion, etc. Ubuntu ships with vim by default. Run vim --version to check the version.

V. Summary

The core of vi/vim is mode switching: Command Mode for moving and editing, Insert Mode for inputting text, and Bottom-Line Mode for executing commands like saving or searching. Beginners don’t need to memorize all commands; first master the frequently used operations: i (insert), dd (delete line), p (paste), and wq (save and exit). Then gradually expand your knowledge.

Practice more in the terminal, such as creating files of different types (text, code), and try using search and replace functions. You’ll soon get used to the efficient operations of vi/vim!

Tip: If vi/vim seems too complex, start with a lightweight editor like nano first (but for long-term terminal use, vim remains a necessary skill).

Xiaoye