Command-Line Editing with tcsh

Mac OS X’s default shell, tcsh, lets you move your cursor around in the command line, editing the line as you type. There are two main modes for editing the command line, based on the two most commonly used text editors: Emacs and vi. Emacs mode is the default; you can switch between the modes with:

bindkey -e

Select Emacs bindings

bindkey -v

Select vi bindings

The main difference between the Emacs and vi bindings is that the Emacs bindings are modeless (i.e., they always work). With the vi bindings, you must switch between insert and command modes; different commands are useful in each mode. Additionally:

  • Emacs mode is simpler; vi mode allows finer control.

  • Emacs mode allows you to cut text and set a mark; vi mode does not.

  • The command-history-searching capabilities differ.

Emacs Mode

Table 4-2 through Table 4-4 describe the various editing keystrokes available in Emacs mode.

Table 4-2. Cursor-positioning commands (Emacs mode)

Command

Description

Control-B

Move the cursor back (left) one character

Control-F

Move the cursor forward (right) one character

Esc-B

Move the cursor back one word

Esc-F

Move the cursor forward one word

Control-A

Move the cursor to the beginning of the line

Control-E

Move the cursor to the end of the line

Table 4-3. Text-deletion commands (Emacs mode)

Command

Description

Del

Control-H

Delete the character to the left of the cursor

Control-D

Delete the character under the cursor

Esc-D

Delete the next word

Esc-Delete or Esc-Control-H

Delete the previous word

Control-K

Delete from the cursor to the end of the line

Control-U

Delete the entire line

Table 4-4. Command control (Emacs mode)

Command

Description

Control-P

Recall the previous command from history

Control-N

Recall the next command from history

Up arrow

Recall the previous command from history

Down arrow

Recall the next command from history

cmd-fragment Esc-P

Search history for cmd-fragment, which must be the beginning of a command

cmd-fragment Esc-N

Like Esc-P, but search forward in the history

Esc num

Repeat the next command num times

Control-Y

Yank the previously deleted string

vi Mode

vi mode has two submodes: insert mode and command mode. The default mode is insert. You can toggle between the modes by pressing Esc; alternatively, in command mode, typing a (append) or i (insert) will return you to insert mode.

Table 4-5 through Table 4-11 describe the editing keystrokes available in vi mode.

Table 4-5. Commands available (vi’s Insert and Command mode)

Command

Description

Control-P

Recall the previous command from history

Control-N

Recall the next command from history

Up arrow

Recall the previous command from history

Down arrow

Recall the next command from history

Table 4-6. Editing commands (vi Insert mode)

Command

Description

Control-B

Move the cursor back (left) one character

Control-F

Move the cursor forward (right) one character

Control-A

Move the cursor to the beginning of the line

Control-E

Move the cursor to the end of the line

Delete or Control-H

Delete the character to the left of the cursor

Control-W

Delete the previous word

Control-U

Delete from the beginning of the line to the cursor

Control-K

Delete from the cursor to the end of the line

Table 4-7. Cursor-positioning commands (vi Command mode)

Command

Description

h or Control-H

Move the cursor back (left) one character

l or Space

Move the cursor forward (right) one word

w

Move the cursor forward (right) one word

b

Move the cursor back (left) one word

e

Move the cursor to the ending of the next word

W, B, E

Like w, b, and e, but treat whitespace as a word separator instead of any nonalphanumeric character

^ or Control-A

Move the cursor to the beginning of the line (first nonwhitespace character)

O

Move the cursor to the beginning of the line

$ or Control-E

Move the cursor to the end of the line

Table 4-8. Text-insertion commands (vi Command mode)

Command

Description

a

Append new text after the cursor until Esc is pressed

i

Insert new text before the cursor until Esc is pressed

A

Append new text after the end of the line until Esc is pressed

I

Insert new text before the beginning of the line until Esc is pressed

Table 4-9. Text-deletion commands (vi Command mode)

Command

Description

x

Delete the character under the cursor

X or Delete

Delete the character to the left of the cursor

dm

Delete from the cursor to the end of motion command m

D

Same as d$

Control-W

Delete the previous word

Control-U

Delete from the beginning of the line up to the cursor

Control-K

Delete from the cursor to the end of the line

Table 4-10. Text-replacement commands (vi Command mode)

Command

Description

cm

Change the characters from the cursor to the end of motion command m until Esc is pressed

C

Same as c$

rc

Replace the character under the cursor with the character c

R

Replace multiple characters until Esc is pressed

s

Substitute the character under the cursor with the characters typed until Esc is pressed

Table 4-11. Character-seeking motion commands (vi Command mode)

Command

Description

fc

Move the cursor to the next instance of c in the line

Fc

Move the cursor to the previous instance of c in the line

tc

Move the cursor just after the next instance of c in the line

Tc

Move the cursor just after the previous instance of c in the line

;

Repeat the previous f or F command

,

Repeat the previous f or F command in the opposite direction

Additional Command-Line Keys

As was just illustrated, the tcsh shell offers dozens of special keystroke characters for navigation on the command line. Table 4-12 lists some additional command-line keys for use in either Emacs or vi editing mode.

Table 4-12. Additional key commands for the tcsh shell

Key command

Description

Control-C

Interrupt the process; cancels the previous command (

Additional key commands for the tcsh shell

-. works as well).

Control-D

Used to signal end of input for some programs and return you to the shell prompt. If Control-D is issued at a shell prompt, it will close the Terminal window.

Control-I

Display an ls-style listing of a directory’s contents; directories in the output will have a forward slash ( / ) after the directory name.

Control-J

Same as pressing the Return (or Enter) key; hitting Control-J after issuing a command will invoke the command, or it will take you to the next line in the shell if no command was given.

Control-K

Remove everything to the right of the insertion point.

Control-L

Clear the display.

Control-Q

Restart output after a pause by Control-S.

Control-S

Pause the output from a program that’s writing to the screen.

Control-T

Transpose the previous two characters.

Control-Z

Suspend a process. To restart the process, issue the bg or fg command to place the process in the background or foreground, respectively.

Esc-C

Capitalize the word following the insertion point.

Esc-Esc

If only a partial path or filename is entered, pressing the Esc key twice will complete the name.

Esc-L

Change the next word to all lowercase letters.

Esc-U

Change the next word to all uppercase letters.

Tab

Has the same effect as pressing the Esc key twice.

Get Mac OS X Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.