Vi is the workhorse of Linux text editing—fast, powerful, and indispensable for developers, admins, and power users. It’s available on every Linux system by default, yet many users avoid it due to its steep learning curve. The good news? With a handful of essential commands, you can edit files efficiently without memorising entire modes. This guide cuts through the complexity by teaching exactly what you need to edit files, navigate text, and save time—no fluff, just practical skills.
What You'll Need
- Any Linux system (Ubuntu, Fedora, Arch, etc.) with Vi installed by default
- A text or configuration file to edit (e.g., `~/.bashrc`, `config.txt`)
- Basic familiarity with terminal commands (e.g., `cd`, `cat`, `ls`)
- A text editor mode:
vi(interactive) orvim(with enhancements) — both are installed by default
Step-by-Step: Your Vi Workflow
-
Open Vi/Vim: Launch the editor by typing:
vi filenameorvim filename(replacefilenamewith a real file, e.g.,~/.bashrc). If the file doesn’t exist, Vi will prompt:File name:type a new name and press Enter>. Vi will create it. Key for success: Always specify a file. Without one, Vi enters "command mode" and lacks context. -
Mode switching: Vi has three modes, each with distinct commands:
- Command mode (default): Press Esc> to enter. Use this to navigate, delete, or save files.
- Insert mode: Press i> to start editing text. Press Esc> to return to Command mode.
- Visual mode: Press v> to select text. Type
pto paste ordto delete.
-
Navigation basics: Move around text with these commands:
- Move up/down:
k(up),j(down) - Move left/right:
h,l - Page up/down:
gg(top),G(bottom) - Jump to line X:
100Gskips to line 100.
Ctrl+f(Ctrl+b) for faster scrolling. - Move up/down:
-
Editing text: Insert text at the cursor:
i(before cursor),a(after cursor),o(adds a newline below),O(above). Delete text with:dw(delete word),dd(delete line),D(delete to end of line). Example: Delete the line after line 5:5dw. -
Save and exit: Save the file without changes:
ZQ(Ubuntu’s shortcut for Vi’s Q key). Save and exit::wq(write + quit). Force save if locked::w !sudo tee % > file. Warning:q!only exits without saving. Do:wqfirst! -
Search/replace: Find text with:
/search_term(press Enter> to execute). Replace text (e.g., all “old” to “new”)::%s/old/new/g(case-sensitive; addifor case-insensitive). -
Undo/redo: Vi has no built-in undo. Use:
u(undo),Ctrl+r(redo). Workaround: Save changes withZQafter undoing.
Common Mistakes and How to Fix Them
-
Vi won’t save: Forgetting
Esc> before saving causes issues. Always typeEsc> first to enter Command mode. -
File locked by another user: Use sudo with
:w !sudo tee % > file. Warn others before running this. -
Mistakenly deleted a line: Recover with
u. If too late, check~/.vi_history(Linux) or~/.vim_history(Vim). -
Mode confusion: Always press Esc> to return to Command mode. Check the cursor
?to confirm mode (e.g.,i> = Insert mode).
Tips to Speed Up Vi Editing
-
Learn shortcuts: Memorize these hotkeys:
yy= Yank line (copy)p= Paste after cursory$= Yank to end of linefc= Replace character under cursor
-
Use Vim plugins: If Vi feels limiting, try
vim(notvi) for plugins likefzf.vim(fuzzy finding). -
Customize keys: Edit
~/.vimrcto swap keys (e.g., bindztodd). -
Emmet/Vim: Use
Ctrl+k+Ctrl+jto expand Abbreviations (e.g.,ul.li→<ul><li></li></ul>).
Troubleshooting Vi Issues
| Problem | Solution |
|---|---|
Vi crashes on launch: A permissions issue prevents access. Run chmod +x /usr/bin/vi or sudo chown your user to the Vi binary. |
sudo chown $USER /usr/bin/vi (Ubuntu). Restart the terminal. |
No file opens: Vi starts in "ex mode" (command-line mode). Press Esc> first, then i to edit. |
Type !vi filename to force Vi mode. |
Keybindings don’t work: The terminal or font encoding conflicts. Try set term=xterm in Vi or switch to a monospace font. |
set t_Co=256 in ~/.vimrc for better color support. |
Frequently Asked Questions
How do I switch from Vi to a GUI editor like Nano?
Install Nano with sudo apt install nano (Debian/Ubuntu) and replace vi with nano in your workflow. Nano is beginner-friendly for simple edits.
Can I use Vi on macOS or Windows?
macOS: Vi is preinstalled; launch it via vi in Terminal. Windows: Use gvim (from Git for Windows) or install Vim via choco install vim.
What’s the difference between Vi and Vim?
Vim adds features like syntax highlighting, plugins, and better undo stacks. Vi is the simpler, feature-lite version. Use Vim for advanced workflows; Vi for minimalism.
How do I edit a file in Vi but save to another location?
Use :w /path/to/newfile to write to a new location. Example: :w /backup/config.txt.
Why does my Vi file have a hidden “!!” at the bottom?
This is a command to execute the last command again. Press Ctrl+r to undo; Ctrl+o to redo (if supported). To remove it, open the file in another editor and delete manually.
Is it possible to recover a deleted Vi file?
Only if you have a backup or recent history. Check ~/.vi_history or ~/.vim_history. For critical files, use tar/rsync to back up regularly.