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.
| Command | Action |
|---|---|
i | Insert before cursor |
a | Insert after cursor |
o | Open new line below, enter insert mode |
O | Open new line above, enter insert mode |
I | Insert at beginning of line |
A | Insert at end of line |
Esc | Return to normal mode from any insert/command state |
Navigation
| Command | Action |
|---|---|
h / j / k / l | Left / down / up / right (arrow keys also work) |
gg | Jump to first line |
G | Jump to last line |
:n | Jump to line number n (e.g. :142) |
0 | Jump to start of current line |
$ | Jump to end of current line |
w | Forward one word |
b | Back one word |
Ctrl+f | Page down |
Ctrl+b | Page up |
Editing
| Command | Action |
|---|---|
x | Delete character under cursor |
dd | Delete (cut) entire line |
dw | Delete word forward from cursor |
D | Delete from cursor to end of line |
yy | Yank (copy) current line |
p | Paste after cursor / below current line |
P | Paste before cursor / above current line |
u | Undo last change |
Ctrl+r | Redo |
. | Repeat last edit command |
Save and Quit
| Command | Action |
|---|---|
:w | Write (save) without quitting |
:w filename | Write to a different filename |
:q | Quit (only works if no unsaved changes) |
:wq | Write and quit |
ZZ | Write 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
| Command | Action |
|---|---|
/pattern | Search forward for pattern |
?pattern | Search backward for pattern |
n | Jump to next match |
N | Jump to previous match |
: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 entire file |
:%s/old/new/gc | Replace 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 — pressEscseveral times, then:q!. Nothing is saved.uundoes one change at a time. Hold it down or press it repeatedly to step back through edit history.Ctrl+rmoves forward again./search is case-sensitive by default. Add\cto the pattern for case-insensitive:/\csga_targetmatchesSGA_TARGET,sga_target, andSga_Target.:%swith nogflag only replaces the first match per line. Almost always wrong for config files — addg.- vi on AIX and older Solaris may not support
gg— use:1to 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
vitovim, which adds syntax highlighting and extended undo history. AIX and older Solaris ship the original POSIX vi. IfCtrl+rdoes 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:
i— enter insert modeEsc— exit insert mode:wq— save and quit:q!— quit without saving/pattern— find something:%s/old/new/g— change something everywhere
Everything else is efficiency. These six are correctness.
When to Use This Reference
- Editing
init.oraorspfile-generated text export to change a parameter - Modifying
listener.orato add a static service or change the port - Adding a connection entry to
tnsnames.ora - Reading the Oracle alert log on a host without
lessconfigured - Making a targeted change to an Oracle Linux crontab (
crontab -e) - Any host where
nano,emacs, or$EDITORis 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
- Oracle Database Administrator's Guide — Specifying Initialization Parameters — canonical Oracle 19c reference for init.ora and spfile parameter management
- shutdownabort.com DBA Utilities — vi Reference (Wayback Machine, 2013) — original source corpus from Andrew Barry's Oracle DBA utilities site; this post derives from the Unix tools section
- The Open Group — vi Utility — POSIX specification; authoritative reference for the subset of vi commands that work identically across all Unix implementations including AIX and Solaris
- Oracle Linux Administrator's Guide — covers the Oracle Linux environment where most on-premises Oracle databases run
Posts in this series
- List Files Opened by an Oracle Process with lsof -p
- Delete the 500 Oldest Files in a Directory (Oracle DBA)
- Diagnose Oracle Net and RAC Interconnect Routing with netstat -r
- Unpacking Oracle Patch and Install Media with cpio
- Oracle Export Through a Named Pipe with mknod for Space-Constrained Hosts
- Recursive find and grep to Search Oracle Trace Files for Any String
- Find the Biggest Files First When an Oracle Filesystem Fills Up
- Scan Every Oracle Alert and Trace Log for ORA- Errors with grep
- vi Editor Commands Reference for Oracle DBAs