vi
text editorsLinux/Unix
The vi command is one of the most frequently used commands in Linux/Unix-like operating systems. vi Screen-oriented (visual) text editor
Quick Reference
Command Name:
vi
Category:
text editors
Platform:
Linux/Unix
Basic Usage:
vi [options] [arguments]
Common Use Cases
Syntax
vi [options] [file...]
Options
Option | Description |
---|---|
-b |
Edit in binary mode |
-c cmd |
Execute command cmd after loading the first file |
-d |
Start in diff mode (vimdiff) |
-e |
Start in Ex mode (like "ex") |
-h |
Display help message and exit |
-i VIMINFO |
Use VIMINFO file instead of .viminfo |
-L |
Same as -r (recover mode) |
-n |
No swap file, changes only in memory |
-o[N] |
Open N windows horizontally (default: one for each file) |
-O[N] |
Open N windows vertically (default: one for each file) |
-p[N] |
Open N tab pages (default: one for each file) |
-r |
List swap files or recover a crashed session |
-R |
Read-only mode (like "view") |
-s SCRIPTFILE |
Source SCRIPTFILE after loading the first file |
-t TAG |
Edit the file containing TAG |
-v |
Start in Vi mode (default) |
-x |
Edit encrypted files (will prompt for key) |
+[num] |
Start at line number [num] |
+/pattern |
Start at the first occurrence of pattern |
+cmd |
Execute command cmd after loading the first file |
-- |
End of options, only file names follow |
Common vi Commands
Vi operates in multiple modes. The main ones are:
- Normal mode: For navigation and simple editing (default)
- Insert mode: For inserting text
- Command-line mode: For executing longer commands
- Visual mode: For selecting text
Basic Navigation (Normal Mode)
Command | Description |
---|---|
h | Move cursor left |
j | Move cursor down |
k | Move cursor up |
l | Move cursor right |
w | Move to next word |
b | Move to previous word |
0 | Move to beginning of line |
$ | Move to end of line |
gg | Move to first line of file |
G | Move to last line of file |
{number}G | Move to line {number} |
Ctrl+f | Page down |
Ctrl+b | Page up |
Editing (Insert Mode)
Command | Description |
---|---|
i | Insert before cursor |
I | Insert at beginning of line |
a | Append after cursor |
A | Append at end of line |
o | Open new line below |
O | Open new line above |
Esc | Exit insert mode |
Deleting and Changing Text
Command | Description |
---|---|
x | Delete character under cursor |
X | Delete character before cursor |
dd | Delete current line |
{number}dd | Delete {number} lines |
dw | Delete word |
D | Delete from cursor to end of line |
cw | Change word (delete word and enter insert mode) |
cc | Change line (delete line and enter insert mode) |
C | Change from cursor to end of line |
r | Replace single character |
R | Enter replace mode |
Copy and Paste
Command | Description |
---|---|
yy | Yank (copy) current line |
{number}yy | Yank {number} lines |
yw | Yank word |
p | Paste after cursor |
P | Paste before cursor |
Search and Replace
Command | Description |
---|---|
/pattern | Search forward for pattern |
?pattern | Search backward for pattern |
n | Repeat search in same direction |
N | Repeat search in opposite direction |
:%s/old/new/g | Replace all occurrences of "old" with "new" in file |
:s/old/new/g | Replace all occurrences of "old" with "new" in current line |
:%s/old/new/gc | Replace all occurrences with confirmation |
File Operations
Command | Description |
---|---|
:w | Write (save) file |
:w filename | Save as filename |
:q | Quit |
:q! | Quit without saving |
:wq | Write and quit |
ZZ | Write and quit (shortcut) |
:e filename | Edit another file |
:e! | Reload current file (discard changes) |
Visual Mode
Command | Description |
---|---|
v | Enter visual mode (character-wise) |
V | Enter visual mode (line-wise) |
Ctrl+v | Enter visual block mode |
Miscellaneous
Command | Description |
---|---|
u | Undo |
Ctrl+r | Redo |
. | Repeat last command |
:set number | Show line numbers |
:syntax on | Enable syntax highlighting |
Ctrl+g | Show file status |
Examples
How to Use These Examples
The examples below show common ways to use the vi
command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.
# Basic Examples Basic
# Open a file for editing
vi file.txt