Chapter 5. Emacs as a Work Environment
Many of the everyday things you do from a command prompt can be done from within Emacs. You can execute commands, work with directories, and print files—all without leaving Emacs. Changing tasks is as simple as jumping between buffers.
What's important about this? Of course, it's nice to be able to move between tasks easily. What's even more important is that you have the same editing environment no matter what you're doing: you can use all of the Emacs editing commands to work on a file, give shell commands, then start up Dired, the directory editor, to do some file maintenance. It is simple to move text from one window to another. You can execute a command and then use Emacs commands to cut and paste the results into a file. If you're trying to compile a program and keep getting error messages, you can save the interactive session as a file and confer with someone about the problem. Despite the many advantages of modern window systems, Emacs often provides the best way to integrate the many kinds of work you do daily.
Much of the information in this chapter involves integration between Emacs and the operating system. Emacs is most commonly a Unix editor, so forgive us for a bias in that direction. But we are happy to report that for users of GNU Emacs on other platforms, integration with the operating system is still available; you can use shell mode to run commands and can edit directories with Dired. There's no reason to leave Emacs no matter what your platform is.
5.1 Executing Commands in Shell Buffers
One of the most important features of Emacs is its ability to run a command shell in a buffer. Once you have started a shell buffer, you can do all of your normal command-line work within Emacs. What does this buy you?
• You don't have to leave Emacs to get a command prompt. If you want to print or compile a file that you're editing, you can do it immediately.
• You can use Emacs editing features to write your commands.
• You can use Emacs editing features to "back up" through your command list, copy an old command, modify it, and execute it again.
• You can save your shell buffer, keeping a transcript of your editing session—which automatically includes the output from every command that you ran. For debugging or remembering commands you run infrequently, this can be invaluable.
• You can copy output from commands into a file or into another command.
• You can save complex commands in a file and insert the file at the prompt, rather than retyping the command.
As you get used to working within Emacs, you will undoubtedly discover more and more ways to put shell mode to use.
In this section, we discuss shell mode. Later in this chapter, we discuss directory editing, printing, and calendar and diary features for doing simple time management in Emacs. Right now, we'll start with a simple variation on shell mode, a feature that lets you execute commands one at a time.
5.1.1 Running One Command at a Time
To run a command while you're in an Emacs session, type M-!. Emacs asks for the command you want to run. Type the command and press Enter. Emacs then opens a window called *Shell Command Output*
where it displays the results of your command.
Type: M-!

Emacs prompts you for a command to execute.
Type: diff joyce joyce2

Emacs executes the diffcommand and puts the output into a *Shell Command Output*
buffer.
Because the output from the diffcommand is in a buffer, you can edit it, save it, or do anything else you would like with it. Of course, if the operating system has no diffcommand or cannot access it for some reason, this command fails.
An interesting twist to the shell command facility is that you can use a region of a buffer rather than a traditional file as input to the command. For example, let's say we want to sort a phone list. First, we put the cursor somewhere in the list (say, on the first character of Liam
), then we give the mark-paragraphcommand ( M-h). This command defines the phone list as a region, with the cursor at the beginning of the paragraph and the mark at the end.
In the following example, the shaded area shows the extent of the region we want to sort. After selecting a region, we press M-|(for shell-command-on-region); Emacs prompts for the shell command to run.
Type: M-h M-|

Emacs prompts you for a command to execute (Windows).
Now we give the command sortwithout specifying any input file. Emacs is taking care of the input for us.
Type: sort Enter

Emacs runs a sort on the region (Windows).
Emacs has sorted the phone list (i.e., everything within the region).
A useful variation for M-!puts the output directly into the current buffer, rather than into a *Shell Command Output*
buffer. To do so, precede the command with C-u: for example, C-u M-!runs a shell command and puts the output in the current buffer.
Type: C-u M-! ls -la Enter

Emacs runs lsand inserts the result at your current location (Mac OS X).
Now we're ready to discuss shell mode, the interactive facility for running commands. To start a shell buffer, type M-x shell Enter. This creates a buffer named *shell*
. You see the prompt for your shell within this buffer. (This defaults to your usual shell; you can substitute another shell to use in Emacs. See "Which shell?" later in this chapter.)
Figure 5-1. Shell buffers for Linux, Mac OS X, and Windows
For the most part, shell mode is exactly like the normal command interface, except that you can use Emacs to edit the commands as you type them. You can copy commands from one place to another, copy the results into a file, save the whole shell buffer to a file, and so on. Note in Figure 5-1that Emacs has added a few items to the menu bar (Complete, In/Out, and Signals).
A few tricks are worth knowing, though. For example, you normally interrupt a command by typing C-c. If you type C-cin shell mode, Emacs thinks that the C-cis part of a command meant for it, because many Emacs commands start with C-c. Therefore, you have to type C-c C-cto terminate the current job. Likewise, under Unix, you type C-c C-zto stop a job, instead of C-z, and C-c C-dinstead of C-d, and so on. ( C-c C-dis not strictly necessary because Emacs understands C-din context. If you're at the end of the buffer, C-dmeans "end of file"; if you're anywhere else, it deletes a character.) Alternatively, you can select options from the Signalsmenu rather than using control characters, if desired (for example, selecting EOFinstead of typing C-d).
Читать дальше