Shell mode also provides a few convenient shortcuts. The command M-pretrieves the last shell command you typed, no matter how far back in the buffer it is. Typing successive M-p's brings back earlier commands.
Type: M-p

M-pretrieves the last command, even if it isn't on the screen (Mac OS X).
In this example, the previous command was more dickensxmas.tex. It's no longer on the screen; its output has pushed it off the top. M-p(for comint-previous-input) retrieves the command, but doesn't execute it; you can edit the command before pressing Enter. To find subsequent commands, type M-n.
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 Chapter 3. Search and Replace The commands we discussed in the first two chapters are enough to get you started, but they're certainly not enough to do any serious editing. If you're using Emacs for anything longer than a few paragraphs, you'll want the support this chapter describes. In this chapter, we cover the various ways that Emacs lets you search for and replace text. Emacs provides the traditional search and replace facilities you would expect in any editor; it also provides several important variants, including incremental searches, regular expression searches, and query-replace. We also cover spell-checking here, because it is a type of replacement (errors are sought and replaced with corrections). Finally, we cover word abbreviation mode; this feature is a type of automatic replacement that can be a real timesaver.
. The In/Outmenu is devoted to working with command history.
Enterand Tabhave special functions in shell mode. Pressing Enterexecutes 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 Tabputs 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-oautomatically 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-ocan 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-rrepositions 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 shellto get the buffer back again.
You can have multiple shell buffers running at once; just use the command M-x rename-uniquelyto rename your shell buffer. You can start another shell buffer, and another, and another—as many as you need to juggle all your tasks.
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 bashand 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_EDITORto 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 EMACSto t, and sets your terminal type (the TERMvariable) 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 Enterand 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-1summarizes 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-uin Unix shells. |
C-c C-z Signals → STOP |
comint-stop-subjob |
Suspend or stop a job; C-zin 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. |
5.2 Using Dired, the Directory Editor
Читать дальше