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:wqbut 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 characterj: Move down one linek: Move up one linel: Move right by one character0: Move to the start of the line (the number 0)$: Move to the end of the lineCtrl + f: Scroll down one pageCtrl + b: Scroll up one page
Basic Editing Operations (in Command Mode)¶
- Deletion:
x: Delete the current character under the cursor (if deleted by mistake, pressuto undo).dd: Delete the entire current line (after deletion, presspto 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 (presspto 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 (pressnfor next,Nfor 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:
- Create a new file: Enter
vim test.txtin the terminal to enter Command Mode. - Enter Insert Mode: Press
i, and a vertical line|will appear before the cursor. Input text (e.g.,Hello, Ubuntu!). - Move the cursor: Press
jto move down one line, then pressato insert text after the cursor (e.g.,This is a test file.). - Delete errors: Press
Escto return to Command Mode, move the cursor to the error character, and pressxto delete. - Copy and paste: Move the cursor to the first line, press
yyto copy the entire line, then presspto paste below. - Save and exit: Press
Escto return to Command Mode, then enter:wqto save and exit.
IV. Common Issues and Tips for Beginners¶
-
How to undo a mistake?
Pressuto undo the last step; to recover, pressCtrl + r. -
How to quickly jump to a specific line?
First pressEscto enter Command Mode, then pressnumber + G(e.g.,5Gto jump to the 5th line). -
Need to temporarily view a file without modifying it?
Usevim -R filenameto open the file in read-only mode to avoid accidental changes. -
Distinguish between vi and vim?
Vim is an enhanced version of vi, supporting syntax highlighting, code completion, etc. Ubuntu ships with vim by default. Runvim --versionto 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).