Go to the previous, next section.

Entering and Exiting Emacs

The usual way to invoke Emacs is just to type emacs RET at the shell. Emacs clears the screen and then displays an initial advisor message and copyright notice. You can begin typing Emacs commands immediately afterward.

Some operating systems insist on discarding all type-ahead when Emacs starts up; they give Emacs no way to prevent this. Therefore, it is wise to wait until Emacs clears the screen before typing your first editing command.

Before Emacs reads the first command, you have not had a chance to give a command to specify a file to edit. But Emacs must always have a current buffer for editing. In an attempt to do something useful, Emacs presents a buffer named `*scratch*' which is in Lisp Interaction mode; you can use it to type Lisp expressions and evaluate them, or you can ignore that capability and simply doodle. (You can specify a different major mode for this buffer by setting the variable initial-major-mode in your init file. See section The Init File, .emacs.)

It is also possible to specify files to be visited, Lisp files to be loaded, and functions to be called, by giving Emacs arguments in the shell command line. See section Command Line Switches and Arguments.

Exiting Emacs

There are two commands for exiting Emacs because there are two kinds of exiting: suspending Emacs and killing Emacs. Suspending means stopping Emacs temporarily and returning control to its superior (usually the shell), allowing you to resume editing later in the same Emacs job, with the same files, same kill ring, same undo history, and so on. This is the usual way to exit. Killing Emacs means destroying the Emacs job. You can run Emacs again later, but you will get a fresh Emacs; there is no way to resume the same editing session after it has been killed.

C-z
Suspend Emacs (suspend-emacs).
C-x C-c
Kill Emacs (save-buffers-kill-emacs).

To suspend Emacs, type C-z (suspend-emacs). This takes you back to the shell from which you invoked Emacs. You can resume Emacs with the command %emacs if you are using the C shell or the Bourne-Again shell.

On systems that do not permit programs to be suspended, C-z runs an inferior shell that communicates directly with the terminal, and Emacs waits until you exit the subshell. The only way on these systems to get back to the shell from which Emacs was run (to log out, for example) is to kill Emacs. C-d or exit are typical commands to exit a subshell.

To kill Emacs, type C-x C-c (save-buffers-kill-emacs). A two-character key is used for this to make it harder to type. Unless a numeric argument is used, this command first offers to save any modified buffers. If you do not save them all, it asks for reconfirmation with yes before killing Emacs, since any changes not saved before that will be lost forever. Also, if any subprocesses are still running, C-x C-c asks for confirmation about them, since killing Emacs will kill the subprocesses immediately.

In most programs running on Unix, certain characters may instantly suspend or kill the program. (In Berkeley Unix these characters are normally C-z and C-c.) This Unix feature is turned off while you are in Emacs. The meanings of C-z and C-x C-c as keys in Emacs were inspired by the standard Berkeley Unix meanings of C-z and C-c, but that is their only relationship with Unix. You could customize these keys to do anything (see section Keymaps).

Command Line Switches and Arguments

GNU Emacs supports command line arguments to request various actions when invoking Emacs. These are for compatibility with other editors and for sophisticated activities. They are not needed for ordinary editing with Emacs, so new users can skip this section.

You may be used to using command line arguments with other editors to specify which file to edit. That's because many other editors are designed to be started afresh each time you want to edit. You edit one file and then exit the editor. The next time you want to edit either another file or the same one, you must run the editor again. With these editors, it makes sense to use a command line argument to say which file to edit.

The recommended way to use GNU Emacs is to start it only once, just after you log in, and do all your editing in the same Emacs process. Each time you want to edit a different file, you visit it with the existing Emacs, which eventually comes to have many files in it ready for editing. Usually you do not kill the Emacs until you are about to log out.

In the usual style of Emacs use, files are nearly always read by typing commands to an editor that is already running. So command line arguments for specifying a file when the editor is started are seldom used.

Emacs accepts command-line arguments that specify files to visit, functions to call, and other activities and operating modes.

The command arguments are processed in the order they appear in the command argument list; however, certain arguments (the ones in the second table) must be at the front of the list if they are used.

Here are the arguments allowed:

`file'
Visit file using find-file. See section Visiting Files.

