Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 16: Interfacing with the System |  | Previous HourNext Hour
Sections in this Hour: |  |

## Hour 16
Interfacing with the System

This hour assumes a working knowledge of your operating system of choice, in which you will be able to leverage your operating system within Emacs's environment. Emacs can interact with other programs in a variety of ways, including

- Invoking Emacs as an editor from other programs

- Executing a program and viewing its output

- Running an interactive shell using Emacs to enhance the shell interface

- Running commands remotely on other machines

- Writing your own specialized system commands

- Editing directories

- Printing to system print queues

This wide range of options continues--Emacs has specialized interfaces--but there is not enough space to cover them all. For convenience in this hour, I will assume you are running on UNIX. UNIX tools are available on Windows NT from different vendors and over the Internet, so examples can be replicated. In many cases, Emacs covers Windows, making it appear as though you are running on a UNIX machine.

Some system commands will not be covered. They include the specialized grep command, found in Hour 8, "Searching for Text in Multiple Files"; compilation and debugging, found in Hour 19, "Programming Utilities"; external spelling checkers, found in Hour 11, "Editing Utilities"; running diffs, found in Hour 14, "Finding Differences Between Files"; or sending email and news, found in Part V, "Mail and News."

## Invocations

Aside from simply invoking Emacs with no parameters, there are several additional details you can use. For example, to edit a file, you can pass the filename to Emacs on the command line as a general argument. Passing the --help argument to Emacs provides you with a list of arguments that are valid. Some useful ones include --no-windows or -nw to run in a terminal, and --no-init-file or -q to suppress the reading in of your ~/.emacs file.

You can even write shell scripts to invoke Emacs into specialized modes. The -f flag causes Emacs to run a specific function, -l causes it to load an additional Lisp file, and --eval permits the execution of arbitrary Lisp code. As a good example of the use of these flags, you can write a shell script to run Ediff on two files like this:

    #!/bin/sh
    emacs -q --eval "(ediff \"$1\" \"$2\" )"

Here, -q prevents loading potentially slow init files, and --eval executes your generated command.

Tip - Different shells have different conventions for quoting characters and using argument substitution. Any such language can invoke Emacs in this way.

Some command-line programs, including mail, need to invoke an editor. They use the EDITOR environment variable. If this is not set, vi is typically used instead. Set the variable to the string emacs to invoke Emacs for your editing needs.

If you do not like the idea of starting a new Emacs every time some program wants to edit something, you are in luck. Emacs can be used as an editing server, allowing you to have a single Emacs invocation provide editing services to other applications. The first thing you want to do is run Emacs. Next, execute the command M-x server-start. The server-start command initializes the interface Emacs listens to while waiting for external editing requests.

Now that your server has been started, you can now move to a terminal window, and execute the program emacsclient. You can do that like this:

    emacsclient ~/.emacs

You immediately see that Emacs brings that file into a buffer. You can edit this file to your heart's content. When you're finished, execute the server-edit command with the keybinding C-x #. When this is executed, Emacs switches to the next client file and the originating emacsclient invocation exits.

To automate this process, you can add the following line to your .emacs file

    (start-server)

and, set your EDITOR environment variable to emacsclient. It might look like this for csh:

    setenv EDITOR emacsclient

Note - Another more complex method of controlling a running Emacs is with a program called gnudoit, which can be found on the Internet and permits the running of arbitrary Lisp expressions.

Sams Teach Yourself Emacs in 24 Hours |  | ContentsIndex
Hour 16: Interfacing with the System |  | Previous HourNext Hour
Sections in this Hour: |  |

© Copyright Macmillan USA. All rights reserved.