Chapter 7. The tcsh Command-Line Editor

In This chapter:

  • Editing a Command

  • Command Key Bindings

  • emacs Editing Mode

  • vi Editing Mode

  • Examining and Modifying Key Bindings

tcsh provides a general purpose command-line editor. You can use it to retrieve and repeat commands from your history list (either as is or in modified form), or to modify the current command. For instance, if you notice a typo at the beginning of the line, you can move the cursor to the beginning and correct the mistake, instead of erasing the command and retyping it.

The command editor is extremely useful, but it can be daunting initially. When you first try it, don’t attempt to learn everything at once. Instead of trying to commit dozens of editing commands to memory in a single session, start with a few. Once you’re comfortable, learn a few more. Take it slow, but stick with it—using the editor becomes increasingly natural with practice, and you’ll be glad you took the time to learn it.

This chapter is organized into the following sections:

  • An overview of the editing process

  • How to choose a set of bindings and ensure that you get the right set at login time

  • How to edit commands using the emacs or vi bindings

  • How to get information about or change your current bindings

Editing a Command

You can use the editor to make arbitrarily complex changes to a command, but the editing process is conceptually quite simple. Type in a new command (or retrieve a command from your history list); if it needs modification (e.g., to create a similar command, or to fix mistakes), edit the command by adding new text or changing existing text; then hit RETURN to execute it.

To retrieve commands from your history list, navigate up and down using CTRL-P (previous command) and CTRL-N (next command).[10] One basic idiom you should commit to memory immediately is CTRL-P RETURN to repeat your previous command. There are also editing commands that perform searches on your history list; these commands are described later.

If you elect not to execute the command in the edit buffer, cancel it by typing your interrupt character (usually CTRL-C). Another way of "canceling” a command is to retrieve a different command into the editing buffer, e.g., with CTRL-P.

The cursor need not be at the end of the line to execute or cancel a command.

You won’t have any history list from which to recall commands unless you’ve set the history shell variable. See Chapter 6, Using Your Command History, for instructions.

Command Key Bindings

You control the command editor interface by choosing a set of key bindings—associations between keys and editing commands. There are two sets of bindings, patterned after the editing commands used in GNU emacs and in vi.[11] If the set of bindings you want is not the set tcsh uses by default at your site,[12] make sure you always get the desired bindings by adding the appropriate bindkey command to ˜/.cshrc or ˜/.tcshrc:

bindkey -e                   Select emacs bindings
bindkey -v                   Select vi bindings

You can also use bindkey commands to add, remove, or modify individual bindings. See Examining and Modifying Key Bindings, later in this chapter.

bindkey is a tcsh-specific command. If you use ˜/.cshrc rather than ˜/.tcshrc, protect any bindkey statements from csh, using the instructions in the section Organizing Your Startup Files in Chapter 4, The Shell Startup Files.

Getting Key Binding Information

With either set of bindings, you’ll sometimes need to review the functions of certain keys as you learn the command editor. The following command is useful:

% bindkey

