Modes

ESC        return to command mode
i, a, o    enter insert mode
:          enter ex mode

Movement

h / l      move left / right
j / k      move down / up
w / b      next / previous word
0          beginning of line
^          first non-blank character
$          end of line
1G         go to first line
G          go to last line
:n or nG   go to line number n

Information

Ctrl-G     show file info (current line, total lines, %)

Editing

x          delete character
dd         delete line
dw, d$     delete word / to end of line
u          undo last change
.          repeat last change

Joining Lines

J          join the next line to the current one
2J         join two lines
:g/^\s*$/d delete all blank lines

In nvi, joining lines follows the historical vi behavior and normalizes whitespace when lines are joined. Unlike Vim, gJ in nvi may still insert or normalize spaces.

For more precise control, Ex-mode join commands can be used:

:j
:j!
:2;+j
:g/pattern/.;+j

Backspace Limitations

Note: In nvi, the Backspace key works only within the same line in insert mode. It cannot delete across line boundaries. Use J in command mode to join lines instead.

Copy & Paste

yy         yank (copy) line
p / P      paste after / before
"ayy       yank into register 'a'
"ap        paste from register 'a'

Search & Replace

/pattern      search forward
?pattern      search backward
n / N         next / previous match
:%s/old/new/g replace all in file
:s/old/new/g  replace all on current line

Files & Buffers

:e file    open another file
:w         write (save) file
:q         quit
:q!        quit without saving
:wq        write and quit
ZZ         write and quit (same as :wq)

Configuration

:set nu           show line numbers
:set autoindent   enable auto-indent
:set ignorecase   case-insensitive search
:set list         show invisible chars

Key Mapping

nvi supports traditional vi-style mappings. Unlike Vim, it does not implement advanced mapping features such as <Leader>, <silent>, <expr>, or :set keymap=.

Only the classic commands are available:

:map        " Normal mode mapping
:map!       " Insert mode mapping
:unmap      " Remove a mapping
:ab         " Abbreviation

Basic Examples

" Faster vertical movement
map J 5j              " J moves down 5 lines (replaces join command)
map K 5k              " K moves up 5 lines (replaces man lookup)

" Auto-correct common typos
ab teh the            " Typing 'teh' automatically becomes 'the'
ab adn and            " Typing 'adn' automatically becomes 'and'

Notes:

  • These mappings work without requiring special character entry

  • J and K replace their default commands (join lines and man lookup)

  • Abbreviations (:ab) work in insert mode and expand when you type a space, tab, or punctuation after the abbreviation

Running External Commands (Advanced)

nvi can execute external shell commands from Ex mode. Any Ex command normally entered after : can also be executed at startup using -c.

:!command
    Run a shell command and display its output.
    The buffer is not modified.

:%!command
    Replace the selected lines with the command's output.

Canonical examples:

# Inspect state before editing (does not modify the buffer)
nvi -c '!test -f file.txt && echo "file exists"' file.txt

# Format the entire file to 62 columns
nvi -c '%!fmt -w 62' file.txt

Rule of thumb: Use !command to inspect, and %!command to transform.

Additional tested examples

# Apply a configuration option at startup (same as :set nu)
nvi -c 'set nu' file.txt

# Enable line numbers and jump to a specific line
nvi -c 'set nu | 42' file.txt

# Jump to the last line
nvi -c '$' file.txt

# Search for a pattern (may prompt with "Press Enter to continue")
nvi -c '/Git' file.txt

# Sort the entire file
nvi -c '%!sort' file.txt

# Prefix each line with its line number (awk)
nvi -c '%!awk '\''{print NR ": " $0}'\''' file.txt

# Format only a range of lines to 62 columns
nvi -c '1,20!fmt -w 62' file.txt

Notes:

  • Only one -c option may be specified; multiple Ex commands can be chained using |

  • Commands that produce output may display "Press Enter to continue:" before interactive editing begins

  • %!command replaces text in the buffer; changes are not written to disk until :w is used

  • Shell quoting rules apply; complex commands may require escaping

Help

:viusage      show vi command reference
:exusage      show ex command reference
:q            exit help view

.nexrc Configuration Examples

