< Day Day Up > |
13.5. Command-Line Shortcuts13.5.1 Command and Filename CompletionTo save typing, bash implements command and filename completion, a mechanism that allows you to type part of a command or filename, press the Tab key, and the rest of the word will be completed for you. If you type the first letters in a command and press the Tab key, bash will attempt to complete the command and execute it. If bash cannot complete the filename or command, because neither exists, the terminal may beep and the cursor will stay at the end of the command. If there is more than one command starting with those characters and you press the Tab key a second time, all commands that start with those characters will be listed. If there are several files starting with the same letters, bash will complete the shortest name that matches, expand out the filename until the characters differ, and then flash the cursor for you to complete the rest. Example 13.28.1 $ ls file1 file2 foo foobarckle fumble 2 $ ls fu[tab] # Expands filename to fumble 3 $ ls fx[tab] # Terminal beeps, nothing happens 4 $ ls fi[tab] # Expands to file_ (_ is a cursor) 5 $ ls fi[tab][tab] # Lists all possibilities file1 file2 6 $ ls foob[tab] # Expands to foobarckle 7 $ da[tab] # Completes the date command date Tue Feb 24 18:53:40 PST 2004 8 $ ca[tab][tab] # Lists all commands starting with ca cal captoinfo case cat EXPLANATION
13.5.2 HistoryThe history mechanism keeps a history list, a numbered record of the commands that you have typed at the command line. During a login session, the commands you type are stored in the the shell's memory in a history list and then appended to the history file when you exit. You can recall a command from the history list and re-execute it without retyping the command. The history built-in command displays the history list. The default name for the history file is .bash_history , and it is located in your home directory. When bash starts accessing the history file, the HISTSIZE variable specifies how many commands can be copied from the history file into the history list. The default size is 500. The HISTFILE variable specifies the name of the command history file (~/.bash_history is the default) where commands are stored. If unset, the command history is not saved when an interactive shell exits. The history file grows from one login session to the next. The HISTFILESIZE variable controls the maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated when it surpasses that number of lines. The default size is 500. The fc –l command can be used to display or edit commands in the history list.
13.5.3 Accessing Commands from the History FileThe Arrow KeysTo access commands from the history file, you can use the arrow keys on the keyboard to move up and down through the history file, and from left to right (see Table 13.5). You can edit any of the lines from the history file by using the standard keys for deleting, updating, backspacing, and so forth. As soon as you have edited the line, pressing the Enter key will cause the command line to be re-executed.
The history Built-In CommandThe history built-in command displays the history of commands typed preceded by an event number. Example 13.29.1 $ history 982 ls 983 for i in 1 2 3 984 do 985 echo $i 986 done 987 echo $i 988 man xterm 989 adfasdfasdfadfasdfasdfadfasdfasdf 990 id -gn 991 id -un 992 id -u 993 man id 994 more /etc/passwd 995 man ulimit 996 man bash 997 man baswh 998 man bash 999 history 1000 history EXPLANATION
The fc CommandThe fc command, also called the fix command, can be used in two ways: (1) to select commands from the history list, and (2) to edit the commands in either the vi or emacs editor, or for that matter, any editor on your system. In the first form, fc with the –l option can select specific lines or ranges of lines from the history list. When the –l switch is on, the output goes to the screen. For example, fc –l, the default, prints the last 16 lines from the history list, fc –l 10 selects lines numbered 10 through the end of the list, and fc –l –3 selects the last three lines. The –n switch turns off the numbering of commands in the history list. With this option on, you could select a range of commands and redirect them to a file, which in turn could be executed as a shell script. The –r switch reverses the order of the commands. The second form of fc is described in "Command-Line Editing" on page 793. Example 13.30.1 $ fc -l 4 ls 5 history 6 exit 7 history 8 ls 9 pwd 10 clear 11 cal 2000 12 history 13 vi file 14 history 15 ls -l 16 date 17 more file 18 echo a b c d 19 cd 20 history 2 $ fc -l -3 19 cd 20 history 21 fc -l 3 $ fc -ln exit history ls pwd clear cal 2000 history vi file history ls -l date more file echo a b c d cd history fc -l fc -l -3 4 $ fc -ln -3 > saved 5 $ more saved fc -l fc -l -3 fc -ln 6 $ fc -l 15 15 ls -l 16 date 17 more file 18 echo a b c d 19 cd 20 history 21 fc -l 22 fc -l -3 23 fc -ln 24 fc -ln -3 > saved 25 more saved 26 history 7 $ fc -l 15 20 15 ls -l 16 date 17 more file 18 echo a b c d 19 cd 20 history
EXPLANATION
If fc is given the –s option, a string pattern can be used to re-execute a previous command; for example, fc –s rm will cause the most previous line containing the pattern rm to be re-executed. To emulate the Korn shell's redo command, you can create a bash alias called r (e.g., alias r='fc –s') so that if you type r vi at the command line, the last history item containing that pattern will be re-executed; in this case, the vi editor will be started just as it was the last time it started, including any arguments passed. Example 13.31.1 $ history 1 ls 2 pwd 3 clear 4 cal 2000 5 history 6 ls -l 7 date 8 more file 9 echo a b c d 2 $ fc -s da date Thu Jul 15 12:33:25 PST 2004 3 $ alias r="fc -s" 4 $ date +%T 18:12:32 5 $ r d date +%T 18:13:19 EXPLANATION
Re-executing History Commands (Bang! Bang!)To re-execute a command from the history list, the exclamation point (called bang) is used. If you type two exclamation points, (!!) bang, bang, the last command in the history list is re-executed. If you type an exclamation point, followed by a number, the command listed by that number is re-executed. If you type an exclamation point and a letter or string, the last command that started with that letter or string is re-executed. The caret (^) is also used as a shortcut method for editing the previous command. See Table 13.7 for a complete list of history substitution characters. Example 13.32.1 $ date Mon Jul 12 12:27:35 PST 2004 2 $ !! date Mon Jul 12 12:28:25 PST 2004 3 $ !106 date Mon Jul 12 12:29:26 PST 2004 4 $ !d date Mon Jul 12 12:30:09 PST 2004 5 $ dare dare: Command not found. 6 $ ^r^t date Mon Jul 12 12:33:25 PST 2004
EXPLANATION
Example 13.33.1 $ ls file1 file2 file3 file1 file2 file3 $ vi !:1 vi file1 2 $ ls file1 file2 file file1 file2 file3 $ ls !:2 ls file2 file2 3 $ ls file1 file2 file3 $ ls !:3 ls file3 file3 4 $ echo a b c a b c $ echo !$ echo c c 5 $ echo a b c a b c $ echo !^ echo a a 6 % echo a b c a b c % echo !* echo a b c a b c 7 % !!:p echo a b c EXPLANATION
13.5.4 Command-Line EditingThe bash shell provides two built-in editors, emacs and vi, that allow you to interactively edit your history list. When you use the editing features at the command line, whether in vi or emacs mode, the readline functions determine which keys will perform certain functions. For example, if using emacs, Ctrl-P allows you to scroll upward in the command-line history, whereas if using vi, the K key moves upward through the history list. Readline also controls the arrow keys, cursor movement, changing, deleting, inserting text, and redoing or undoing corrections. Another feature of readline is the completion feature previously discussed in "Command and Filename Completion" on page 783. This allows you to type part of a command, filename, or variable, and then, by pressing the Tab key, the rest of the word is completed. There are many more features provided by the Readline library designed to help manipulate text at the command line. The emacs built-in editor is the default built-in editor and is modeless, whereas the vi built-in editor works in two modes, one to execute commands on lines and the other to enter text. If you use UNIX, you are probably familiar with at least one of these editors. To enable the vi editor, add the set command listed below[4] and put this line in your ~/.bashrc file. To set vi, type what's shown in the following example, at either the prompt or in the ~/.bashrc file.
Example 13.34.
set –o vi
EXPLANATION Sets the built-in vi editor for command-line editing of the history list. To switch to the emacs editor, type: Example 13.35.
set –o emacs
EXPLANATION Sets the built-in emacs editor for command-line editing of the history list. The vi Built-In EditorTo edit the history list, go to the command line and press the Esc key. Then press the K key if you want to scroll upward in the history list, and the J key[5] to move downward, just like standard vi motion keys. When you find the command that you want to edit, use the standard keys that you would use in vi for moving left and right, deleting, inserting, and changing text. (See Table 13.8.) After making the edit, press the Enter key. The command will be executed and added to the bottom of the history list.
The emacs Built-In EditorIf using the emacs built-in editor, like vi, start at the command line. To start moving upward through the history file, press ^P. To move down, press ^N. Use emacs editing commands to change or correct text, then press Enter and the command will be re-executed. See Table 13.9.
FCEDIT and Editing CommandsIf the fc command is given the –e option followed by the name of a UNIX/Linux editor, that editor is invoked containing history commands selected from the history list; for example, fc –e vi –1 –3 will invoke the vi editor, create a temporary file in /tmp, with the last three commands from the history list in the vi buffer. The commands can be edited or commented out. (Preceding the command with a # will comment it.) If the user quits the editor, the commands will all be echoed and executed.[6]
If the editor name is not given, the value of the FCEDIT variable is used (typically set in the initialization files, either bash_profile or .profile), and the value of the EDITOR variable is used if FCEDIT is not set. When editing is complete, and you exit the editor, all of the edited commands are echoed and executed. Example 13.36.1 $ FCEDIT=/bin/vi 2 $ pwd 3 $ fc < Starts up the full screen vi editor with the pwd command on line 1> 4 $ history 1 date 2 ls -l 3 echo "hello" 4 pwd 5 $ fc -3 -1 # Start vi, edit, write/quit, and execute # last 3 commands. EXPLANATION
|
< Day Day Up > |