Command Line Editing

The BASH shell, which is your default shell, has special command line editing capabilities that you may find helpful as you learn Linux (see Table 8-2). You can easily modify commands you have entered before executing them, moving anywhere on the command line and inserting or deleting characters. This is particularly helpful for complex commands. You can use the ctrl-f or right arrow key to move forward a character, or the ctrl-b or left arrow key to move back a character. ctrl-d or del deletes the character the cursor is on, and ctrl-h or backspace deletes the character before the cursor. To add text, you use the arrow keys to move the cursor to where you want to insert text and type the new characters. You can even cut words with the ctrl-w or alt-d key and then use the ctrl-y key to paste them back in at a different position, effectively moving the words. As a rule, the ctrl version of the command operates on characters, and the alt version works on words, such as ctrl-t to transpose characters and alt-t to transpose words. At any time, you can press enter to execute the command. For example, if you make a spelling mistake when entering a command, rather than reentering the entire command, you can use the editing operations to correct the mistake. The actual associations of keys and their tasks, along with global settings, are specified in the /etc/inputrc file.

TIP The editing capabilities of the BASH shell command line are provided by Readline. Readline supports numerous editing operations. You can even bind a key to a selected editing operation. Readline uses the /etc/inputrc file to configure key bindings. This file is read automatically by your /etc/profile shell configuration file when you log in (see Chapter 9). Users can customize their editing commands by creating an .inputrc file in their home directory (this is a dot file). It may be best to first copy the /etc/inputrc file as your .inputrc file and then edit it. /etc/profile will first check for a local .inputrc file before accessing the /etc/inputrc file. You can find out more about Readline in the BASH shell reference manual at

You can also conditionally run several commands on the same line with the && operator (see Chapter 9). A command is executed only if the previous one is true. This feature is useful for running several dependent scripts on the same line. In the next example, the ls command is run only if the date command is successfully executed.

TIP Command can also be run as arguments on a command line, using their results for other commands. To run a command within a command line, you encase the command in back quotes, see Values from Linux Commands later in this chapter.

Movement Commands


ctrl-f, right-arrow

Move forward a character

ctrl-b, left-arrow

Move backward a character

ctrl-a or home

Move to beginning of line

ctrl-e or end

Move to end of line


Move forward a word


Move backward a word


Clear screen and place line at top

Editing Commands


ctrl-d or del

Delete character cursor is on

ctrl-h or backspace

Delete character before the cursor


Cut remainder of line from cursor position


Cut from cursor position to beginning of line


Cut previous word


Cut entire line


Cut the remainder of a word


Cut from the cursor to the beginning of a word


Paste previous cut text


Paste from set of previously cut text


Paste previous cut text


Insert quoted text, used for inserting control or meta (alt) keys as text, such as ctrl-b for backspace or ctrl-t for tabs


Transpose current and previous word


Lowercase current word


Uppercase current word


Capitalize current word


Undo previous change

Table 8-2 Command Line Editing Operations

Table 8-2 Command Line Editing Operations

0 0

Post a comment