Linux · VI Editor

Linux · Lesson

VI Editor

Master vi editor for configuration editing. Search, replace and navigation tips. Editing system files safely. Must have skill for Unix admins.

Why vi editor is still important

On almost every Unix, Linux and Solaris system, the vi editor is guaranteed to be available.

Even if administrators prefer other editors, vi remains essential because it is commonly used on remote servers, rescue systems and minimal installations.

This lesson focuses on the most commonly used vi operations for system administrators including navigation, editing, searching and safe configuration file management.

Core vi editor concepts

Modes

vi uses command mode for operations and insert mode for typing text.

Buffers and files

Changes are stored in memory until you save them using :w or :wq.

Search and change

Quickly search and modify configuration files without using a mouse.

Essential vi commands

Modes and saving

  • ESC → return to command mode
  • i → insert before cursor
  • a → append after cursor
  • o → open new line below
  • :w → save file
  • :wq → save and quit
  • :q! → quit without saving

Navigation and editing

  • h j k l → move cursor
  • gg / G → first or last line
  • 0 / $ → start or end of line
  • yy / p → copy and paste
  • dd → delete line
  • u → undo changes
  • /pattern → search text

Safe editing practices for administrators

Backup important files

Always create backup copies before editing critical system configuration files.

Verify modifications

Use tools like diff to confirm exactly what changed after editing.

Practical vi workflows

The following examples demonstrate common vi editor workflows used by Linux and Solaris administrators.

1. Open a file and quit without saving

Use :q! to exit without saving changes when you make a mistake.

terminal — vi
solaris-lab
[root@solaris ~]# vi notes.txt
 
# (inside vi)
# Press ESC to ensure you are in command mode, then type:
 
:q!
 
# and press ENTER
 
[root@solaris ~]#
# Back to shell, file unchanged.

2. Insert text and save the file

Enter insert mode, type text, then save and quit safely.

terminal — vi
solaris-lab
[root@solaris ~]# vi motd
 
# (inside vi, command mode)
# Press:
i
 
# You are now in INSERT mode, type your text:
 
Welcome to the Solaris training lab.
Please do not change system settings without permission.
 
# Press ESC to return to command mode
# Then type:
 
:wq
 
# and press ENTER
 
[root@solaris ~]# cat motd
Welcome to the Solaris training lab.
Please do not change system settings without permission.

3. Move around and search inside a file

Use navigation shortcuts and pattern searching to edit configuration files quickly.

terminal — vi
solaris-lab
[root@solaris ~]# vi /etc/ssh/sshd_config
 
# (inside vi, command mode)
 
G # jump to last line
gg # jump to first line
10G # jump directly to line 10
 
/PermitRootLogin
# search forward for the text 'PermitRootLogin'
 
n # go to next match
N # go to previous match
 
:q

4. Edit a configuration file safely

Always create a backup before modifying important system configuration files.

terminal — vi
solaris-lab
[root@solaris ~]# cd /etc
[root@solaris /etc]# cp sshd_config sshd_config.bak
 
[root@solaris /etc]# vi sshd_config
 
# Search for:
# PermitRootLogin
 
/PermitRootLogin
 
# Change:
# PermitRootLogin yes
 
# To:
# PermitRootLogin no
 
# Place cursor on 'yes'
# Press:
cw
 
# Type:
no
 
# Press ESC
 
:wq
 
[root@solaris /etc]# diff sshd_config.bak sshd_config
< PermitRootLogin yes
> PermitRootLogin no

5. Undo, copy and paste lines

Use undo, yank and paste commands while editing files.

terminal — vi
solaris-lab
[root@solaris ~]# vi todo.txt
 
# (inside vi, command mode)
 
dd # delete current line
 
u # undo last change
 
yy # yank (copy) current line
 
p # paste below current line
 
3yy # yank 3 lines
 
p # paste copied lines below
 
:wq

Small vi cheat sheet

File operations

  • :w → save file
  • :q → quit
  • :wq → save and quit
  • :q! → quit without saving

Editing shortcuts

  • cw → change current word
  • yy → copy line
  • p → paste line
  • dd → delete line
  • u → undo change

Best practices while using vi

Configuration management

  • Always edit carefully as root.
  • Take backups before modifying configs.
  • Make small and controlled changes.
  • Verify services after configuration edits.

Learning approach

  • Practice common commands repeatedly.
  • Learn navigation before advanced features.
  • Use search shortcuts for large config files.
  • Exit without saving if unsure about changes.

Practice tasks for your Linux or Solaris lab

  • Open a file using vi filename.
  • Practice switching between command mode and insert mode.
  • Save files using :wq and exit without saving using :q!.
  • Search for patterns using /pattern.
  • Practice deleting, copying and pasting lines.
  • Create a backup before editing a configuration file.
  • Use diff to compare original and modified files.

Once you become comfortable with vi, editing ZFS, SMF, SSH and network configuration files becomes much easier for system administration tasks.