vi
is a classic text editor in Unix-based systems, while vim
is an enhanced version of vi
, offering additional functionality like syntax highlighting, multiple undo levels, and plugins. Both editors operate in modes, which can be confusing for beginners but offer great flexibility once understood.
When you open a file in vi
or vim
, you are in Normal Mode. In this mode, you can navigate, delete, and manipulate text without actually editing the content.
-
Navigation:
h
: Move the cursor leftj
: Move the cursor downk
: Move the cursor upl
: Move the cursor rightw
: Jump to the start of the next wordb
: Jump back to the start of the previous word0
: Move to the beginning of the current line$
: Move to the end of the current linegg
: Go to the beginning of the fileG
: Go to the end of the file
-
Editing and Deletion:
x
: Delete the character under the cursordw
: Delete the word starting from the cursordd
: Delete the entire lined$
: Delete from the cursor to the end of the lineu
: Undo the last changeCtrl + r
: Redo the last undone actionp
: Paste the copied or deleted text after the cursoryy
: Yank (copy) the entire liney
: Yank selected text or portion
-
Search:
/pattern
: Search forpattern
in the filen
: Move to the next occurrence of the search resultN
: Move to the previous occurrence of the search result
Normal Mode is the mode for quick edits and navigation. If you’re in any other mode, press Esc
to return to Normal Mode.
This mode allows you to insert or modify text. It’s similar to the text editing modes in modern editors like VS Code or Sublime.
i
: Insert before the cursora
: Insert after the cursorI
: Insert at the beginning of the current lineA
: Insert at the end of the current lineo
: Open a new line below and enter Insert ModeO
: Open a new line above and enter Insert Mode
- Press
Esc
to return to Normal Mode.
Command Mode is used to perform tasks like saving the file, quitting vim
, or performing complex search-and-replace operations. You enter Command Mode by typing :
from Normal Mode.
-
Saving and Quitting:
:w
: Save the file:q
: Quitvim
:wq
or:x
: Save and quit:q!
: Quit without saving
-
File Operations:
:e filename
: Open another file:r filename
: Read another file into the current file
-
Search and Replace:
:%s/old/new/g
: Replace all occurrences ofold
withnew
in the file:%s/old/new/gc
: Replace all occurrences but ask for confirmation before each change
-
Line Numbers:
:set number
: Show line numbers:set nonumber
: Hide line numbers
You can execute many more complex commands using Command Mode. To return to Normal Mode from Command Mode, press Esc
.
In Visual Mode, you can select text for copying, cutting, or other actions.
v
: Character-wise selectionV
: Line-wise selectionCtrl + v
: Block selection (visual block mode)
y
: Yank (copy) selected textd
: Delete the selected text>
: Indent the selected text<
: Un-indent the selected textp
: Paste the copied text after the selection
To exit Visual Mode, press Esc
.
Unlike vi
, which only supports one undo, vim
allows multiple undo actions:
u
: Undo the last changeCtrl + r
: Redo an undone action
You can split your screen to work on multiple files or parts of a file simultaneously:
:split filename
: Split the screen horizontally:vsplit filename
: Split the screen verticallyCtrl + w, w
: Switch between windows
You can record and replay macros in vim
:
qa
: Start recording into registera
- Perform actions.
q
: Stop recording@a
: Play back the macro in registera
To make vim
behave the way you like, you can customize it through a configuration file, .vimrc
. For example:
set number # Show line numbers
set tabstop=4 # Set tab width to 4 spaces
set expandtab # Use spaces instead of tabs
set mouse=a # Enable mouse usage in all modes
syntax on # Enable syntax highlighting
Learning vim
can feel overwhelming at first because of its multiple modes and extensive command set. However, once you master the basic navigation and editing commands in Normal Mode, and you get comfortable switching to Insert, Command, and Visual Modes, your productivity in vim
will increase significantly. Keep practicing these commands and consider customizing vim
with .vimrc
settings to suit your workflow.