Introduction – Why the vi Editor Still Matters
If you’ve ever opened a terminal on a Unix‑like system and typed `vi`, you’ve just stepped into a world that has powered developers, sysadmins, and writers for nearly five decades. Despite the rise of flashy graphical editors, vi (and its modern cousin Vim) remains the go‑to text editor on servers, embedded devices, and remote machines where speed, reliability, and low‑resource usage are non‑negotiable.
But let’s be honest—most newcomers feel a little intimidated by the cryptic keystrokes and the seemingly endless list of commands. The good news? Once you grasp the core concepts, vi becomes an extension of your brain, allowing you to edit files faster than you ever thought possible. In this guide we’ll demystify the vi editor, walk you through the essential commands, and give you actionable tips to become a confident command‑line editor in under an hour.
1. Getting Started: Opening, Navigating, and Exiting vi
1.1 Launching vi
The simplest way to start vi is:
“`bash
vi filename.txt
“`
If the file doesn’t exist, vi creates a new, empty buffer ready for you to type. On many modern systems you’ll actually be using Vim (Vi IMproved), which adds a wealth of enhancements while staying fully compatible with classic vi commands.
1.2 Understanding Modes
vi operates on the principle of modes—a concept that separates text insertion from command execution:
| Mode | How to Enter | What It Does |
|——|————–|————–|
| Normal (or Command) | Press `Esc` from any other mode | Navigate, delete, copy, paste, run commands |
| Insert | Press `i`, `a`, `o`, etc. from Normal | Insert text just like a regular editor |
| Visual | Press `v` (character), `V` (line), or `Ctrl‑v` (block) | Select text for operations |
| Command‑line | Press `:` from Normal | Execute ex commands (save, quit, search) |
> Pro tip: Always start by pressing `Esc` a few times to guarantee you’re in Normal mode before issuing a command.
1.3 Basic Navigation
In Normal mode, you can move the cursor without using arrow keys:
| Key | Action |
|—–|——–|
| `h` | Move left |
| `j` | Move down |
| `k` | Move up |
| `l` | Move right |
| `w` | Jump to the start of the next word |
| `b` | Jump back to the beginning of the previous word |
| `0` (zero) | Go to the beginning of the line |
| `$` | Go to the end of the line |
| `gg` | Jump to the first line of the file |
| `G` | Jump to the last line (or `:10G` for line 10) |
These motions become the building blocks for more sophisticated editing commands.
1.4 Saving and Exiting
| Command | Description |
|———|————-|
| `:w` | Write (save) the current file |
| `:q` | Quit vi (fails if there are unsaved changes) |
| `:wq` or `ZZ` | Save and quit in one step |
| `:q!` | Quit without saving (force quit) |
| `:w filename` | Save to a new file name (useful for backups) |
2. Essential Editing Commands: From Deleting to Copy‑Pasting
Once you can move around, the real power of vi shines through its operator‑motion model. An operator (like delete `d`, yank/copy `y`, or change `c`) works on a motion (like a word `w` or line `$`). Combine them for lightning‑fast edits.
2.1 Deleting Text
| Command | Effect |
|———|——–|
| `x` | Delete the character under the cursor |
| `dw` | Delete from the cursor to the start of the next word |
| `d$` | Delete from the cursor to the end of the line |
| `dd` | Delete the entire current line |
| `3dd` | Delete three consecutive lines |
Deleted text is stored in the unnamed register, so you can paste it later.
2.2 Yanking (Copying) and Pasting
| Command | Effect |
|———|——–|
| `yy` or `Y` | Yank (copy) the current line |
| `yw` | Yank from cursor to the start of the next word |
| `y$` | Yank to the end of the line |
| `p` | Paste after the cursor (or below the current line for linewise yanks) |
| `P` | Paste before the cursor (or above the current line) |
You can also specify a count: `5yy` yanks five lines, and `5p` pastes them five times.
2.3 Changing Text
The `c` operator works like “delete and enter Insert mode”:
| Command | Effect |
|———|——–|
| `cw` | Change the current word (deletes it and drops you into Insert mode) |
| `c$` | Change to the end of the line |
| `cc` | Change the entire line (equivalent to `dd` followed by `i`) |
When you finish typing, hit `Esc` to return to Normal mode.
2.4 Undo, Redo, and Repeating
| Command | Effect |
|———|——–|
| `u` | Undo the last change |
| `Ctrl‑r` | Redo (undo the undo) |
| `.` (dot) | Repeat the last change command (a huge time‑saver) |
3. Searching, Replacing, and Working with Multiple Files
3.1 Searching Within a File
-
- `/pattern` → Search forward for pattern.
- `?pattern` → Search backward.
- `n` → Jump to the next occurrence.
- `N` → Jump to the previous occurrence.
You can use regular expressions for powerful matches, e.g., `/^TODO` finds lines that start with “TODO”.
3.2 Substituting Text
The `:s` command performs find‑and‑replace:
“`vim
:s/old/new/ ” Replace first occurrence on the current line
:s/old/new/g ” Replace all occurrences on the current line
:%s/old/new/gc ” Replace all in the file, ask for confirmation each time
“`
-
- `%` represents the whole file.
- `c` prompts you for confirmation, which is handy for bulk changes.
3.3 Opening Multiple Files
You can edit several files in one vi session:
“`bash
vi file1.txt file2.txt file3.txt
“`
-
- `:n` → Edit the next file in the argument list.
- `:prev` → Go back to the previous file.
- `:args` → Show the full list of files.
- `:argdo %s/foo/bar/g | update` → Run a command on all files (useful for project‑wide refactors).
3.4 Splits and Tabs (Vim Feature)
If you’re using Vim, you gain window management:
-
- `:split filename` or `:vsplit filename` → Open a file in a horizontal/vertical split.
- `Ctrl‑w h/j/k/l` → Move between splits.
- `:tabnew filename` → Open a new tab page.
These features keep you in the terminal while letting you view multiple files side‑by‑side.
4. Advanced Techniques: Macros, Registers, and Customization
4.1 Using Registers
Registers are named clipboards. By default, vi uses the unnamed register (`”`). You can explicitly target others:
-
- `”aY` → Yank a line into register a.
- `”ap` → Paste from register a.
- `”*y` → Yank to the system clipboard (Vim with `+clipboard` support).
Registers `0` and `1` store the most recent yank and delete, respectively, which can be handy for complex edits.
4.2 Recording Macros
Macros let you record a sequence of keystrokes and replay them:
1. Press `qa` to start recording into register a.
2. Perform the actions you want (e.g., `dw` to delete a word, then `p` to paste).
3. Press `q` to stop recording.
4. Replay with `@a`. Use `@@` to repeat the last macro.
Macros are a game‑changer for repetitive edits across many lines.
4.3 Configuring vi/Vim with `.vimrc`
A small `.vimrc` file in your home directory can transform vi into a personalized powerhouse. Here are a few beginner‑friendly settings:
“`vim
” Enable line numbers
set number
” Highlight search results
set hlsearch
” Use incremental search (shows matches as you type)
set incsearch
” Turn on syntax highlighting
syntax on
” Use 4 spaces for tabs (common coding style)
set tabstop=4 shiftwidth=4 expandtab
” Map jj to Escape for faster mode switching (if you use a keyboard often)
inoremap jj
“`
After saving, reload with `:source ~/.vimrc` or restart vi.
4.4 Plugins for Vim Users
If you’re on Vim (or Neovim), consider adding plugins to extend functionality:
-
- nerdtree – File system explorer sidebar.
- fzf.vim – Fuzzy finder for quickly opening files.
- vim-airline – Attractive status line with git branch info.
- coc.nvim – IDE‑like autocompletion and linting.
Plugins are managed with a plugin manager such as vim-plug:
“`vim
call plug#begin(‘~/.vim/plugged’)
Plug ‘preservim/nerdtree’
Plug ‘junegunn/fzf.vim’
call plug#end()
“`
Run `:PlugInstall` to fetch them. Even a modest plugin set can make vi feel like a modern IDE while keeping the low‑resource footprint you love.
5. Tips for a Smooth Transition from GUI Editors
1. Practice the “home row” – Keep your fingers on `h j k l` for navigation; muscle memory will develop quickly.
2. Use cheat sheets – Print a one‑page command cheat sheet and keep it near your workstation until the basics become second nature.
3. Leverage the dot command – After mastering a single edit (e.g., `ci(` to change inside parentheses), the `.` key repeats it on subsequent lines.
4. Start with a minimal `.vimrc` – Too many settings can overwhelm beginners. Add tweaks gradually as you discover pain points.
5. Take advantage of built‑in help – Type `:help` followed by any command (e.g., `:help dw`) to read the comprehensive manual directly in vi.
—
Conclusion – Key Takeaways
- vi is modal: Normal, Insert, Visual, and Command‑line modes keep editing and navigation distinct, enabling rapid actions.
- Master the operator‑motion model (`d`, `c`, `y` + motions like `w`, `$`, `}`) to edit text without leaving the keyboard.
- Search, replace, and multi‑file editing are built into vi, allowing you to refactor code or documents efficiently.
- Registers, macros, and a customized `.vimrc` turn vi from a simple editor into a personalized development environment.
- Practice makes perfect: Spend a few minutes each day navigating and editing in vi, and you’ll soon outpace many GUI editors in speed and precision.
Whether you’re troubleshooting a remote server, writing a quick script, or embarking on a large codebase refactor, vi (or Vim) equips you with a lightweight, ubiquitous tool that works anywhere. Embrace its learning curve, and you’ll find that the command line becomes not a barrier but a catalyst for faster, more focused work.
Happy editing!