-
Notifications
You must be signed in to change notification settings - Fork 4
Ex Mode
Ex mode is a secondary command prompt that can be accessed as a peripheral to the main prompt. It has its own set of built-in commands that can affect the state of the line editor, and it can also execute arbitrary shell commands with !.
| Command | Description |
|---|---|
:s/{old}/{new}/[flags] |
Replace occurrences of old with new. |
:g/{pat}/{cmd} |
Execute the ex command cmd on every line matching pat. |
:normal{seq}/:normal!{seq}
|
Execute seq as a sequence of normal mode keys. ! is optional and disables keymaps. |
:!{cmd} |
Execute cmd as a shell command. |
:r {file} |
Insert the content of file at the current cursor position. |
:r !{cmd} |
Insert the output of cmd at the current cursor position. |
:w {file} |
Write the buffer contents to file. Creates the file if it does not exist. |
:w >> {file} |
Append the buffer contents to file. Creates the file if it does not exist. |
:w !{cmd} |
Execute cmd as a shell command, with the current buffer as stdin. |
:e {file} |
Open file using the program currently defined by $EDITOR. |
:q |
Exits shed. |
:d |
Delete the targeted line(s). |
:y |
Yank (copy) the targeted line(s). |
:pu |
Put (paste) the yanked line(s) after the current line. |
:expand/:!expand
|
Perform shell expansion on the current buffer. ! will remove quotes and split words |
:help {topic} |
Runs the help builtin for the given topic
|
:stash [pop] |
Save and recall in-progress commands. Check help stash for more info |
Custom ex mode aliases can also be registered using the excmd builtin. An alias maps a name to an ex-command body, expanded before the line is parsed; only the leading command word is replaced, so an address or range prefix on the invocation is preserved. For example:
excmd sort='%!sort'Then using :sort in ex mode will filter the whole buffer through the sort command.
When running a shell command, that command will have direct read-write access to the line editor via the following environment variables:
-
$BUFFER- Current line contents -
$CURSOR- Cursor position, can be written back as either a raw byte index or as 'row:col' -
$ANCHOR- Visual selection anchor -
$KEYS- Keys that the line editor will execute upon returning. This can be used to script arbitrary input.
Modifying these variables from within the command updates the editor when it returns. This allows for zsh-like widget functionality, where you can run a command to modify the current line, and then return to the editor with the modified line.
Additionally, if any run of text is selected using visual mode when a command is run, that text will be used as stdin to the command, and the text will be replaced by the stdout of the command. This allows for creating filters that can be applied to the current selection.
Most ex commands accept an optional address or range before the command name to specify which lines to operate on.
Single address forms:
-
{n}- line number (1-indexed).1is the first line. -
.- current line -
$- last line -
.+{n}-nlines after the current line -
.-{n}-nlines before the current line -
/{pat}/- next line matching the patternpat(wraps around) -
?{pat}?- previous line matching the patternpat(also wraps)
Range forms (two addresses separated by ,):
-
{a},{b}- lines from addressato addressb, inclusive -
%- all lines in the buffer (equivalent to1,$)
If no address is given, behavior is decided by the command. Most operate on the current line, but some (like :expand) target the entire buffer by default.