`+linenum file'
Visit file using find-file, then go to line number linenum in it.

`-l file'
`-load file'
Load a file file of Lisp code with the function load. See section Libraries of Lisp Code for Emacs.

`-f function'
`-funcall function'
Call Lisp function function with no arguments.

`-i file'
`-insert file'
Insert the contents of file into the current buffer. This is like what M-x insert-buffer does; see section Miscellaneous File Operations.

`-kill'
Exit from Emacs without asking for confirmation.

The following switches are recognized only at the beginning of the command line. If more than one of them appears, they must appear in the order that they appear in this table.

`-t device'
Use device as the device for terminal input and output.

`-d display'
When running with the X window system, use the display named display to make Emacs's X window.

`-nw'
Don't use a window system; display text only, using an ordinary terminal device. Thus, if you run an X-capable Emacs in an Xterm with `emacs -nw', it displays in the Xterm's own window instead of making its own.

`-batch'
Run Emacs in batch mode, which means that the text being edited is not displayed and the standard Unix interrupt characters such as C-z and C-c continue to have their normal effect. Emacs in batch mode outputs to stdout only what would normally be printed in the echo area under program control.

Batch mode is used for running programs written in Emacs Lisp from shell scripts, makefiles, and so on. Normally the `-l' switch or `-f' switch will be used as well, to invoke a Lisp program to do the batch processing.

`-batch' implies `-q' (do not load an init file). It also causes Emacs to exit after all command switches have been processed. In addition, auto-saving is not done except in buffers for which it has been explicitly requested.

`-q'
`-no-init-file'
Do not load your Emacs init file `~/.emacs'.

`-u user'
`-user user'
Load user's Emacs init file `~user/.emacs' instead of your own.

With X Windows, you can use these additional options to specify how to display the window. Each option has a corresponding resource name (used with `emacs' unless you specify another name with `-rn name'), listed with the option, which lets you specify the same parameter using the usual X Windows defaulting mechanism. The corresponding generic resource name (used with `Emacs') is usually made by capitalizing the first letter of the individual resource name, but in some cases it is a completely different string (which is listed below).

-rn name
Use name instead of `emacs' when looking for X resources.

-font fontname
-fn fontname
Use font fontname.
(Resource `font'.)

-wn name
Name the window name.
(Resource `title'.)

-i
Use a bitmap icon (showing the kitchen sink) rather than a textual icon.
(Resource `bitmapIcon'.)

-in name
Name the icon name. (Resource `iconName'; `Title').

-geometry coords
-w coords
Specify the shape and optionally the position of the Emacs window in the usual X way.
(Resource `geometry'.)

-b width
Specify that the window border is width pixels thick.
(Resource `borderWidth'.)

-ib width
Leave width blank pixels between the border and the window contents.
(Resource `internalBorder'; `BorderWidth'.)

-r
Use reverse video.
(Resource `reverseVideo'.)

-fg color
Use color color for text in the window.
(Resource `foreground'.)

-bg color
Use the color color for the background of the window.
(Resource `background'.)

-bd color
Use color color for the window border.
(Resource `borderColor'.)

-cr color
Specify the color, color, to use for the cursor.
(Resource `cursorColor'; `Foreground'.)

-ms color
Use color color for the mouse cursor.
(Resource `pointerColor'; `Foreground'.)

The init file can get access to the command line argument values as the elements of a list in the variable command-line-args. (The arguments in the second table above will already have been processed and will not be in the list.) The init file can override the normal processing of the other arguments by setting this variable.

One way to use command arguments is to visit many files automatically:

emacs *.c

passes each .c file as a separate argument to Emacs, so that Emacs visits each file (see section Visiting Files).

Here is an advanced example that assumes you have a Lisp program file called `hack-c-program.el' which, when loaded, performs some useful operation on current buffer, expected to be a C program.

emacs -batch foo.c -l hack-c-program -f save-buffer -kill > log

This says to visit `foo.c', load `hack-c-program.el' (which makes changes in the visited file), save `foo.c' (note that save-buffer is the function that C-x C-s is bound to), and then exit to the shell that this command was done with. `-batch' guarantees there will be no problem redirecting output to `log', because Emacs will not assume that it has a display terminal to work with.

Go to the previous, next section.