Skip to content

Vim Basic Usage

Understanding Vim's modal nature is the key to unlocking its power. Unlike most editors where you can always type text, Vim has different modes for different tasks. This article introduces the most fundamental modes and the basic commands for navigating and editing files.

The Vim Way: Understanding Modes

Vim's efficiency comes from its modal design. You switch between modes to perform different operations. For a beginner, the most important modes to know are:

  • Normal Mode: The default mode when you open Vim. You don't insert text here. Instead, you use key presses as commands to navigate, delete, copy, and paste. This is where you'll spend most of your time.
  • Insert Mode: This is for typing text, like in a traditional editor.
  • Command-Line Mode: This is for running commands, such as saving (:w), quitting (:q), or searching. You enter it by pressing : from Normal mode.

Think of it this way: Normal mode is for commanding the text, and Insert mode is for writing the text.

Switching Between Modes

  • From Normal to Insert mode:
    • i: insert text before the cursor.
    • a: append text 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.
  • From Insert to Normal mode:
    • Press <Esc> (the Escape key). This is the most important key in Vim.

Basic Navigation (Normal Mode)

Instead of the mouse or arrow keys, Vim uses home row keys for navigation, which keeps your hands on the keyboard.

  • h: Move left
  • j: Move down
  • k: Move up
  • l: Move right

Basic Editing (Normal Mode)

All editing commands are performed from Normal mode.

  • Deleting Text:
    • x: Delete the character under the cursor.
    • dw: delete word. Deletes from the cursor to the beginning of the next word.
    • dd: Delete the entire current line.
  • Copying and Pasting (Yank and Put):
    • yw: yank word. Copies the word.
    • yy: Yank the entire line.
    • p: put (paste) the copied text after the cursor.
  • Changing Text:
    • r: replace the single character under the cursor.
    • cw: change word. Deletes the word and puts you in Insert mode.
    • cc: Change the entire line. Deletes the line and enters Insert mode.
  • Undo and Redo:
    • u: Undo the last change.
    • Ctrl-r: Redo the last change.

Creating, Saving, and Quitting (Command-Line Mode)

To run these commands, you must be in Normal mode and press : first.

  • Creating and Opening Files:
    • From your terminal: vim filename.txt opens an existing file or creates a new one.
    • Inside Vim: :e newfile.txt (edit) opens a new file in the current window.
    • :enew creates a new, unnamed file in a new buffer.
  • Saving Files:
    • :w: write (save) the current file.
    • :w new_filename.txt: Save the current content to a new file.
  • Quitting:
    • :q: quit. Fails if there are unsaved changes.
    • :q!: Quit without saving (force quit).
    • :wq: Write (save) and quit. A handy shortcut is ZZ (from Normal mode, no colon).
  • Saving and Quitting All Files:
    • :qa: Quit all open files.
    • :wqa: Write (save) and quit all open files.
最近更新