If these commands sound familiar to you, they should. They are history commands, which are identical to the minibuffer history commands we discussed in Chapter 3. The In/Out menu is devoted to working with command history.
Enter and Tab have special functions in shell mode. Pressing Enter executes the command on the line where the cursor is, even if you move the cursor up to the line of an earlier command you want to execute again. When you press Enter, Emacs copies the command to the end of the buffer and executes it. Of course, you can modify the command before pressing Enter.
Pressing Tab puts the Emacs completion feature into action; use completion for operating system commands, filenames, and variables. Note that the completion of system commands works best on Unix implementations like Linux and Mac OS X; Emacs doesn't seem to find all the possible Windows commands, for example.
If you type a command that produces a lot of output, cluttering up your session, there's an easy way to get rid of it. Type C-c C-o (for comint-kill-output).
Type: C-c C-o
C-c C-o automatically deletes the output from the last command (Mac OS X).
The previous command (ls-la) remains on the screen, but its output, a long list of files, is deleted. C-c C-o can delete output from only the most recent command; it can't delete output from your previous commands.
Another useful command for shell mode is C-c C-r (for comint-show-output). This command is useful if a command produces a lot of output and causes the first few lines of output to scroll off the screen. C-c C-r repositions the window so the first line of output from your last command is at the top of the window. If you want to see the end of the output instead, type C-c C-e (for comint-show-maximum-output); this command moves the last line of the input to the bottom of the window.
When you're writing a book, moving by paragraphs makes sense, but when you're using a shell, moving by output group is more helpful. An output group consists of a command and its output. To move to the previous output group, type C-c C-p. To move to the next output group, type C-c C-n.
An advantage of shell mode is that you can start a command and then edit another buffer while the command runs. The shell buffer doesn't need to be onscreen; just type M-x shell to get the buffer back again.
You can have multiple shell buffers running at once; just use the command M-x rename-uniquely to rename your shell buffer. You can start another shell buffer, and another, and another—as many as you need to juggle all your tasks.
5.1.2.1 Which shell?
Normally, Emacs uses your default shell in shell mode. Under Windows that's cmd.exe (the familiar C:\> prompt or a close relative).[25] But Unix has a wide variety of available shells, including the GNU Project's bash and the zed shell, zsh. Whatever shell you normally use, that's what Emacs starts when you enter shell mode.
How does Emacs know which shell to start? First, it looks at the variable shell-file-name. Then it looks for a Unix environment variable named ESHELL. Finally it looks for an environment variable named SHELL. If you want to run another particular shell (for example, the zed shell) when you're in Emacs, you can add the following command to your .emacs file:
(setq shell-file-name "/bin/zsh")
When Emacs starts an interactive shell, it runs an additional initialization file after your shell's normal startup files. The name of this file is .emacs_shell-name, where shell-name is the name of the shell you want to use in Emacs. It must be located in your home directory. For example, if you use the C shell, you can add Emacs-only startup commands by placing them in the file .emacs_csh. Let's say that when you're in Emacs, you want to change the prompt to emacs:% and you want an environment variable called WITHIN_EDITOR to be set to T. Here's the contents of your .emacs_csh file:
set prompt="emacs:% "
setenv WITHIN_EDITOR T
Within a shell buffer, Emacs also sets the environment variable EMACS to t, and sets your terminal type (the TERM variable) to emacs.
5.1.2.2 Making passwords invisible in shell mode
By default, shell mode displays everything you type and that includes passwords—not a good situation if someone is peering over your shoulder. There is a way around this problem, however. Before you type the password, type M-x send-invisible. Emacs asks for the nonechoed text. When you type a character, Emacs puts an asterisk in the minibuffer. Press Enter and Emacs enters the password without displaying it. To have Emacs hide passwords as you type them, add the following two lines to your .emacs file:
(add-hook 'comint-output-filter-functions
'comint-watch-for-password-prompt)
Emacs asks for nonechoed text in the minibuffer whenever a password prompt appears on the screen, making sure that the password is never displayed. Table 5-1 summarizes shell mode commands.
Table 5-1. Shell mode commands
| Keystrokes | Command name | Action |
|---|---|---|
| (none) | shell | Enter shell mode. |
| C-c C-c Signals → BREAK | comint-interrupt-subjob | Interrupt current job; equivalent to C-c. |
| C-d | comint-delchar-or-maybe-eof | Send EOF character if at end of buffer; delete a character elsewhere. |
| C-c C-d Signals → EOF | comint-send-eof | Send EOF character. |
| C-c C-u | comint-kill-input | Erase current line; equivalent to C-u in Unix shells. |
| C-c C-z Signals → STOP | comint-stop-subjob | Suspend or stop a job; C-z in Unix shells. |
| M-p In/Out → Previous Input | comint-previous-input | Retrieve previous commands (can be repeated to find earlier commands). |
| M-n In/Out → Next Input | comint-next-input | Retrieve subsequent commands (can be repeated to find more recent commands). |
| Enter | comint-send-input | Send input on current line. |
| Tab | comint-dynamic-complete | Complete current command, filename, or variable name. |
| C-c C-o In/Out → Delete Current Output Group | comint-kill-output | Delete output from last command. |
| C-c C-r | comint-show-output | Move first line of output to top of window. |
| C-c C-e In/Out → Show Maximum Output | comint-show-maximum-output | Move last line of output to bottom of window. |
| C-c C-p In/Out → Backward Output Group | comint-previous-prompt | Move to previous command. |
| C-c C-n In/Out → Forward Output Group | comint-next-prompt | Move to next command. |
25
You do have choices under Windows as well, thanks to Cygwin (http://cygwin.com/). For example, if you wanted to run Cygwin's bash, you'll find helpful information on how to set that up on Ngai Kim Hoong's page on that topic at http://www.khngai.com/emacs/cygwin.php.