LinuxCommandLibrary

ex

Edit text files (line editor)

TLDR

Open a file

$ ex [path/to/file]
copy

Save and Quit
$ wq<Enter>
copy

Undo the last operation
$ undo<Enter>
copy

Search for a pattern in the file
$ /[search_pattern]<Enter>
copy

Perform a regex substitution in the whole file
$ %s/[regex]/[replacement]/g<Enter>
copy

Insert text
$ i<Enter>[text]<Ctrl c>
copy

Switch to Vim
$ visual<Enter>
copy

SYNOPSIS

ex [options] [+command | +/pattern] [file ...]

PARAMETERS

-v
    Invoke vi (visual mode) instead of ex

-e
    Script mode: suppress prompts, error bells; use for batch editing

-s
    Silent mode: like -e, no prompts or non-error messages

-r [file]
    Recover previous edit session from recovery file

-R
    Readonly mode: edits allowed but warn on write attempts

-L
    List recovery files and exit

-l
    Enter with lisp option set for Lisp code editing

-c command
    Execute command on startup, then enter interactive mode

-t tag
    Edit file containing tag from tags file

+command
    Like -c, execute after file read

+/pattern
    Place cursor on first line matching pattern

-n
    No swap file (Vim extension)

-h
    Help mode (Vim extension)

DESCRIPTION

ex is a classic Unix line-oriented text editor, serving as the foundation for the visual editor vi. Developed as an extension ('ex' for extended) to the ed editor, it allows precise editing via colon-prefixed commands entered at the screen bottom. Ideal for scripting, batch processing, and non-interactive use, ex supports addressing lines by number, pattern, or mark; global substitutions; macros; and file I/O operations.

Users enter a colon (:) followed by commands like g/pattern/d to delete matching lines or w file to write changes. It lacks vi's screen-based navigation but offers superior power for complex edits. Modern Linux distributions provide ex via vim (invoked as vim -e) or nvi, maintaining compatibility with original BSD behavior. Though overshadowed by visual editors, ex remains valuable for automated tasks, embedded scripts, and learning vi internals.

Key strengths include portability, minimal interface, and extensibility through exrc files. It's less beginner-friendly due to its modal, command-driven nature.

CAVEATS

Primarily for scripts; interactive use prefers vi. Swap/recovery varies by implementation (vim, nvi). Deprecated on some systems; invoke via vim -e. Limited screen handling without -v.

STARTUP FILES

Reads $EXINIT or ~/.exrc for initial set commands, mappings, abbreviations.

ADDRESSING

Lines via numbers (5), searches (/pat/), marks ('a), or ranges (1,$). Defaults to current line (.).

EXITING

q! to quit without saving; x or ZQ for modified buffers; wq to save and quit.

HISTORY

Created by Bill Joy in 1976 at UC Berkeley for 1BSD as an 'ed' superset. Released publicly in 2BSD (1979) alongside vi. Evolved through 4BSD; POSIX standardized subset. Maintained in vim (1991+ by Bram Moolenaar) and nvi (1990s by Steve Kirkendall) for POSIX compliance. Usage shifted to visual modes, but scripting endures.

SEE ALSO

vi(1), vim(1), ed(1), sed(1), nvi(1)

Copied to clipboard