Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 16: Interfacing with the System

Previous HourNext Hour

Sections in this Hour:

 

Running Programs


Emacs can also be used to run commands and maintain the output in its buffers. You have already seen this with the M-x command in Hour 7, "Searching for Text in a Buffer." This technique is also used by Ediff for building its differences in Hour 14. There are several ways you can take advantage of this with some simple commands. The most obvious is the shell command bound to M-!. This prompts you for a command to run that depends on the operating system you are currently using.

On UNIX, the command you enter is executed through sh, the Borne shell, and any conventions allowed by it are permitted. Thus, on UNIX you can run a command and pipe it into another command as you would in a terminal program. The command entered is run synchronously.

Note - Executing a command synchronously means that all processing for Emacs is put on hold until this new subprocess is completed. If you want to keep working in Emacs while a program is running, you will want to run it asynchronously.


When the program exits, the output is then displayed. If there is no output, a message is displayed stating so. If the output is one line long, it is put in the minibuffer. If there is lots of output, that output is displayed in the Shell Command Output buffer. This is quite handy when you need to do a simple task, such as perform command-line operations on a file or other details.

Another handy command is shell-command-on-region, which is bound to M-| (meta pipe). This command takes the current region (the area between point and the last mark) and uses it as the standard input of the command you type at the prompt. Output is handled in the same way as the shell command.

This command can do some nifty stuff. For example, you can uuencode a region and insert that buffer's contents into a mail message. Some other uses include piping a mail message through metamail or pushing command sequences in a text file through an interactive program.

Interactive Shells

Emacs can also run other programs asynchronously. So far you learned the synchronous commands, but you can also execute the command shell asynchronously with M-x shell. A new buffer appears, named Shell, in which all output from your default shell will go. Each additional call of M-x shell brings up a previously created shell when it is available.

Tip - You can create multiple shell buffers by renaming the Shell buffer to something else by using M-x rename-buffer and running M-x shell a second time.


Working with this shell is like working with a shell in a terminal, with one difference. You can navigate anywhere in the Shell buffer, and copy, paste, or use any other Emacs editing command. When you press Enter, the line that point is on is used as the command line and executed. When you are on a prompt, you can also use the key sequences M-n and M-p to navigate through the command history while sitting on a prompt.

This basic behavior is the lowest common denominator to a whole range of programs and is referred to as comint. Comint is a subsystem for running an interactive command line-based asynchronous process. Learning these basics then becomes invaluable for everything from the shell to remote logins to running debuggers.

Some common configuration elements for comint include the following:

In the Shell buffer, there are additional commands that can be used. Emacs performs command-line completion of filenames with the Tab key and tracks your current directory through your use of cd, pushd, and popd so that a find file ( C-x C-f. defaults to the correct location.

Note - When you work with a comint buffer, it is important to remember that the asynchronous subprocess receives no input from Emacs until Enter is used. This is unlike a common shell where the shell receives the input and uses it to display characters.


In addition to running a simple shell under Emacs, you can also run shells remotely with the commands M-x rlogi. and M-x telnet. These commands run rlogin and telnet respectively, and are based on comint. Both these commands behave as the shell does, with the exceptions that the startup requires the name of the machine to run on and that you will have to log in to that machine.

Terminal Emulation (Interactive Shells)

If you need better interaction with your subprocesses under Emacs, such as wanting to run Emacs on a remote machine, you are in luck. The command M-x term starts up a terminal buffer called terminal and offers to run a shell command of your choice. This terminal interprets the sequences used to control a typical terminal, and performs the appropriate display adjustments. This emulation is so good, you can run Emacs in terminal mode inside Emacs.

The greatest disadvantage to this is that you cannot take advantage of many of Emacs's default editing commands. All editing is provided solely from the program being run, no matter how inadequate that might be. Another side effect is that C-c is the only escape character. The important escape commands to remember are

In fact, most C-x commands are replicated on C-c in the terminal. Type C-c C-h to get a list of all the commands bound under C-c.

Tip - Use M-x shell in preference to M-x term unless terminal emulation is absolutely necessary. The advantages are well worth it.


Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 16: Interfacing with the System

Previous HourNext Hour

Sections in this Hour: