If you’re comfortable editing text in your favorite editor and frustrated using Readline’s shortcuts, you’re not alone. The good news is that there’s something easier—and a little more powerful. By pairing Readline with an external editor, you can edit a command line using familiar key sequences.
Before we look at how Readline chooses which editor to launch, here’s a quick review of the key sequences you’ll use with Readline, Vim, and Nano.
| Application | Key | Sequence | OS | Definition |
|---|---|---|---|---|
| Precursor | A precursor key is the first key pressed in a key sequence. This key is used to modify the meaning of the following key or key sequence that, in most cases, is used to modify a condition or function or execute a command. Once activated, this key modifies the meaning of the following key (or keys) in the sequence. In most cases, this is used to change a current condition, toggle a specific software function, or execute a direct system command. An example of a precursor key is the ESC key. |
|||
| Modifier | A modifier key is a specific type of precursor key that is held down while simultaneously pressing the next key or key sequence. Examples of modifier keys are the Ctrl, Meta, and Cmd keys. |
|||
| Control | All | The Control (Ctrl) key is a modifier key that, when held while simultaneously selecting another key, creates a precursor to execute specialized system or application commands. | ||
| Meta | All | The Meta key is a specific type of modifier key that, when held while simultaneously selecting another key, transforms the resulting key sequence into a precursor for text-editing or navigation commands. In Linux or Windows environments, the Alt key typically functions as the meta key. On macOS, the Option (⌥) key serves this role, provided it has been enabled as the meta key within the terminal profile settings. |
||
| Escape | Windows Linux | The Escape (Esc) key is a precursor key that is tapped, rather than held. The next key depressed defines whether the Esc key signals the system or application to exit a current mode, cancel an ongoing operation, return to a previous state, or enter a specific command mode. | ||
| Command | macOS | The Command (⌘) key is a primary modifier key used in macOS. When held while simultaneously selecting another key, the resulting key sequence acts as a precursor to execute system or application commands, allowing users to initiate actions—such as opening files and launching applications—while also modifying the functional behavior of the active interface. | ||
| Cmd-e | macOS | The key sequence Cmd-e is a precursor that opens the editor specified by either the VISUAL or EDITOR variable, inserts the contents of the buffer into that editor, and—once editing is complete—writes the updated contents back to the buffer | ||
| C-x | Windows Linux | The key sequence C-x creates a special type of precursor that causes Readline to wait for the following key or key sequence. That next input is then compared against the map of sequences defined under C-x; if a match is found, Readline executes the associated command. | ||
| C-e | Windows Linux | The key sequence C-e is a precursor that executes the command edit‑and‑execute‑command. This command starts the editor defined in either the VISUAL or EDITOR variables,, loads the buffer into the editor, and when editing is completed, writes the contents back to the buffer. |
||
| C -x C-e | Windows Linux | The key sequence C-x |
||
| nano | ||||
| M-! command | N/A | Open a shell, execute a command and read the command’s output into the editor. Please not this command should be written on one line: M-! <command> |
||
| C-x | N/A | Write and quit | ||
| vim | ||||
| <ESC> | N/A | Switch to command mode | ||
| <ESC>ZZ | N/A | Write file changes and quit vim | ||
| <ESC>: | N/A | Switch to command line mode | ||
| <ESC>: r command | N/A | Execute a command and read the command’s output into the editor. Please not this command should be written on one line: <ESC>: r <command> |
||
| vim continued | ||||
| <ESC>: wq | N/A | Write and quit |
Here’s what happens behind the scenes. As you type a command on a Bash shell command line, Readline stores the text in an editable buffer.
When you press the Readline key sequence C‑x + C‑e in a Linux or Windows environment Readline writes the contents of the buffer to a temporary file and opens that file in your external editor. After you save the file and close the editor, Readline replaces the buffer with the contents of the saved file. If the file ends with a newline (most editors add this automatically) Bash immediately executes the edited command.
Note: Only the executed command is added to history; the original unedited version is discarded.