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 KeystrokesBash 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
To cut (kill) text and place it in the yank buffer, press
To paste text from the yank buffer, press
To insert text from previous command lines, press
To insert special characters, press
Tip
Expand the Command LineYou 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 CompletionsYou 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
Change readline's Key BindingsSuppose 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'
emacs or viIf 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 BindingsThe 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 |