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 regular expression substitution in the whole file
$ %s/[regular_expression]/[replacement]/g<Enter>
copy

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

Switch to Vim
$ visual<Enter>
copy

SYNOPSIS

ex [options] [file...]

PARAMETERS

-s
    Silent mode. Suppresses all interactive user feedback. Useful for scripting.

-v
    Sets vi mode, opposite of ex mode.

-r [file]
    Recover file after a system crash.

-R
    Read-only mode. Prevents accidental modifications.

-t tag
    Edit the file containing the tag. It's a way to position the cursor to a specific location using ctags like functionality.

-c command
    Execute the ex command upon startup.

+command
    Execute the ex command upon startup.

DESCRIPTION

ex is a line-oriented text editor, a predecessor to the full-screen editor vi. It's designed for batch editing, scripting, and situations where a full-screen editor isn't suitable. ex is rarely used directly by humans anymore as vi and its modern incarnations (e.g., Vim, Neovim) have superseded it for interactive editing. However, understanding ex is crucial because vi often runs in ex mode, and many vi commands are actually ex commands (prefixed with a colon, e.g. :wq). You can use ex to automate repetitive editing tasks via scripts, apply changes to multiple files, or perform complex text manipulations that would be cumbersome to do manually in a GUI editor. ex offers powerful features like global search and replace, line addressing, and macro definitions.

CAVEATS

Modern systems might alias `ex` to `vi` or another vi-compatible editor. Therefore, behaviour could vary based on the specific implementation.

ADDRESSING

ex commands operate on lines of text. Addresses can be specified using line numbers (e.g., `1`, `$`), regular expressions (`/pattern/`), or relative offsets (`.`, `+`, `-`). A range of lines can be specified using a comma or semicolon (e.g., `1,5` or `1;5`).
For example: `1,$/d` deletes all lines from the first to the last line in the file.

COMMANDS

Common ex commands include `p` (print), `d` (delete), `s` (substitute), `g` (global), `w` (write), `q` (quit), and `!command` (execute a shell command).
For example `:g/pattern/s//replacement/g` Globally search for lines matching `pattern` and replace all occurrences with `replacement`.

HISTORY

ex was created by Bill Joy in 1976 as an extended version of the ed editor. It introduced features like multiple buffers, regular expressions, and more sophisticated addressing. ex became the foundation for vi, which added a visual, full-screen interface. The rise of vi largely eclipsed ex as a direct user tool, but its command set is still essential to understand vi's functionality.

SEE ALSO

vi(1), ed(1), sed(1), awk(1)

Copied to clipboard