vi Text Editor

The classic modal text editor for Unix and Linux systems

Syntax: vi [options] [file...]
Note: vi (Visual Editor) is a modal text editor that operates in different modes. It's available on virtually every Unix-like system and is essential for system administration and programming.

Description

vi is a screen-oriented text editor originally created for the Unix operating system. It's a modal editor, meaning it has different modes of operation: command mode for navigation and commands, insert mode for typing text, and command-line mode for executing commands. Despite its steep learning curve, vi is powerful and efficient once mastered.

Learning Curve: vi has a reputation for being difficult to learn initially, but it becomes very powerful and efficient with practice. Many modern systems use vim (Vi IMproved) as the default vi implementation.

vi Modes

Mode Purpose How to Enter
Command Mode Navigation, deletion, copying Default mode, press Esc
Insert Mode Text insertion and editing i, a, o, etc.
Command-line Mode File operations, search, replace : from command mode

Basic Commands

Starting and exiting vi:
# Open a file
vi filename.txt

# Open file at specific line
vi +25 filename.txt

# Open multiple files
vi file1.txt file2.txt

# Exit commands (from command mode)
:q          # Quit (if no changes)
:q!         # Quit without saving
:w          # Save (write)
:wq         # Save and quit
:x          # Save and quit (same as :wq)
ZZ          # Save and quit (command mode)

Navigation Commands

Command Action
h Move left
j Move down
k Move up
l Move right
w Move to next word
b Move to previous word
0 Move to beginning of line
$ Move to end of line
gg Go to first line
G Go to last line
:n Go to line n

Insert Mode Commands

Command Action
i Insert before cursor
a Insert after cursor
I Insert at beginning of line
A Insert at end of line
o Open new line below
O Open new line above
Esc Return to command mode

Editing Commands

Command Action
x Delete character under cursor
X Delete character before cursor
dd Delete entire line
dw Delete word
d$ Delete to end of line
yy Copy (yank) entire line
yw Copy word
p Paste after cursor
P Paste before cursor
u Undo last change
Ctrl+r Redo

Search and Replace

Search commands:
# Search forward
/pattern

# Search backward
?pattern

# Find next occurrence
n

# Find previous occurrence
N

# Search and replace (command-line mode)
:s/old/new/        # Replace first occurrence on current line
:s/old/new/g       # Replace all occurrences on current line
:%s/old/new/g      # Replace all occurrences in file
:%s/old/new/gc     # Replace all with confirmation

File Operations

File management commands:
# Save file
:w

# Save as different name
:w newname.txt

# Save part of file
:10,20w partial.txt

# Read another file into current buffer
:r filename.txt

# Edit another file
:e filename.txt

# Show current file info
:f

# List open buffers
:ls

Advanced Commands

Advanced editing:
# Repeat last command
.

# Join lines
J

# Change case
~

# Indent line
>>

# Unindent line
<<

# Set line numbers
:set number
:set nu

# Remove line numbers
:set nonumber
:set nonu

Working with Multiple Files

Multiple file operations:
# Open multiple files
vi file1.txt file2.txt file3.txt

# Switch to next file
:n

# Switch to previous file
:N

# Edit specific file from list
:e file2.txt

# Show current file list
:args

# Split window horizontally
:split filename

# Split window vertically
:vsplit filename

Practical Examples

Common editing workflow:
# 1. Open file
vi config.txt

# 2. Navigate to line 10
:10

# 3. Enter insert mode
i

# 4. Type new text
server_port=8080

# 5. Return to command mode
Esc

# 6. Save and exit
:wq
Quick text manipulation:
# Delete 5 lines starting from current line
5dd

# Copy 3 lines
3yy

# Paste copied lines
p

# Replace all tabs with spaces
:%s/\t/    /g

# Delete all empty lines
:g/^$/d

vi Configuration

Runtime configuration:
# Show line numbers
:set number

# Enable syntax highlighting (if available)
:syntax on

# Set tab width
:set tabstop=4

# Enable auto-indent
:set autoindent

# Show matching brackets
:set showmatch

# Case-insensitive search
:set ignorecase

Emergency Recovery

Recovering from common problems:
# If vi seems frozen (usually Ctrl+S was pressed)
Ctrl+Q

# Force quit without saving
:q!

# If you accidentally entered replace mode
Esc

# If you're lost in command-line mode
Esc

# Recover from swap file
vi -r filename.txt

Common Use Cases

  • System administration: Editing configuration files on servers
  • Programming: Writing and editing code files
  • Log analysis: Viewing and searching through log files
  • Remote editing: Editing files over SSH connections
  • Quick edits: Making small changes to text files
  • Scripting: Creating and modifying shell scripts
  • Documentation: Writing and editing documentation
  • Emergency situations: When GUI editors are not available

Tips for Beginners

  • Always remember which mode you're in
  • Use Esc to return to command mode when in doubt
  • Start with basic commands: i (insert), Esc (command mode), :wq (save and quit)
  • Practice navigation with h, j, k, l keys
  • Learn to use :q! to quit without saving when you make mistakes
  • Use vimtutor command for interactive tutorial (if vim is installed)
  • Keep a vi command reference handy until commands become muscle memory
  • Don't try to learn all commands at once - master basics first

vi vs vim

vi vs vim: Most modern Linux systems use vim (Vi IMproved) when you type vi. vim includes many enhancements like:

  • Syntax highlighting
  • Multiple undo levels
  • Split windows
  • Plugin support
  • Better search and replace
  • Visual mode for selecting text
Related Commands: vim, nano, emacs, less, more