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 www.gnu.org/manual/bash.

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

Operation

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

alt-f

Move forward a word

alt-b

Move backward a word

ctrl-l

Clear screen and place line at top

Editing Commands

Operation

ctrl-d or del

Delete character cursor is on

ctrl-h or backspace

Delete character before the cursor

ctrl-k

Cut remainder of line from cursor position

ctrl-u

Cut from cursor position to beginning of line

ctrl-w

Cut previous word

ctrl-c

Cut entire line

alt-d

Cut the remainder of a word

alt-del

Cut from the cursor to the beginning of a word

ctrl-y

Paste previous cut text

alt-y

Paste from set of previously cut text

ctrl-y

Paste previous cut text

ctrl-v

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

alt-t

Transpose current and previous word

alt-l

Lowercase current word

alt-u

Uppercase current word

alt-c

Capitalize current word

ctrl-shift-_

Undo previous change

Table 8-2 Command Line Editing Operations

Table 8-2 Command Line Editing Operations

0 0

Post a comment