Editing Commands and Scripts#

Interactive editing covers basically everything we do in a shell. Here we’ll look into the various ways to edit commands and text files and how they tie together.


Bash command line editing is provided by the GNU Readline library, which is also used by many other programs. This is why text editing works remarkably similar in interactive shells for other programming languages – many of them, like Python, also use Readline.

The things that keyboard shortcuts bind to in Readline are variously called “commands”, “functions” and “macros” in the documentation. In Readline, “command” and “function” are synonymous. Commands/functions do something to the state of the text (like copying a word or changing it to uppercase) while macros insert some literal text. I will use “macro” to refer to both Readline commands/function and macros to distinguish them from Bash commands and functions.

Readline comes with a lot of macros – there are macros even for moving one character to the left (backward-char) and “accepting” (usually running) a line (accept-line). We’ll look at some common ones here (assuming a default configuration), how to create keyboard shortcuts, and some quality–of–life examples.

Moving the cursor#

The left and right arrows move one character at a time. Combined with the Ctrl key they move one word at a time. You can also use Home and End to go to the start and end of the line, respectively.

Readline and Bash define “word” differently. Bash uses $IFS, the internal field separator variable (by default consisting of a space character, tab character and newline) to split words, while Readline treats only alphanumeric characters as part of words. This means a string like foo-bar is one “Bash word” and two “Readline words.”

Editing a command on the command line can get clunky, in particular when working on a command on multiple lines. It’s not possible to go back up to the previous lines to edit them within Bash itself, but we can use the Ctrl–x + Ctrl–e shortcut to open the command as a temporary file in an editor. That way we can modify multiple lines freely, and when exiting the editor the saved file is executed as if we typed it directly.

Deleting text#

Backspace and Delete delete backwards and forwards, respectively. Alt–Backspace deletes a word backwards. The following setting configures Alt–Delete to delete a word forwards; see Configuring Readline for how to add it.

Command history#

By default, Bash will:

  • record most new commands in memory,

  • save them to a file when the shell exits, and

  • read that file when starting.

Let’s unpack that with an example session as a brand–new user:

In other words, $HISTFILE is the absolute path to the current history file. Continuing the session above, we’ll try to print the contents of that file using the cat (concatenate) command:

This matches the description of the behavior above: this file only exists once we exit Bash for the first time as this user. In other words, the Bash history has not yet been written to disk. We can access the previous commands from memory by using the Up and Down buttons. Basically, after running a command, pressing Up will replace the current command line with the previous command we ran, cat "$HISTFILE", and put the cursor at the end of that line. If we press Up again it will instead show the echo "$HISTFILE" command. We can then edit that line and run it, or continue pressing Up and Down to find the command we want to run or modify.


This page is a preview of The newline Guide to Bash Scripting

Start a new discussion. All notification go to the author.