The .nexrc file (or .exrc) in your home directory allows you to set default options for nvi. Settings can also be placed in a local .nexrc in your current directory (requires set exrc in your home .nexrc).

Minimal Configuration

A basic setup with essential visual feedback:

" Minimal .nexrc configuration
set showmode          " Show current mode (INSERT/COMMAND)
set showmatch         " Briefly jump to matching brackets
set ruler             " Show cursor position in status line
set flash             " Flash screen instead of beeping
set autoindent        " Auto-indent new lines
set report=1          " Report any line changes

Configuration for C Programming

Settings optimized for C and shell script editing:

" C programming .nexrc
set showmode showmatch ruler flash
set autoindent
set tabstop=8 shiftwidth=8
set wraplen=80
set ignorecase iclower
set report=1
set number

Commonly Used Options

" Display & Feedback
set showmode          " Show current mode (INSERT/COMMAND)
set showmatch         " Briefly jump to matching bracket
set ruler             " Show cursor position (line, column)
set number            " Show line numbers
set flash             " Flash screen instead of beeping

" Indentation & Formatting
set autoindent        " Auto-indent new lines
set tabstop=8         " Tab width (8 for kernel, 4 for apps)
set shiftwidth=8      " Indent width for << and >>
set wraplen=80        " Wrap lines at 80 columns

" Search
set ignorecase        " Case-insensitive search
set iclower           " Smart case (respect uppercase)
set searchincr        " Incremental search
set wrapscan          " Searches wrap around file

" Editing
set report=1          " Report changes affecting 1+ lines
set autowrite         " Auto-save when changing files
set backup            " Backup files before overwriting

" Command Line (Tab completion & history editing)
set cedit=<Tab>       " Press Tab on : command-line to edit history
set filec=<Tab>       " Press Tab to autocomplete filenames

" Security
set secure            " Disable external programs

Command Line Tips:

  • cedit: While on the : command-line, press Tab to open your command history in a buffer for editing

  • filec: While typing a filename (e.g., :e /etc/pas), press Tab to autocomplete

  • Both require a literal tab character in .nexrc: type set cedit=, then press Ctrl+V followed by Tab

File Locations

/etc/vi.exrc          System-wide configuration (always read first)
$HOME/.nexrc          User configuration (first choice)
$HOME/.exrc           User configuration (second choice)
.nexrc                Local directory (requires set exrc)
.exrc                 Local directory (second choice)

Note: $HOME/.nexrc and $HOME/.exrc are skipped if NEXINIT or EXINIT is set. See the EXINIT section below.

For a complete list of all available options, see man vi or :exusage.


EXINIT

EXINIT is the standard ex/vi initialization variable, honored by nvi and traditional vi implementations. nvi also supports NEXINIT, which takes precedence over EXINIT if both are set.

The startup initialization follows this order:

  1. System-wide /etc/vi.exrc (always read first)

  2. NEXINIT environment variable (if set)

  3. EXINIT environment variable (else if set)

  4. $HOME/.nexrc or $HOME/.exrc (only if neither NEXINIT nor EXINIT is set)

  5. Local .nexrc or .exrc (only if set exrc is enabled)

It is most useful when no configuration file is needed:

# Minimal setup with mode display and key remapping
export EXINIT='set showmode|set showmatch|set flash|set ruler|set report=1|map J 5j|map K 5k'

Commands are separated by |.

XDG Base Directory

nvi does not support XDG natively. As a workaround, add the following to your shell profile (e.g. ~/.bashrc):

export EXINIT='source ~/.config/nvi/nexrc'

Ensure ~/.nexrc and ~/.exrc do not exist, as they take precedence over EXINIT.


Important Notes

  • Undo: u undoes the last change (repeating u toggles undo/redo between two states). Use . immediately after u to step through the change log (multi-level undo/redo).

  • No visual mode: Use counts like d3w (delete 3 words) instead of text selection.

  • Set as default editor:

# Symlink (requires admin):
sudo ln -s /usr/bin/nvi /usr/bin/vi

# OR set per-user environment:
export EDITOR=nvi
export VISUAL=nvi

Project repository: https://repo.or.cz/nvi.git

This document is maintained using nvi.