Solaris · VI Editor

Solaris · Lesson 7

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 is still important

On almost every Unix and Solaris system, vi is guaranteed to be present. Even if you prefer other editors, knowing vi is essential when you connect to new servers or minimal installations.

This lesson focuses on the 20% of vi commands you use 80% of the time as a system administrator: opening files, moving around, making small edits and saving safely.

Key vi concepts to remember

Modes

vi mainly uses command mode and insert mode. Command mode is for navigation and operations; insert mode is for typing text.

Buffers & files

When you open a file in vi, you edit a buffer in memory. The file is only changed on disk when you write it with :w or :wq.

Search & change

Powerful search (/) and change commands (c, d, y) make it quick to edit configs without a mouse.

Essential vi workflows in practice

Try each of the following flows in your Solaris lab. Don't worry about memorising everything at once—repeat them a few times and they will become natural.

1. Open a file and quit without saving

Use vi filename to open a file. If you made a mistake, you can exit without saving using :q! from command mode.

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. Enter insert mode, type text and save

The most basic workflow: open a file, enter insert mode with i, type, then save and quit with :wq.

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 to write and quit.
 
[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

Learn how to move between lines and search for specific strings using /pattern.

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
 
# After reviewing, you can quit with:
:q

4. Edit a config file safely using a backup

Always make a backup before editing important system files. Then use vi to make a small, controlled change.

terminal — vi
solaris-lab
[root@solaris ~]# cd /etc
[root@solaris /etc]# cp sshd_config sshd_config.bak
 
[root@solaris /etc]# vi sshd_config
 
# (inside vi, command mode)
# Search for PermitRootLogin line:
/PermitRootLogin
 
# Change the line from:
# PermitRootLogin yes
# to:
# PermitRootLogin no
 
# Steps:
# - Place cursor on 'yes'
# - Press: cw
# - Type: no
# - Press: ESC
 
:wq # save and exit
 
[root@solaris /etc]# diff sshd_config.bak sshd_config
< PermitRootLogin yes
> PermitRootLogin no

5. Undo, copy and paste lines

Use u to undo, yy to yank (copy) a line and p to paste it below the cursor.

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

Small vi cheat sheet for administrators

Modes and saving

  • ESC → go to command mode
  • i → insert before cursor (insert mode)
  • a → append after cursor
  • o → open new line below and insert
  • :w → write (save) file
  • :q → quit (fails if there are unsaved changes)
  • :wq → write and quit
  • :q! → quit and discard changes

Navigation, edit and search

  • h / j / k / l → move left / down / up / right
  • gg / G → go to first / last line
  • 0 / $ → go to start / end of line
  • yy / p → yank (copy) current line and paste
  • dd → delete current line
  • u → undo last change
  • /pattern → search forward
  • n / N → next / previous match

Safety tips when editing system files

  • Always take a quick backup of important files before editing, for example: cp sshd_config sshd_config.bak.
  • Make small, focused changes and use diff to confirm exactly what changed.
  • If you are not sure about your edits, use :q! to exit without saving and start again.

Once you are comfortable with vi, editing ZFS, SMF and network configuration files becomes much easier.