home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Linux in a NutshellLinux in a NutshellSearch this book

7.2. Syntax

This subsection describes the many symbols peculiar to bash. The topics are arranged as follows:

  • Special files

  • Filename metacharacters

  • Command-line editing

  • Quoting

  • Command forms

  • Redirection forms

  • Coprocesses

7.2.3. Command-Line Editing

Command lines can be edited like lines in either Emacs or vi. Emacs is the default. See Section 7.5.1 later in this chapter for more information.

vi mode has two submodes, input mode and command mode. The default is input mode; you can go to command mode by pressing Esc. In command mode, typing a (append) or i (insert) returns you to input mode.

Some users discover that the Del or Backspace key on the terminal does not delete the character before the cursor, as it should. Sometimes this problem can be solved by issuing one of the following commands (or placing it in your .bashrc file):

stty erase ^?
stty erase ^H

See the stty command in Chapter 3 for more information. On the X Window System, an alternative solution is to use the xmodmap command; this is not described here, as it requires some research about your terminal.

Tables Table 7-1 through Table 7-14 show various Emacs and vi commands.

Table 7-1. Basic Emacs-mode commands

Command

Description

Ctrl-b

Move backward one character (without deleting).

Ctrl-f

Move forward one character.

Del

Delete one character backward.

Ctrl-d

Delete one character forward.

Table 7-2. Emacs-mode word commands

Command

Description

M-b

Move one word backward.

M-f

Move one word forward.

M-Del

Kill one word backward.

M-d

Kill one word forward.

Ctrl-y

Retrieve (yank) last item killed.

Table 7-3. Emacs-mode line commands

Command

Description

Ctrl-a

Move to beginning of line.

Ctrl-e

Move to end of line.

Ctrl-k

Kill forward to end of line.

Table 7-4. Emacs-mode commands for moving through the history file

Command

Description

Ctrl-p

Move to previous command.

Ctrl-n

Move to next command.

Ctrl-r

Search backward.

M-<

Move to first line of history file.

M->

Move to last line of history file.

Table 7-5. Emacs-mode completion commands

Command

Description

Tab

Attempt to perform general completion of the text.

M-?

List the possible completions.

M-/

Attempt filename completion.

Ctrl-x /

List the possible filename completions.

M-~

Attempt username completion.

Ctrl-x ~

List the possible username completions.

M-$

Attempt variable completion.

Ctrl-x $

List the possible variable completions.

M-@

Attempt hostname completion.

Ctrl-x @

List the possible hostname completions.

M-!

Attempt command completion.

Ctrl-x !

List the possible command completions.

M-Tab

Attempt completion from previous commands in the history list.

Table 7-6. Miscellaneous Emacs-mode commands

Command

Description

Ctrl-j

Same as Return.

Ctrl-l

Clear the screen, placing the current line at the top of the screen.

Ctrl-m

Same as Return.

Ctrl-o

Same as Return, then display next line in command history.

Ctrl-t

Transpose character left of and under the cursor.

Ctrl-u

Kill the line from the beginning to point.

Ctrl-v

Insert keypress instead of interpreting it as a command.

Ctrl-[

Same as Esc (most keyboards).

M-c

Capitalize word under or after cursor.

M-u

Change word under or after cursor to all capital letters.

M-l

Change word under or after cursor to all lowercase letters.

M-.

Insert last word in previous command line after point.

M-_

Same as M-. .

Table 7-7. Editing commands in vi input mode

Command

Description

Del

Delete previous character.

Ctrl-W

Erase previous word (i.e., erase until a blank).

Ctrl-V

Insert next keypress instead of interpreting it as a command.

Esc

Enter command mode (see Table 7-8).

Table 7-8. Basic vi command-mode commands

Command

Description

h

Move left one character.

l

Move right one character.

b

Move left one word.

w

Move right one word.

B

Move to beginning of preceding nonblank word.

W

Move to beginning of next nonblank word.

e

Move to end of current word.

E

Move to end of current nonblank word.

0

Move to beginning of line.

^

Move to first nonblank character in line.

$

Move to end of line.

Table 7-9. Commands for entering vi input mode

Command

Description

i

Insert text before current character (insert).

a

Insert text after current character (append).

I

Insert text at beginning of line.

A

Insert text at end of line.

r

Replace current character with next keypress.

R

Overwrite existing text.

Table 7-10. Some vi-mode delete commands

Command

Description

dh

Delete one character backward.

dl

Delete the current character.

db

Delete one word backward.

dw

Delete one word forward.

dB

Delete one nonblank word backward.

dW

Delete one nonblank word forward.

d$

Delete to end-of-line.

d0

Delete to beginning of line.

Table 7-11. Abbreviations for vi-mode delete commands

Command

Description

D

Delete to end of line (equivalent to d$).

dd

Delete entire line (equivalent to 0d$).

C

Delete to end of line; enter input mode (equivalent to c$).

cc

Delete entire line; enter input mode (equivalent to 0c$).

X

Delete character backward (equivalent to dh).

x

Delete the current character (equivalent to dl).

Table 7-12. vi-mode commands for searching the command history

Command

Description

k or -

Move backward one line.

j or +

Move forward one line.

G

Move to first line in history.

/string

Search backward for string.

?string

Search forward for string.

n

Repeat search in same direction as previous.

N

Repeat search in opposite direction of previous.

Table 7-13. vi-mode character-finding commands

Command

Description

fx

Move right to next occurrence of x.

Fx

Move left to previous occurrence of x.

tx

Move right to next occurrence of x, then back one space.

Tx

Move left to previous occurrence of x, then forward one space.

;

Redo last character-finding command.

,

Redo last character-finding command in opposite direction.

Table 7-14. Miscellaneous vi-mode commands

Command

Description

~

Invert (toggle) case of current character(s).

_

Insert last word of previous command after cursor; enter input mode.

Ctrl-L

Clear the screen and redraw the current line on it; good for when your screen becomes garbled.

#

Prepend # (comment character) to the line and send it to the history file; useful for saving a command to be executed later, without having to retype it.

7.2.6. Redirection Forms

File descriptor

Name

Common abbreviation

Typical default

0

Standard input

stdin

Keyboard

1

Standard output

stdout

Screen

2

Standard error

stderr

Screen

The usual input source or output destination can be changed as shown in Table 7-15.

Table 7-15. I/O redirectors

Redirector

Function

> file

Direct standard output to file.

< file

Take standard input from file.

cmd1 | cmd2

Pipe; take standard output of cmd1 as standard input to cmd2.

>> file

Direct standard output to file; append to file if it already exists.

>| file

Force standard output to file even if noclobber is set.

n>| file

Force output from the file descriptor n to file even if noclobber is set.

<> file

Use file as both standard input and standard output.

<< text

Read standard input up to a line identical to text (text can be stored in a shell variable). Input is usually typed on the screen or in the shell program. Commands that typically use this syntax include cat, echo, ex, and sed. If text is enclosed in quotes, standard input will not undergo variable substitution, command substitution, etc.

n> file

Direct file descriptor n to file.

n< file

Set file as file descriptor n.

>&n

Duplicate standard output to file descriptor n.

<&n

Duplicate standard input from file descriptor n.

&>file

Direct standard output and standard error to file.

<&-

Close the standard input.

>&-

Close the standard output.

n>&-

Close the output from file descriptor n.

n<&-

Close the input from file descriptor n.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.