Quick take: Vim has modes: press i to insert text, Esc to return to Normal mode, then type :wq to save and quit (or :q! to quit without saving). Movement and editing happen in Normal mode.
Introduction
Vim is the powerful, ubiquitous terminal editor installed on virtually every Unix system — which is why knowing the basics is essential even if you prefer nano. Its defining feature is modes: you switch between typing text (Insert mode) and issuing commands (Normal mode), which feels strange at first but becomes very fast.
This guide gets you productive with the essentials: the modes, saving and quitting, moving around, editing, and search and replace — enough to confidently edit any file.
Syntax
The basic syntax of the vim command is:
vim [OPTIONS] [FILE]Understanding Vim Modes
Vim's modes are the key to using it. Normal mode (the default) is for navigation and commands — keys like dd delete a line. Insert mode (press i) is for typing text like a normal editor. Command-line mode (press :) is for actions like saving (:w) and quitting (:q).
The golden rule: when in doubt, press Esc to return to Normal mode, then type your command. If you ever get stuck, Esc followed by :q! exits without saving.
Common Options and Parameters
The most useful options and parameters for the vim command:
| Option | Description |
|---|---|
| i | Enter Insert mode to type text. |
| Esc | Return to Normal mode. |
| :w | Write (save) the file. |
| :q | Quit (fails if there are unsaved changes). |
| :wq or ZZ | Save and quit. |
| :q! | Quit without saving. |
| dd / yy / p | Delete a line / copy a line / paste. |
| /text | Search forward for text (n for next). |
| :%s/old/new/g | Replace every occurrence in the file. |
| :set number | Show line numbers. |
Practical Examples
Real vim commands you can run today:
# Open a file in Vim
vim config.yml
# Inside Vim: enter insert mode, type, then save and quit
i ... type ... Esc :wq
# Quit without saving
:q!
# Search for a word
/server
# Replace all occurrences in the file
:%s/8080/80/g
# Jump to line 50
:50Essential Editing Commands
Once you are comfortable switching modes, a handful of Normal-mode commands make Vim genuinely fast. They combine an operator with a motion, so the same verbs work everywhere.
| Command | Action |
|---|---|
| dd / yy / p | Delete a line / yank (copy) a line / paste |
| x / u / Ctrl+r | Delete a character / undo / redo |
| w / b / 0 / $ | Next word / previous word / line start / line end |
| gg / G / :n | Top of file / end of file / go to line n |
| cw / dw | Change a word / delete a word |
The magic is composition: d2w deletes two words, 3dd deletes three lines. Learn the verbs and motions separately and they multiply into hundreds of precise edits.
Tips and Best Practices
- Stuck and cannot type? You are probably in Normal mode — press
ito insert. Cannot run a command? PressEscfirst. - The famous “how do I exit Vim?” answer is
Escthen:wqto save and quit, or:q!to discard changes. - Run
vimtutorin the terminal for a built-in 30-minute interactive lesson.
Final Thoughts
Vim rewards a small upfront investment with lifelong speed. Master the modes, :wq and :q!, basic movement, and :%s/old/new/g, and you can edit any file on any server with confidence. Keep vimtutor handy, and reach for nano when you just want a quick, modeless edit.
FAQ: vim Command in Linux
How do I exit Vim?+
Press Esc to enter Normal mode, then type :wq and Enter to save and quit, or :q! and Enter to quit without saving.
How do I save a file in Vim?+
Press Esc, then type :w and Enter to write the file. Use :wq to save and quit in one step, or the shortcut ZZ.
What are the modes in Vim?+
The main modes are Normal (navigation and commands, the default), Insert (typing text, entered with i), and Command-line (actions like :w and :q, entered with :).
How do I search and replace in Vim?+
Use :%s/old/new/g to replace every occurrence in the file. Add a c flag (:%s/old/new/gc) to confirm each change.
How do I show line numbers in Vim?+
Type :set number in Normal mode. Add it to your ~/.vimrc to make it permanent across sessions.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support