Comprehensive Vi Editor Tutorial
Vi is a powerful, lightweight, and ubiquitous text editor in Linux and Unix systems. This tutorial covers the essentials of using Vi, from basic navigation to advanced editing techniques, suitable for beginners and intermediate users.
Table of Contents
Introduction to Vi
Vi (and its enhanced version, Vim, short for “Vi IMproved”) is a highly configurable, modal text editor available on almost all Linux and Unix systems. Its efficiency comes from keyboard-driven commands, making it a favorite for developers and system administrators.
Key features: - Lightweight and fast - Modal editing (command, insert, visual modes) - Extensive plugin support (in Vim) - Available by default on most Linux distributions
This tutorial focuses on Vi commands, which are largely compatible with Vim.
Starting Vi
To start Vi, open a terminal and use the following commands:
- Open a file:
vi filename
- Open a new file:
vi newfile.txt
- Open in read-only mode:
view filename
- Start Vim (if installed):
vim filename
If the file exists, Vi opens it; otherwise, it creates a new file.
Modes in Vi
Vi operates in three main modes:
- Command Mode: Default mode for navigation and issuing commands. Most keys trigger actions (e.g.,
dd
deletes a line). - Insert Mode: For typing and editing text. Enter this mode with keys like
i
ora
. - Visual Mode: For selecting text blocks. Enter with
v
(character-wise),V
(line-wise), orCtrl+v
(block-wise).
Switch between modes: - Command to Insert: i
(insert before cursor), a
(append after cursor), o
(new line below) - Insert to Command: Esc
- Command to Visual: v
, V
, or Ctrl+v
- Visual to Command: Esc
Editing Text
Enter Insert Mode to edit text: - i
: Insert before cursor - a
: Append after cursor - o
: Open a new line below - O
: Open a new line above - I
: Insert at the start of the line - A
: Append at the end of the line
In Command Mode, edit with: - x
: Delete character under cursor - dw
: Delete word - dd
: Delete current line - D
: Delete from cursor to end of line - r
: Replace single character - R
: Replace multiple characters (overwrite mode) - cw
: Change word (delete word and enter Insert Mode)
Use numbers for repetition (e.g., 3dd
deletes three lines).
Saving and Exiting
In Command Mode, use these commands (start with :
): - :w
: Save (write) the file - :w filename
: Save as a new file - :q
: Quit (if no changes made) - :q!
: Quit without saving - :wq
or ZZ
: Save and quit - :x
: Save and quit (only writes if changes were made)
Note: If you lack permissions, use :w!
to force save (if possible).
Search and Replace
Searching
/pattern
: Search forward forpattern
?pattern
: Search backward forpattern
n
: Move to next matchN
: Move to previous match
Example: /hello
searches for “hello”. Use n
to jump to the next occurrence.
Replacing
:%s/old/new/g
: Replace all occurrences ofold
withnew
in the file:%s/old/new/gc
: Replace with confirmation for each occurrence:s/old/new
: Replace in the current line only
Example: :%s/foo/bar/g
replaces all “foo” with “bar”.
Copy, Cut, and Paste
Vi uses “yank” for copying and “delete” for cutting: - yy
: Yank (copy) current line - yw
: Yank word - p
: Paste after cursor - P
: Paste before cursor - dd
: Cut (delete) current line
Use numbers (e.g., 3yy
copies three lines). In Visual Mode: - Select text with v
, V
, or Ctrl+v
- y
: Copy selection - d
: Cut selection - p
: Paste after cursor
Undo and Redo
u
: Undo last changeCtrl+r
: Redo undone change:u
: Undo (alternative)
Note: Vi has a limited undo history; Vim supports more extensive undo.
Advanced Features
Multiple Files
- Open multiple files:
vi file1 file2
- Switch files:
:n
(next),:p
(previous) - List open files:
:ls
- Open another file:
:e filename
Splitting Windows (Vim-specific)
:sp
: Split horizontally:vsp
: Split verticallyCtrl+w
followed by arrow keys: Switch between windows
Macros
qa
: Start recording macro to registera
- Perform actions
q
: Stop recording@a
: Replay macro@@
: Replay last macro
Command Repetition
.
: Repeat the last change- Example: If you type
dw
to delete a word,.
repeats it.
Customizing Vi
Vi and Vim can be customized via a configuration file: - For Vi: ~/.exrc
- For Vim: ~/.vimrc
Example .vimrc
:
set number " Show line numbers
set tabstop=4 " Tabs are 4 spaces
set autoindent " Enable auto-indentation
syntax on " Enable syntax highlighting (Vim)
To apply settings in a session: - :set number
: Enable line numbers - :set nonumber
: Disable line numbers
Tips and Tricks
- Jump to matching brace: Use
%
on{}
,()
, or[]
- Case-insensitive search:
:set ignorecase
- Highlight search results:
:set hlsearch
- Clear highlights:
:noh
- Execute shell command:
:!command
(e.g.,:!ls
) - Auto-complete (Vim):
Ctrl+n
orCtrl+p
in Insert Mode - Visual Block Mode:
Ctrl+v
for column editing (e.g., insert text across multiple lines)
Common Issues and Solutions
- Stuck in Insert Mode:
- Press
Esc
to return to Command Mode.
- Press
- Permission Denied on Save:
- Use
:w!
to force save (if you have sudo privileges, try:w !sudo tee %
).
- Use
- Accidental Macro Recording:
- Stop with
q
and avoid using@
until you clear the register.
- Stop with
- Lost Changes:
- Check for swap files (e.g.,
.filename.swp
). Recover withvi -r filename
.
- Check for swap files (e.g.,
- Slow Performance:
- Disable syntax highlighting (
:syntax off
) or use Vi instead of Vim for large files.
- Disable syntax highlighting (
Conclusion
Vi is a versatile editor that rewards practice. Start with basic navigation and editing, then explore advanced features like macros and splits as needed. For more functionality, consider using Vim, which extends Vi with additional features.
For further learning: - Run vimtutor
in the terminal for an interactive Vim tutorial. - Check the Vi/Vim man page: man vi
or man vim
. - Explore online resources like the Vim documentation or community forums.