Project53.Edit Command Lines


Project 53. Edit Command Lines

"What features does the Bash shell offer when I wish to edit a command line?"

This project shows how to edit a command line while typing it from scratch or after recalling it from the command-line history. It discusses key bindings and demonstrates how to change them by using the command bind. The last section covers Tcsh shell equivalents. Project 49 shows the many ways in which you may recall a previously typed command line.

Know the Key Keystrokes

Bash provides extensive command-line-editing capabilities, but we'll consider just a subset of those. Command-line editing, like history and recall, is implemented by the readline library. For full details, refer to the Bash man page, and search for section "READLINE" by typing /^READLINE within the man page.

Here are the most useful editing keystrokes.

For general editing tasks, press

  • Left and right arrow to move the cursor left and right

  • Delete (Backspace) to delete the character behind the cursor

  • Control-d to delete the character ahead of the cursor

  • Control-a to move to the start of the line

  • Control-e to move to the end of the line

To cut (kill) text and place it in the yank buffer, press

  • Escape-Backspace to cut the word behind the cursor

  • Escape-d to cut the word ahead of the cursor

  • Control-k to cut from the cursor to the end of the line

  • Control-u to cut from the cursor to the start of the line

To paste text from the yank buffer, press

  • Control-y to paste (yank) cut text

  • Escape-y after Control-y to cycle through the yank buffer. A cut keystroke adds the cut text to the yank buffer but does not overwrite previous cuts; they are still available for pasting.

To insert text from previous command lines, press

  • Escape Control-y to insert the first argument from the previous command line

  • Escape . (period) to insert the last argument given on the last command line

To insert special characters, press

  • Control-v to insert a character literally. To insert the escape character, for example, press Control-v and then press the Escape key.

Tip

A command recalled from the history can be edited subsequently. After pressing Control-r to recall a command, press Escape to edit it instead of Return to execute it. This also applies to history expansion when the shell option histverify is set (refer to Project 49).


Expand the Command Line

You may want to expand a command line to check that it expands as expected or to edit the command line after expansion. To do so, press Escape followed by Control-e to tell the shell to expand variables and aliases, and then present the new command line. Here's an example. First, define an alias and a shell variable.

$ alias ls='ls -lF' $ d='~/Documents'


Now type a command line that uses the alias and shell variable. Don't press Return, but press Escape followed by Control-e.

$ ls $d<Escape><Control-e>


You'll see the command line expanded to

$ ls -lF ~/Documents


Note: After you have defined d='~/Documents', typing ls $d and pressing Return will not work as expected. Try it. This is because the shell expands the tilde character (~) to the pathname of your home directory and does so before shell variable expansion. As the shell takes only one sweep at the command line, tilde will not be expanded.

Expand Completions

You may expand shell completions directly onto the command line by pressing Escape followed by star (*). Expand the star pattern-matching character by typing

$ ls *<Escape>*


Learn More

"Check Completion" in Project 12 shows other ways in which you can complete wildcard expansion directly onto the current command line.


Change readline's Key Bindings

Suppose you wish to assign a macro to a keystroke or change the default key bindings because you prefer different editing keystrokes. You can do so, and can in fact completely reconfigure the default key bindings, by taking advantage of Bash's built-in command bind.

Display the current key bindings by typing

$ bind -p


In the output that follows, you'll see the current key bindings, shown like this.

"\C-k": kill-line


"\C-k" means Control-k. kill-line is a readline function, and we can see that it is bound to Control-k. The form shown here is suitable for use as an argument to bind. You'll also notice that some lines show "\M-", which means press Escape. The readline functions are documented in the Bash man page; search the page for the section called "READLINE" by typing /^READLINE within the man page.

Let's have a bash at defining our own keystrokes. First, we'll bind a macro to Control-p, being the text ps auxc\r. (The sequence \r represents Return and is a way of pressing Return from within the macro.) Type the following, noting that the entire sequence is enclosed in single quotes.

$ bind '"\C-p": "ps auxc\r"'


Now press Control-p, and you'll see that the command ps auxc is executed.

Next, let's bind the readline function kill-whole-line to Control-k. The default bindings have Control-k and Control-u deleting opposing halves of the command line. After you type the following command, Control-k will delete the whole line, even when the cursor is placed midline.

Let's just check that kill-whole-line is not already bound.

$ bind -p | grep kill-whole # kill-whole-line (not bound)


Now let's bind it to Control-k.

$ bind '"\C-k": kill-whole-line'


The readline Library

Bash uses the readline library to receive keyboard input, and readline manages key bindings on behalf of Bash. To configure key bindings permanently, we would add settings to the readline configuration file, which is .inputrc in your home directory (~/.inputrc). Type bind -p to list the current bindings in a form that can be written to .inputrc.

These binding examples generally can be written to the configuration file in the same form shown here. Thus, we, we set the kill-whole-line binding.

$ cat ~/.inputrc "\C-k": kill-whole-line



emacs or vi

If you are familiar with the emacs text editor (covered in Chapter 4), you'll recognize that Bash editing keystrokes mirror those used by emacs. If you are a user of the vi text editor, you might hanker after vi-style editing. If so, all you need do is type

$ set -o vi


To switch back to emacs, type

$ set -o emacs


Tcsh Shell Bindings

The Tcsh shell offers equivalent functionality to Bash's bind via its bindkey command.

To list all editing functions, type

% bindkey


To assign a macro to Control-p, type

% bindkey -c ^P "ps auxc"


Option -c specifies a command and issues the macro followed by Return. If you do not want an automatic Return, use option -s (string) instead.

% bindkey -s ^P "ps auxc"


To make Control-k kill the whole line, type

% bindkey ^K kill-whole-line


To switch command-line binding to a more vi-like style, type

% bindkey -v


To switch back to emacs style, type

% bindkey -e





Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net