bindkey, with no arguments, displays your current key bindings by showing the associations between keystrokes and editing command names. Keystrokes are displayed using ^X for CTRL-X, ^[ for ESC, ^? for DEL, and nnn for the ASCII character with octal code nnn. The nnn notation is obscure; typically these are keystrokes you type by holding down the META key. It’s usually easier to figure out what META-c does by looking for the command associated with ESC c (displayed as ^[c), because META-c and ESC c execute the same command.

If you don’t know what an editing command name means, use the following command to get a list of short descriptions:

% bindkey -l

Comparison of emacs Mode and vi Mode

If you’re familiar with emacs or vi, the corresponding set of command editor bindings is generally similar to what you’re already used to, so you may simply want to use the bindings that most resemble whichever editor you prefer. Otherwise, you can try both sets and see which you like best. The following comparison describes some general differences between the emacs and vi bindings:

  • emacs bindings are modeless, i.e., they are always active and you don’t have to think about when you can use them. vi mode, by contrast, actually consists of two modes. The commands are different in each mode, and you switch between modes to do different things. The dual modes can be confusing until you get used to them.

  • emacs, with fewer basic commands, is simpler. vi gives you finer control (for example, vi mode gives you two definitions of a word to use in word motion commands and has character-seeking motion commands).

  • emacs mode maintains a cut buffer from which you can yank deleted text. vi mode does not.

  • In emacs mode, you can set a mark. In vi mode, you can’t.

  • The command history searching capabilities differ.

emacs Editing Mode

This section describes how to edit commands using the emacs bindings. Be sure to select those bindings before trying any of the commands described below:

% bindkey -e

In emacs mode, editing commands are always active. Any character that is not an editing command is inserted into the edit buffer. Otherwise, the command is executed.

emacs Cursor Motion Commands

Before making a change (e.g., adding or deleting text), you position the cursor. The emacs cursor motion commands are shown in Table 7-1.

Table 7-1. Cursor Positioning Commands (emacs Mode)

Command

Description

CTRL-B

Move cursor back (left) one character

CTRL-F

Move cursor forward (right) one character

ESC b

Move cursor back one word

ESC f

Move cursor forward one word

CTRL-A

Move cursor to beginning of line

CTRL-E

Move cursor to end of line

Try these commands by entering the following line, leaving the cursor at the end:

% echo this is a command

Then, enter the editing commands below; you should see the cursor move, as shown:

% echo this is a comman       Type CTRL-B
% echo this is a ommand       Type ESC b
% cho this is a command       Type CTRL-A
% eho this is a command       Type CTRL-F
% echothis is a command       Type ESC f
% echo this is a command      Type CTRL-E

emacs Modification Commands

To add new characters to the current command, just type them. To delete characters, words, or even the entire line, use the commands shown in Table 7-2. Modifications take place at the cursor position.

Table 7-2. Text Deletion Commands (emacs Mode)

Command

Description

DEL or CTRL-H

Delete character to left of cursor

CTRL-D

Delete character under cursor

ESC d

Delete word

ESC DEL or ESC CTRL-H

Delete word backward

CTRL-K

Delete from cursor to end of line

CTRL-U

Delete entire line

Try the modification commands by typing the following line, leaving the cursor at the end:

% echo My typing skilks are not amazing.

Then, use the following editing commands to make the changes shown below:

% echo My typing skilks are not mazing.     Type ESC b
% echo My typing skilks are mazing.         Type ESC CTRL-H
% echo My typing skilk are amazing.         Type CTRL-B several times
% echo My typing skil are amazing.          Type CTRL-H
% ech My typing skill are amazing.         Type l

Deletion commands, other than those that clobber single characters, place the deleted text into a cut buffer. To yank the text back into the command line, use CTRL-Y. You can easily rearrange sections of the command line this way, because the text need not be returned to its original location. You can yank the text into the command line more than once. (Character deletions are not placed in the cut buffer, since you can retype a character to restore it.)

Repeating emacs Commands

If you want to repeat a command several times, it’s often easier to specify a repeat count than to type the command multiple times. To repeat a command n times, precede it with ESC n, where n is some number. For example, ESC 10 CTRL-B moves the cursor back ten characters, and ESC 3 ESC d deletes three words. If a command cannot be repeated as many times as you specify, tcsh repeats it as many times as possible.

Another way to repeat a command, if it consists of a single keystroke, is simply to hold down the key.

emacs History-Searching Commands

emacs mode allows you to search your history list for a command you want to edit. This is often faster than backing up a line at a time with CTRL-P or up arrow. You can specify a command line either by a prefix of the line, or by a string that appears in the line.

To use prefix searching, enter the first part of the command line, then type ESC p. The prefix can contain filename pattern characters. tcsh searches backward through your history for a command that begins with the prefix and retrieves it into the edit buffer. If the command isn’t the one you want, type ESC p again to find the next match. ESC n is similar but searches forward. You can use ESC p and ESC n together to bounce back and forth among the matching commands.

String searching is done using two commands that allow emacs-style incremental searches. These commands are not bound to any keys by default, so you must bind them to some key or key sequence before you can use them. For example, to bind them to CTRL-X p and CTRL-X n, put the following commands in your ˜/.cshrc file:

bindkey "^xp" i-search-back
bindkey "^xn" i-search-fwd

These two commands allow you to search backward or forward, so that as you type a string, the command line containing that string is retrieved into the edit buffer. For example, suppose your history looks like this:

unexpand note.txt > note2.txt
man cal
cal 1066

When you type CTRL-X p, the command editor prints bck: as a prompt, and prepares to search backward for commands. If you then type a, n, and d in succession, tcsh retrieves commands into the edit buffer in the following order:

cal 1066                          Command matches a
man cal                           Command matches an
unexpand note.txt > note2.txt     Command matches and

With incremental searching, you type no more than necessary to find the desired command, and the text you type can be anywhere in the command line. If you enter an extra character, delete it and the search will return to the previous command found.

Hit ESC to terminate an incremental search, so that you can edit the currently displayed command. Or, just hit RETURN to execute the command.

Using the Arrow Keys in emacs Mode

You can use the arrow keys for emacs mode editing. Up arrow and down arrow move up and down through your history list, like CTRL-P and CTRL-N. Left arrow and right arrow move the cursor left and right one character, like CTRL-B and CTRL-F.

vi Editing Mode

This section describes how to edit commands using the vi bindings. Be sure to select those bindings before trying any of the commands described below:

% bindkey -v

The vi bindings provide capabilities similar to those of the emacs bindings, but they have their own distinctive characteristics. The keystrokes to perform the commands sometimes differ, of course; a more significant difference is that editing using the vi bindings really involves two modes—just as with vi itself. In vi insert mode, characters are placed in the edit buffer as you type them. In vi command mode, characters are interpreted as editing commands. For example, if you type an x in insert mode, an x is put into the command line at the cursor position. If you type an x in command mode, the character under the cursor is deleted.

Because the two vi modes behave differently, it’s important to know the rules for switching between them:

  • The editor is in insert mode initially, e.g., if you’re typing a new command, or if you’ve just retrieved a line from your history list into the edit buffer, using CTRL-P.

  • To enter command mode, hit ESC.

  • In command mode, several commands put you into insert mode for entering new text, e.g., a (append after cursor) or i (insert before cursor). From insert mode, you can return to command mode by hitting ESC.

  • If you’re not sure what mode you’re in, hit ESC until tcsh beeps. You will then be in vi command mode.

The dual modes may seem confusing, but they’re such an integral part of vi editing mode that they quickly become second nature. Some of the most common editing commands work in either mode, which helps alleviate confusion. For example, CTRL-P and CTRL-N move up and down through the history list, and RETURN and CTRL-C execute and cancel the current command, no matter which mode you’re in.

Using vi Insert Mode

When you begin to edit a command using the vi bindings, you’re in insert mode and most characters simply go into the edit buffer at the cursor position. You can do some limited cursor movement and text deletion using the commands listed in Table 7-3.

Table 7-3. Editing Commands Available in vi Insert Mode

Command

Description

CTRL-B

Move cursor back (left) one character

CTRL-F

Move cursor forward (right) one character

CTRL-A

Move cursor to beginning of line

CTRL-E

Move cursor to end of line

DEL or CTRL-H

Delete character to left of cursor

CTRL-W

Delete word backward

CTRL-U

Delete from beginning of line to cursor

CTRL-K

Delete from cursor to end of line

Using vi Command Mode

To make changes that cannot easily be made in insert mode, hit ESC to enter command mode. The commands for moving the cursor, changing text, or deleting text in command mode are more flexible than those available in insert mode. (There is also a u command that is supposed to undo the last change made in command mode; unfortunately, I have not found it to work reliably.)

Cursor motion commands

Table 7-4 lists some of the cursor motion commands that you can use in command mode. The word motion commands w, b, and e stop at whitespace or punctuation, whereas W, B, and E stop only at whitespace. Consequently, you can produce larger cursor motions with the uppercase commands, whereas the lowercase commands are useful for finer motions such as bouncing along successive components of pathnames.

0 is similar to ^ and CTRL-A, but 0 moves to the first column of the command, whereas ^ and CTRL-A move to the first non-whitespace character.

Table 7-4. Cursor Positioning Commands (vi Command Mode)

Command

Description

h or CTRL-H

Move cursor back (left) one character

l or SPACE

Move cursor forward (right) one character

w

Move cursor forward (right) one word

b

Move cursor back (left) one word

e

Move cursor to next word ending

W, B, E

Like w, b, and e, but different word definition

^ or CTRL-A

Move cursor to beginning of line (first non-whitespace character)

0

Move cursor to beginning of line

$ or CTRL-E

Move cursor to end of line

Try the vi command mode motion commands by entering the following line, leaving the cursor at the end of the line:

% echo this is a command

Hit ESC to enter command mode, then type the following commands. You should see the cursor move as shown below:

% echo this is a comman        Type h
% echo this is a ommand        Type b
% cho this is a command        Type ^ or CTRL-A
% eho this is a command        Type SPACE or 1
% echo his is a command        Type w
% echo this is a command       Type $ or CTRL-E

Repeating commands

Motion commands can be repeated in vi command mode by preceding the command with a number. For instance, 3b moves backward three words and 15 SPACE moves forward 15 characters. 0 is not a valid repeat count because 0 is itself a command (move the cursor to the start of the line).

Many of the vi command mode modification commands, described below, can take a repeat count. The command descriptions indicate which can and which can’t.

Adding text

To add text when you’re in command mode, use one of the commands in Table 7-5 to enter insert mode, then start typing. New text is added to the command buffer until you hit ESC to return to command mode.

Repeat counts do not work for the commands listed in Table 7-5.

Table 7-5. Text Insertion Commands (vi Command Mode)

Command

Description

a

Append new text after cursor until ESC

i

Insert new text before cursor until ESC

A

Append new text after end of line until ESC

I

Insert new text before beginning of line until ESC

Deleting text

The commands shown in Table 7-6 delete text from the edit buffer. x and X delete single characters. The dm command is useful for deleting units of text—it deletes from the cursor to wherever the motion command m would place the cursor. For instance, dw and db delete a word forward or backward.

Command mode deletion commands can take repeat counts, e.g., 3dw (or d3w) deletes three words and 5x deletes five characters.

Table 7-6. Text Deletion Commands (vi Command Mode)

Command

Description

x

Delete character under cursor

X or DEL

Delete character to left of cursor

dm

Delete from cursor to end of motion command m

D

Synonym for d$

CTRL-W

Delete word backward

CTRL-U

Delete from beginning of line to cursor

CTRL-K

Delete from cursor to end of line

Replacing text

To replace part of the edit buffer, you can delete the old text, then insert the new text. However, vi command mode offers a set of text replacement commands that combine the delete and insert operations (see Table 7-7). The cm command acts like dm, but also puts you in insert mode. For instance, to change a word, type cw, then type the new word and hit ESC. To replace the character under the cursor, type r, then the new character. R puts you in replace mode, which replaces characters as you type, until you hit ESC. The s command substitutes characters that you type for the character under the cursor, until you hit ESC.

The c and s commands can be given a repeat count. For instance, 4cw (or c4w) changes four words, and 6s substitutes six characters beginning with the one under the cursor, using the text you type following the command.

Table 7-7. Text Replacement Commands (vi Command Mode)

Command

Description

cm

Change characters from cursor to end of motion command m until ESC

C

Synonym for c$

rc

Replace character under cursor with character c

R

Replace multiple characters until ESC

S

Substitute character under cursor with characters typed until ESC

To see how vi command mode editing works, try the following:

% echo My typing skilks are not amazing. Type a simple command line
% echo My typing skilks are notmazing.  Hit ESC to enter command mode, then type b
% echo My typing skilks aremazing.     Type db
% echo My typing skils are amazing.     Type h several times
% echo My typing skils are amazing.     Type rl

vi History-Searching Commands

In vi command mode, you can use ? and / to search backward and forward through your history list. For instance, when you type ?, tcsh prints ? as a prompt to solicit the string you want to search for. Type a string and hit RETURN. The string can contain filename pattern characters. The editor searches backward for a line that matches the string and retrieves it into the edit buffer, with the cursor at the end of the line. If the command isn’t the one you want, type n (next match) to repeat the search in the same direction. N repeats the search in the opposite direction. You can use n and N together to bounce back and forth among the matching commands.

When you find the command you want, just start editing it as usual (note that you’ll still be in command mode after a search). Or, you can hit RETURN to execute it.

vi Character-Seeking Commands

vi command mode provides a set of commands that search for characters in the current command line (see Table 7-8). You can use these as motion commands to move the cursor to a particular character, or combine them with the d or c commands to delete or change text from the cursor position to that character.

Table 7-8. Character-Seeking Motion Commands (vi Command Mode)

Command

Description

fc

Move cursor to next instance of c in line

Fc

Move cursor to previous instance of c in line

tc

Move cursor up to next instance of c in line

Tc

Move cursor back to previous instance of c in line

;

Repeat previous f or F command

,

Repeat previous f or F command in opposite direction

fc moves the cursor forward to the next instance of character c in the command. Fc moves the cursor backward:

% echo abcdefghi     Type a command, leaving cursor at end
% ech abcdefghi      Type ESC to enter command mode, then Fo to move back to o
% echo abcdefhi      Type fg to move forward to g

The ; command repeats the last f or F command. The , command is similar but moves the cursor in the opposite direction. That is, the , command repeats f as F and F as f. This feature is useful if you use multiple ; commands to search for successive instances of a character, and go too far:

% echo three games of tic tac toe    Type a command, leaving cursor at end
% echo three games of tic tac oe     Hit ESC to enter command mode, then type Ft
% echo three games of tic ac toe     Type ; to repeat search
% echo three games of ic tac toe     Type ; to repeat search
% echo hree games of tic tac toe     Type ; to repeat search
% echo three games of ic tac toe     Oops, too far; type, to reverse search

tc and Tc are similar to fc and Fc, but the former move the cursor up to or back to the c, i.e., just to the left of c for t and just to the right of c for T.

You can precede the character-seeking commands with d or c to delete or change text. For example, dt/ deletes up to the next slash (handy for clobbering parts of pathnames) and 3c SPACE changes text up through the third space after the cursor.

Using the Arrow Keys in vi Mode

Up arrow and down arrow are like CTRL-P and CTRL-N, just as in emacs mode. Left arrow and right arrow move the cursor left and right one character. However, there is a quirk you should look out for if you’re in insert mode. On many terminals, arrow keys actually send out strings of characters that begin with ESC. Although the arrow keys move the cursor, they’ll also switch you into command mode. If you find this distracting, avoid left arrow and right arrow in insert mode, and stick with CTRL-B and CTRL-F.

Examining and Modifying Key Bindings

tcsh lets you display and change your key bindings using the bindkey command. The various forms of bindkey are summarized in Table 7-9, and discussed below.

Table 7-9. Forms of the bindkey Command

Command

Description

bindkey -e

Select emacs bindings

bindkey -v

Select vi bindings

bindkey -d

Restore default bindings

bindkey -u

Display bindkey usage message

bindkey -l

List editing commands and their meanings

bindkey

List all key bindings

bindkey key

List binding for key

bindkey key cmd

Bind key to editing command cmd

bindkey -c key cmd

Bind key to UNIX command cmd

bindkey -s key str

Bind key to string str

bindkey -r key

Remove binding for key

The forms of bindkey that take a key argument also allow the following flags:

–k

Allows key to be up, down, left, or right, to indicate an arrow key.

–b

Allows key to be C-X or M-X, to indicate CTRL-X or META-X.

–a

Allows you to specify the alternate key map (the map used for vi command mode).

––

May be used immediately preceding key to tell bindkey to stop processing arguments as flags. This flag is useful when key itself begins with a dash.

–k and -b cannot both be used in the same command.

Selecting a Set of Bindings

To select a set of bindings en masse, use the -e or -v option:

% bindkey -e                     Select emacs bindings
% bindkey -v                     Select vi bindings

Selecting either set rebinds every key to the default for that set, undoing any prior bindings you may have established. To bind any individual keys specially (as discussed under Changing Key Bindings, below), you should do so only after executing bindkey -e or bindkey -v.

bindkey -d restores all bindings to whatever the default set is at your site. This isn’t really useful, unless you’re using whatever set that happens to be. You might as well use bindkey -e or bindkey -v to explicitly select a particular set.

Getting a List of Editing Commands

To see the full list of editing command names and their meanings, use the following command:

% bindkey -l
backward-char
    Move back a character
backward-delete-char
    Delete the character behind cursor
backward-delete-word
    Cut from beginning of current word to cursor - saved in cut buffer
    .
    .
    .

bindkey -l is useful for determining which editing commands can be bound to keys, or what a given command does. The descriptions displayed are quite concise, however. Appendix C, Other Sources of Information, lists a document that provides more detailed explanations of the editing commands.

Displaying Key Bindings

bindkey can display all key bindings currently in effect, or the bindings for individual keys.

Displaying all bindings

bindkey, with no arguments, lists all current key bindings:

% bindkey
Standard key bindings
"^@"           ->  set-mark-command
"^A"           ->  beginning-of-line
"^B"           ->  backward-char
"^C"           ->  tty-sigintr
"^D"           ->  delete-char-or-list-or-eof
.
.
.

To make the output more manageable, use grep. For example, to find out which keys are bound to commands that access the history list, type the following command:

% bindkey | grep history

bindkey reports keystrokes using the notation shown in Table 7-10. Characters that have bit 8 turned on (usually typed using the META key, if you have one) are reported using nnn notation.

Table 7-10. bindkey Keystroke Notation

Sequence

Character Represented by Sequence

^X

CTRL-X

^[

ESC

^?

DEL

nnn

ASCII character with octal code nnn

The key binding list displayed by bindkey is divided into four sections:

Standard key bindings

Commands bound to single keystrokes in the standard key map. For the vi bindings, these are the commands that are active while you are in insert mode.

Alternative key bindings

Bindings for the alternative key map. These are single-character bindings that are active during vi command mode. This section is empty if you use emacs bindings because there is no “command mode,” and thus no alternative key map.

Multicharacter key bindings

Commands bound to multiple-keystroke sequences.

Arrow key bindings

Commands bound to the arrow keys.

Displaying individual key bindings

To display a single binding, name the key or key sequence in which you’re interested:

% bindkey ^A                    Show binding for CTRL-A
"^A"   ->    beginning-of-line
% bindkey ^L                    Show binding for CTRL-L
"^L"   ->    clear-screen

If you name a single character key argument, bindkey displays the binding from the standard key map. For the vi bindings, this display corresponds to the key’s meaning in insert mode. To see what the key means in vi command mode, use the -a option to display the binding from the alternative key map:

% bindkey !                      Show binding from standard key map
"!"     ->      self-insert-char
% bindkey -a !                Show binding from alternative key map
"!"     ->      expand-history

Specifying the key Argument

The key argument to bindkey can represent a single character or a string of characters. key can include special characters, and some of bindkey’s options modify the key argument’s interpretation.

The following methods allow you to include special characters in key:

  • Use caret notation to denote control characters, e.g., ^A or ^a for CTRL-A. Also, ^[ means ESC, and ^? means DEL.

  • Precede a special character with CTRL-V to turn off its special meaning.

  • Put key in quotes.

  • Use one of the backslash sequences shown in Table 7-11. Putting a backslash in front of a character that’s not listed in the table turns off any special meaning the character might have.

Table 7-11. bindkey Special Character Sequences

Sequence

Character Represented by Sequence

^X

CTRL-X

^[

ESC

^?

DEL

^

^

a

CTRL-G (bell)



CTRL-H

e

ESC

f

FORMFEED

NEWLINE

RETURN

TAB

v

CTRL-K (vertical tab)

nn

ASCII character with octal code nnn

For example, to check the binding for CTRL-C, you can type any of these commands:

% bindkey ^C
% bindkey CTRL-VCTRL-C
% bindkey "03"
% bindkey \003

In general, it’s a good idea to put quotes around a key argument that contains backslash sequences or characters that the shell interprets specially, like | or ; or (. Note that you type a backslash only once if it’s used within quotes, but twice if it’s not in quotes.

Options that modify interpretation of the key argument

To specify an arrow key, use the -k option and left, right, up, or down as the key name, e.g.:

% bindkey -k left                Display binding for left arrow key

The -b option allows you to use C-X or M-X notation to specify control or META characters:

% bindkey -b C-A                Show binding for CTRL-A
% bindkey -b M-A                Show binding for META-A

If you use an arrow key name or C-X/M-X but forget the -k or -b option, the key argument is interpreted as a multicharacter sequence, which won’t give you the results you’re looking for:

% bindkey left
Unbound extended key "left"
% bindkey C-A
Unbound extended key "C-A"

The tcsh manual page says that -b allows F-n notation to be used for specifying function keys. Unfortunately, this feature doesn’t work.

Changing Key Bindings

You can customize your bindings by modifying bindings for individual keys. bindkey allows you to do the following:

  • Assign a new binding

  • Remove a binding

  • Bind a key to a UNIX command

  • Bind a key to a literal string

You can experiment with your bindings by changing them at the command line, using the bindkey commands described below. If you decide to make a given binding part of your environment, put the appropriate command in ˜/.cshrc.

Binding an individual key

To associate an editing command with a key, bind the command to the key:

% bindkey key command

key is indicated as described previously, under Specifying the key Argument. command is any of the editing command names listed by bindkey -l.

If you edit in vi mode, individual key bindings are installed by default in the standard key map, which applies to vi insert mode. To establish a binding for use in vi command mode, use the -a option to install the binding in the alternative key map. For instance, CTRL-B and CTRL-F are normally unbound in vi command mode. To give these keys the same meaning in command mode that they have in insert mode, use the following commands:

% bindkey -a ^B backward-char
% bindkey -a ^F forward-char

-a can be used with -b or -k, or with the -c, -d, -r, and -s options, described below.

Binding a key to itself

To bind a key to itself, bind it to self-insert-command:

% bindkey key self-insert-command

In effect, this binding turns off any special meaning the key has for editing, so that when you type the key it appears as itself in the command line. Most alphanumeric keys are bound to self-insert-command in emacs mode and vi insert mode.

Removing a binding

To unbind a key, remove the binding:

% bindkey -r key

Removing a binding is different than binding the key to itself. A self-bound key appears as itself in the command line when you type it, whereas a key that’s been unbound becomes dead, i.e., when you type it, nothing happens.

Binding a key to a shell command

bindkey -c lets you bind a key to UNIX commands. For example, if you’d like CTRL-X l to run ls -l more, use the following binding:

% bindkey -c ^Xl   'ls -l|more'

When you type a key or key sequence that’s bound to a UNIX command, the current command line remains intact. That is, the UNIX command is executed immediately, and when it has finished, the command line is redisplayed as it was before you typed the key sequence.

It’s not necessary to include a newline at the end of the command string. You can include special characters in the command, just as you can include special characters in key, as described above in Specifying the key Argument. However, caret sequences inside quotes are interpreted literally in this usage.

Binding a key to a literal string

bindkey -s lets you bind a key to a literal string:

% bindkey -s key string

Typing key becomes equivalent to typing string. You can include special characters in string as you do in key (see Specifying the key Argument), except that caret sequences inside quotes are interpreted literally.

If string contains other editing commands, they are interpreted as such when you use key in a command line.

Binding a key to a string can be used to assign pieces of UNIX commands to keys. Suppose you often search for processes using this command:

% ps ax |  grep string

You could assign the first part of the command to a key sequence, e.g., CTRL-X p:

% bindkey -s ^Xp 'ps ax |  grep '

Then, to search for, say, emacs processes, you could type the following:

% CTRL-Xpemacs

Conflicts Between Terminal Settings and Key Bindings

If a key doesn’t do what you expect, you may have a conflict between the way the terminal driver and the command editor are interpreting keystrokes. For example, CTRL-W is normally interpreted as the werase (word erase) character by the terminal driver. But CTRL-W doesn’t perform the word erase function if you use the emacs bindings, because CTRL-W is bound to the editor’s kill-region command. If you suspect a conflict, you can change the terminal driver setting using stty (see Chapter 5, Setting Up Your Terminal), or change the command editor key binding using bindkey.

Conflicts Between Key Bindings

If you specify two multicharacter bindings that begin with the same prefix, you will not be able to use both bindings. Instead, the second binding replaces the first:

% bindkey -s ^Xl ls
% bindkey -s ^Xlm 'ls | more'


[10] The up arrow and down arrow keys do the same thing as CTRL-P and CTRL-N.

[11] tcsh internalizes the ever-raging emacs vs. vi conflict and resolves it by accommodating users of both editors.

[12] The emacs bindings are usually (but not necessarily) the default. Try this command:

% echo $version

If you see the word vi in the result, tcsh uses the vi bindings by default at your site.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.184.90