vi Editor Commands Reference for Oracle DBAs

vi Editor Commands Reference for Oracle DBAs

Every Oracle DBA on Unix or Linux ends up in vi whether they planned to or not. It opens by default when you crontab -e, it is what visudo drops you into, and it is the editor on most Oracle Linux and AIX hosts before anyone has installed nano or configured EDITOR. Knowing the core commands is not optional — it is the difference between a clean parameter change and a corrupted init.ora that brings down the instance.

Purpose

This reference covers the vi commands that come up most often in Oracle DBA work: entering and exiting insert mode, navigating large configuration files, searching for a parameter, making a targeted substitution, and getting out safely without mangling the file. The source is the shutdownabort.com DBA utilities corpus (2007–2013), rescued from the Wayback Machine, which included a condensed vi reference aimed specifically at DBAs doing Oracle work on Unix.

Commands

Modes

vi is a modal editor. Everything breaks if you are in the wrong mode.

CommandAction
iInsert before cursor
aInsert after cursor
oOpen new line below, enter insert mode
OOpen new line above, enter insert mode
IInsert at beginning of line
AInsert at end of line
EscReturn to normal mode from any insert/command state
CommandAction
h / j / k / lLeft / down / up / right (arrow keys also work)
ggJump to first line
GJump to last line
:nJump to line number n (e.g. :142)
0Jump to start of current line
$Jump to end of current line
wForward one word
bBack one word
Ctrl+fPage down
Ctrl+bPage up

Editing

CommandAction
xDelete character under cursor
ddDelete (cut) entire line
dwDelete word forward from cursor
DDelete from cursor to end of line
yyYank (copy) current line
pPaste after cursor / below current line
PPaste before cursor / above current line
uUndo last change
Ctrl+rRedo
.Repeat last edit command

Save and Quit

CommandAction
:wWrite (save) without quitting
:w filenameWrite to a different filename
:qQuit (only works if no unsaved changes)
:wqWrite and quit
ZZWrite and quit (shorthand for :wq)
:q!Quit and discard all changes — use when you want to abandon edits
:e!Reload file from disk, discarding unsaved changes

Search and Replace

CommandAction
/patternSearch forward for pattern
?patternSearch backward for pattern
nJump to next match
NJump to previous match
:s/old/new/Replace first occurrence on current line
:s/old/new/gReplace all occurrences on current line
:%s/old/new/gReplace all occurrences in entire file
:%s/old/new/gcReplace all with confirmation prompt

Code Breakdown

Navigation to a known line number is the fastest way to jump to a specific init.ora parameter when you know the line from a previous grep:

1grep -n "db_cache_size" $ORACLE_HOME/dbs/init$ORACLE_SID.ora
2# returns: 47:db_cache_size = 2G
3vi +47 $ORACLE_HOME/dbs/init$ORACLE_SID.ora

The +n flag opens vi at line n. Once inside, :n jumps to any line, and gg / G navigate to the top and bottom.

Global search-and-replace with :%s/old/new/gc is the safest form for changing a hostname across tnsnames.ora — the c flag prompts before each substitution so you confirm each change rather than blindly replacing every match.

:w before :q when you are unsure of your changes. Write first, then examine the result from another terminal (tail -20 alert_$ORACLE_SID.log), then quit. If something looks wrong, :e! reloads the saved version from disk.

Key Points

  • :q! is your escape hatch. If you opened a file by accident, pressed random keys, and have no idea what state it is in — press Esc several times, then :q!. Nothing is saved.
  • u undoes one change at a time. Hold it down or press it repeatedly to step back through edit history. Ctrl+r moves forward again.
  • / search is case-sensitive by default. Add \c to the pattern for case-insensitive: /\csga_target matches SGA_TARGET, sga_target, and Sga_Target.
  • :%s with no g flag only replaces the first match per line. Almost always wrong for config files — add g.
  • vi on AIX and older Solaris may not support gg — use :1 to jump to line 1 instead. The colon commands (:n, :w, :q) are universal across vi implementations.
  • Do not confuse vi and vim. Most modern Linux systems alias vi to vim, which adds syntax highlighting and extended undo history. AIX and older Solaris ship the original POSIX vi. If Ctrl+r does not redo, you are on POSIX vi — use . and re-apply the edit manually.

Insights and Best Practices

Always Back Up Before Editing Oracle Config Files

1cp $ORACLE_HOME/network/admin/tnsnames.ora tnsnames.ora.$(date +%Y%m%d)
2vi $ORACLE_HOME/network/admin/tnsnames.ora

One bad :wq on tnsnames.ora with a misplaced parenthesis can break connectivity for every application on the host. The date-stamped backup takes two seconds.

Use / to Navigate init.ora, Not Line Numbers

init.ora parameter order varies by Oracle version and by who set the instance up. Searching by parameter name (/db_cache_size) is more reliable than memorizing line numbers across instances.

Editing the Alert Log Read-Only

To examine the alert log without risk of accidentally modifying it:

1vi -R $ORACLE_BASE/diag/rdbms/$ORACLE_SID/$ORACLE_SID/trace/alert_$ORACLE_SID.log

The -R flag opens in read-only mode. :q! will still exit cleanly — the ! is not needed but does not hurt.

The Minimal vi Survival Kit for On-Call

If you are on-call and get paged at 2am with no muscle memory for vi, these six commands get you through most emergencies:

  1. i — enter insert mode
  2. Esc — exit insert mode
  3. :wq — save and quit
  4. :q! — quit without saving
  5. /pattern — find something
  6. :%s/old/new/g — change something everywhere

Everything else is efficiency. These six are correctness.

When to Use This Reference

  • Editing init.ora or spfile-generated text export to change a parameter
  • Modifying listener.ora to add a static service or change the port
  • Adding a connection entry to tnsnames.ora
  • Reading the Oracle alert log on a host without less configured
  • Making a targeted change to an Oracle Linux crontab (crontab -e)
  • Any host where nano, emacs, or $EDITOR is not set and vi is what opens

Troubleshooting Common Issues

Stuck in insert mode and keys are producing garbage characters: Press Esc several times. If the terminal is corrupted from a dropped SSH session, type reset blindly and press Enter — this resets the terminal state without needing to see the output.

:wq fails with "read-only file": The file has permissions that prevent writing. Check with :!ls -la % (the % expands to the current filename). If you have sudo rights, :w !sudo tee % writes the buffer through sudo without leaving vi.

Search pattern not found on Oracle Linux but works on dev: Character encoding difference or trailing whitespace. Try /parameter_name\s*= to match the parameter name followed by optional whitespace before the =. Oracle-generated init.ora files sometimes have inconsistent spacing around =.

References

Posts in this series