< Day Day Up > |
Both shells let you display or modify previous commands. Commands in the history list can be modified using:
Bash also supports a command history mechanism very similar to that of the C shell. Because the interactive line-editing features are considerably superior, and because Bash's command history is almost identical to that of the C shell, we have chosen not to cover those features here. See Chapter 5 and the Bash manpage for more information. 4.7.1. Line-Edit ModeLine-edit mode emulates many features of the vi and emacs editors. The history list is treated like a file. When the editor is invoked, you type editing keystrokes to move to the command line you want to execute. You can also change the line before executing it. When you're ready to issue the command, press the ENTER key. In ksh, line-edit mode can be started in several ways. For example, these are equivalent: $ VISUAL=vi $ EDITOR=vi $ set -o vi Overrides value of VISUAL or EDITOR For Bash, you must use either set -o vi or set -o emacs; assignment to the VISUAL or EDITOR variables has no effect. Note that vi starts in input mode; to type a vi command, press the Escape key first. 4.7.1.1. Common editing keystrokes
4.7.2. The fc and hist Commands"fc" stands for either "find command" or "fix command," since it does both jobs. Use fc -l to list history commands and fc -e to edit them. See the fc entry in the later section "Built-in Commands (Bash and Korn Shells)," for more information. In ksh93, the fc command has been renamed hist, and alias fc=hist is predefined. 4.7.2.1. Examples$ history List the last 16 commands $ fc -l 20 30 List commands 20 through 30 $ fc -l -5 List the last 5 commands $ fc -l cat List all commands since the last command beginning with cat $ fc -l 50 List all commands since command 50 $ fc -ln 5 > doit Save command 5 to file doit $ fc -e vi 5 20 Edit commands 5 through 20 using vi $ fc -e emacs Edit previous command using emacs The following only work in the Korn shell, which predefines the r alias: $ r Reexecute previous command $ r cat Reexecute last cat command $ r doc=Doc Substitute, then reexecute last command $ r chap=doc c Reexecute last command that begins with c, but change string chap to doc For both shells, the interactive line-editing is easier to use than fc, since you can move up and down in the saved command history using your favorite editor commands (as long as your favorite editor is either vi or Emacs!). Current versions of both shells also let you use the Up and Down arrow keys to traverse the command history. 4.7.3. Programmable Completion (Bash Only)Bash and the readline library provide completion facilities, whereby you can type part of a command name, hit the TAB key, and have Bash fill in part or all of the rest of the command or filename. Programmable completion lets you, as a shell programmer, write code to customize the list of possible completions that Bash will present for a particular, partially entered word. This is accomplished through the combination of several facilities.
... COMPREPLY=( $( compgen options arguments ) ) ... Compspecs may be associated with either a full pathname for a command, or more commonly, with the an unadorned command name (/usr/bin/man versus plain man). Completions are attempted in the following order, based on the options provided to the complete command.
Ian Macdonald has collected a large set of useful compspecs, often distributed as the file /etc/bash_completion. If your system does not have it, one location for downloading it is http://www.dreamind.de/files/bash-stuff/bash_completion. It is worth retrieving and reviewing. 4.7.3.1. ExamplesRestrict files for the C compiler to C, C++ and assembler source files, and relocatable object files: complete -f -X '!*.[Ccos]' gcc cc For the man command, restrict expansions to things that have manpages: # Simple example of programmable completion for manual pages. # A more elaborate example appears in the bash_completion file. # Assumes man [num] command command syntax. shopt -s extglob Enable extended pattern matching _man ( ) { local dir mandir=/usr/share/man Local variables COMPREPLY=( ) Clear reply list if [[ ${COMP_WORDS[1]} = +([0-9]) ]] Section number provided then # section provided: man 3 foo dir=$mandir/man${COMP_WORDS[COMP_CWORD-1]} Look in that directory else # no section, default to commands dir=$mandir/'man[18]' Look in command directories fi COMPREPLY=( $( find $dir -type f | Generate raw file list sed 's;..*/;;' | Remove leading directories sed 's/\.[0-9].*$//' | Remove trailing suffixes grep "^${COMP_WORDS[$COMP_CWORD]}" | Keep those that match given prefix sort Sort final list ) ) } complete -F _man man Associate function with command |
< Day Day Up > |