Understand the Emacs runtime and display options
Emacs has a number of command-line options that affect its behavior. You usually start Emacs with no options at all—maybe a filename argument or two—but the options let you perform a number of advanced operations, including running Emacs Lisp code on files in batch mode. There are many times when it's convenient and helpful to use these options, and they're good for a number of specific situations, which are described in this section. (One particular instance where calling Emacs from the command line with command-line options is useful and popular is in shell scripts or startup files.)
Emacs uses the GNU-style "long" options, which begin with two dashes and take
either a traditional space character or an equals sign between the option and
its argument. These long options are intended to be easy to understand and
remember—the long-style option to start Emacs in inverse video,
for example, is --inverse-video
—but as
a consequence, they're longer to type than options on most other tools and
applications. However, most Emacs options have a traditional single-hyphen
equivalent. A complete list of options, including both styles, is given in
Table 2, later in this tutorial.
Some of the most popular and useful Emacs options are the ones for modifying or specifying files. You can perform a number of handy tricks with these options.
Automatically open certain files
The only argument Emacs takes is a filespec. When you give a file, a list of
files, or another filespec as arguments, Emacs starts and opens the specified
files into individual buffers of their own, just as if you'd specified each of
them in turn with calls to the find-file function,
C-x
C-f:
$ emacs /usr/local/share/newdev/inputs/*.txt
|
This command starts Emacs, opens all the files in the /usr/local/share/newdev/inputs/ directory having a .txt file name extension, and puts them into new buffers of their own. Each buffer's name is identical to the corresponding filename.
If there are certain files that you always edit, such as a task list, day
planner, or to-do file, it makes sense to add them to the
emacs command line that you use in your startup file.
You can have Emacs automatically open them into buffers as soon as it begins by
putting them there:
emacs plans tasks |
Emacs opens files in the order they're given, so the last file you specify is
always the current buffer. In the previous example, the file named tasks is the
current buffer when Emacs starts. If you specify more than one file, Emacs
splits the window in half across the middle, showing one file's buffer on the
top and one on the bottom. Type C-x 1 to delete the
second window so that the entire Emacs window is filled with the one buffer.
If you specify three or more files, Emacs displays the last file in the
top window as the current buffer; it displays a buffer list in the second
window beneath it, showing the names of all the open buffers, their sizes, their
major modes, and the names of the files they correspond to. Again, you type
C-x 1 to remove this window.
You can also use the long --visit and
--file options to precede the filespec. The command
lines in Listing 1, for example, are equivalent.
Listing 1. Equivalent command lines for specifying Emacs filenames
emacs myfile
emacs --visit myfile
emacs --visit=myfile
emacs --file myfile
emacs --file=myfile
|
These long options are sometimes useful when you're constructing long Emacs command lines for batch operations (see the Perform batch operations section).
If a file you give doesn't exist on the file system, Emacs opens a new buffer
and indicates on the mode line that it's a new file. For example, if the current
directory is an empty directory named /usr/local/src/projx/inputs/ and you type
emacs
README, you open a new buffer named README. If
you save the buffer to disk, you write a file named
/usr/local/src/projx/inputs/README.
Try making a new file in your home directory:
$ emacs myfile
|
Notice how Emacs tells you (in the minibuffer) that myfile is a new file.
Type some text in the buffer:
Enough! or Too much! |
Now, save the buffer to disk: Type C-x C-s C-x
C-c
to save the buffer to your new file and then exit Emacs. Notice that you don't
have to give a filename, which you normally do if you visit a new file, as
described in previous tutorials—that's because what you specify
as a command-line argument is the name of a file, not a buffer. When the
file doesn't exist, Emacs makes it. (If you exit without saving the file, you're
asked to confirm—and although the file won't exist, an autosave
file with your edits has been written.)
Open files at certain positions
If you precede a filename argument with
+number
, Emacs opens the file and moves point
to the line corresponding to number. This is useful for when you start
Emacs with the purpose of editing a file in a particular position. Say, for
example, you're debugging a C program, myprog.c, and the compiler has warned you
that an error exists on line 315; you can type
emacs +315 myprog.c to open the source file into a
buffer for editing at the exact line where the compiler found the error.
If you've been reading the tutorials in this series and typing the examples given, you should have a sample file named practice.b containing William Blake's "The Tiger;" in case you haven't saved it or your copy's been mangled by the text operations in previous tutorials, a clean copy is available in the Downloads section of this tutorial.
Try opening practice.b with the cursor at the fifteenth line:
$ emacs +15 practice.b
|
(Exit with C-x C-c.)
This option is also good for opening a buffer at the end of a file. To do that, give a number that's very high, relative to the length of the file.
Try opening practice.b with the cursor at the end of the file:
$ emacs +999 practice.b
|
(Exit with C-x C-c.)
You can also specify the horizontal position in the file. If you think of
vertical lines as rows, then horizontal positions across each line are
columns, and they are given by following the line number (row position)
with a colon character (:) and the numeric value for
the column.
Try opening practice.b with the cursor on the twelfth character across (twelfth column) on the third line (row):
$ emacs +3:12 practice.b
|
The cursor should be on the a of immortal when you do this.
(Then, exit with C-x C-c.)
If the specified line has fewer columns than you specify, the cursor doesn't wrap to the next line; it stays at the end of the line.
If you specify multiple files, you can precede each file with its own option for the row and column at which to place the cursor. The default behavior for each file is to open it into a buffer with point at the first row and first column—this is the equivalent of preceding every filename with +1:1.
Use the --insert option to insert the
contents of the file you give as an argument. Give this option after the
file argument you want to insert to; with no preceding file argument, this
option inserts the given file into a scratch buffer.
Try it:
$ emacs --insert myfile
|
This starts Emacs with a scratch buffer that should look like Figure 1, where your new file, myfile, is inserted and point is at the beginning of the buffer.
Figure 1. Insert a file into an Emacs buffer from the command line.
(Exit with C-x C-c.)
Now, try inserting your new file into practice.b:
$ emacs practice.b --insert myfile
|
This starts Emacs with the practice.b file open in a new buffer and the contents of myfile inserted at the beginning of it.
(Exit with C-x C-c, type
n, and then yes to confirm
an exit without saving.)
You can combine a positional option with an insert, and you can give additional arguments at the same time. Try it:
$ emacs +10:5 practice.b --insert myfile anotherfile
|
This starts Emacs with the contents of practice.b in a new buffer, inserting myfile at the fifth column of the tenth line, and then opens a new file named anotherfile in a new buffer and makes it the current buffer.
(Exit with C-x C-c, type
n, and then yes to confirm
an exit without saving.)
Perhaps second in popularity to the Emacs options for specifying files are the options pertaining to the X Window System. You can change the way Emacs appears in X using these options. Most of these options are standard X Window System options that work on all X clients.
One of the most useful X-related Emacs command-line options is
-nw, which you saw demonstrated in the first tutorial
in this series (see Resources). This option opens
Emacs in the current terminal instead of creating a new X client window of its
own. It's useful in many situations when you're running a remote terminal window
over a slow connection and don't want to start a local X client window, or when
you're doing a number of operations in a terminal and want to do a quick edit to
a file without launching a new window for Emacs to open in.
If you have X running, try it in a terminal window:
$ emacs -nw
|
When you type this, Emacs starts in terminal mode and appears in that terminal
instead of making a new X client window. (Type
C-x C-c to exit.)
Use the --display option to specify where the Emacs
X client window should be displayed. It takes as an argument the hostname of the
X server followed by a colon and the display number, which indicates the number
of the particular X server on that host (it's usually 0, but it differs on
systems running multiple X servers).
For example, this command displays the client on the default server on the local host and is equivalent to not giving any option:
emacs --display :0 |
To open the Emacs window on the default server of a remote host named rs6000, type:
emacs --display rs6000:0 |
The --geometry option is another standard X option.
Give the width, height, and optional X and Y offsets of the client window in
this format:
WIDTHxHEIGHT[{+-}XOFFSET{+-}YOFFSET]
|
The offsets are counted from the top-left corner of the screen and can be either positive or negative values.
If you're running this tutorial in an X server, you can try it now. To open a long window 25 characters wide and 250 characters high in the upper-left corner of your desktop, type:
$ emacs --geometry 25x250+0+0
|
(Exit with C-x C-c.)
Set the Emacs fonts and colors
You can use other standard X options to start Emacs with a specified font, colors for the text (the foreground), and blank space behind it (the background).
To specify a font, give an X font name as a quoted argument to the
--font option. Colors are specified with a number of
options (see Table 1).
Table 1. Emacs command-line options for colors
| Option | Description |
|---|---|
--foreground-color color
-fg color
| Set the foreground color to color. |
--background-color color
-bg color
| Set the background color to color. |
--border-color color
-bd color
| Set the border color to color. |
--cursor-color color
-cr color
| Set the cursor color to color. |
--mouse-style color
-ms color
| Set the mouse pointer color to color. |
You can combine font and color options, too. Try setting them all at once:
$ emacs -fn "-misc-fixed-medium-r-normal-ja-13-120-75-75-c-120-iso10646-1" \
-bg "sea green" -fg "white" -cr "brown1" -ms "royalblue1" practice.b
|
This command starts Emacs in an X client window that looks like Figure 2.
Figure 2. Specifying Emacs colors and fonts
The particular fonts and colors that are available to you depend on your X server and its configuration.
Emacs lets you run a startup file, also called an init file, which can contain Emacs Lisp code for evaluation, and it's normally used to set various properties and variables—you use it to customize your editing environment. It's a hidden file named .emacs, and it's in your home directory. (Customizing your .emacs file is the subject of a future tutorial in this series.)
You can specify that Emacs doesn't run any existing startup file by giving the
-q option. This option is useful for debugging new additions
to your startup file, when you want to run an Emacs session without any
customizations you've made, when you're using a new Emacs function,
when something is behaving oddly, or you're not sure if one of your own
customizations is the culprit:
emacs -q |
Because everyone's .emacs file is different, you might want to try someone
else's startup file for a change of pace and to see how others customize their
editing environment. You can do that with the -u
option, giving the name of the user as an argument to the option. This is useful
for trying out the customizations of a local Emacs wizard, such as (for example)
user joe:
emacs -u joe |
This command starts Emacs with the .emacs startup file in user
joe's home directory, as long as that file is
readable by you. If user joe doesn't exist, doesn't
have a .emacs file, or the file isn't readable, Emacs starts with no init file.
Emacs also has a global startup file, site-start.el, which is often kept in the
/etc/emacs/ directory tree. The --no-site-file option
starts Emacs without evaluating the Emacs Lisp in this file.
Finally, --debug-init enables the Lisp debugger to
check your startup file, which is useful when you're adding things to it and
something isn't working right. You can even use it to debug another user's
startup file:
emacs -u joe --debug-init |
Use options for Emacs Lisp execution
Emacs has command-line options for the execution of Lisp code; these options are particularly useful when you're running Emacs in batch mode, as described below.
Use -l to load a Lisp file; give the name of the
file as an argument. Emacs looks for the file first in the current directory and
second in any of the directories in the EMACSLOADPATH
environment variable, if set. To load a Lisp file from elsewhere on the file
system, give the full path name as the argument.
To run an Emacs Lisp function from the command line, use the
-f option and give the name of the Lisp function to
execute as an option. If the function takes any arguments, be sure to give them.
For example, try starting Emacs and running the
phases-of-moon function:
$ emacs -f phases-of-moon
|
When you do this, your Emacs session should look like
Figure 3, with two windows in the Emacs X client: a scratch
buffer and a window containing dates for the current phases of the moon. (Exit
with C-x C-c.)
Figure 3. Starting Emacs with the phases-of-moon function
To start Emacs and evaluate a Lisp expression, use the
--eval option; follow it with a quoted Lisp
expression.
For example, try starting Emacs with your new file, moving to the end of the
buffer, and evaluating the Lisp code
(insert (current-time-string)), which inserts the
current time and date into the current buffer at point:
$ emacs +999 myfile --eval "(insert (current-time-string))"
|
(Exit with C-x C-c, type
n, and then yes to confirm
an exit without saving.)
You can run Emacs in batch mode for non-interactive processing. When you give
the --batch option, Emacs doesn't write to standard
output, and it takes input from the standard input instead of the minibuffer.
(It still writes to standard error.)
Normally, you'd use either the -l or
-f options, as just described, with
--batch to evaluate Lisp code or run functions
non-interactively. If you call Emacs with --batch and
no other options, Emacs exits immediately.
Otherwise, Emacs performs the given commands until a command causes it to exit
or until it reaches a --kill option, which
immediately exits Emacs—put it at the end of the command line to
exit Emacs when the batch operations are completed.
Try running Emacs in batch mode to insert the contents of myfile into
practice.b at the thirty-fifth line, saving it with the
save-buffer function, and then exiting:
$ emacs --batch +35 practice.b --insert myfile -f save-buffer --kill
|
Now, look at your practice.b file—it should contain an extra line.
You can just as easily take out that line with another Emacs one-liner. Try it:
$ emacs --batch +35 practice.b -f kill-line -f save-buffer --kill
|
Don't forget to look at practice.b and make sure the extra line is gone.
Table 2 summarizes the many Emacs command-line options described so far, giving both their long and short option names (if applicable) and describing their functions.
Table 2. Emacs command-line options
| Option | Description |
|---|---|
--visit=filespec
--file=filespec
filespec
| Open filespec into individual buffers for editing. |
+row[:column]
| Move point to line number row and (optional) horizontal position column in the file (default is +1:1). |
--insert file
| Insert file at the beginning of the buffer. |
--debug-init
| Use the Lisp debugger on the .emacs startup file. |
--no-init-file
-q
| Don't run any .emacs startup file. |
--no-site-file
| Don't run the global site-start.el file. |
-u user
--user user
| Use the .emacs startup file of user. |
--funcall function
-f function
| Execute the Emacs Lisp function. |
--eval expression
--execute expression
| Evaluate the Emacs Lisp expression. |
--load file
-l file
| Execute the Emacs Lisp instructions in file. |
-batch
--batch
| Use batch (non-interactive) mode. |
-kill
--kill
| Exit Emacs when in batch mode. |
--name name
| Use name as the name for the Emacs X client window (default is "emacs"). |
-T title
--title title
| Use title as the title for the Emacs X client window (default is name@FQDN, where FQDN is the host's fully qualified domain name). |
--reverse-video
-r
| Use reverse video, swapping the foreground and background colors. |
--iconic
-iconic
| Start Emacs as an icon instead of an active window. |
--icon-type
-i
| When iconifying the Emacs window, use the Emacs icon (usually qat /usr/share/emacs/version/etc/gnu.xpm) instead of any window manager defaults. |
-fn name
-font name
| Use name for the Emacs window font. |
--border-width width
-bw width
| Set the window border to width pixels. |
--internal-border width
-ib width
| Set the window's internal border to width pixels. |
--g dimensions
--geometry dimensions
| Set the window's width, height, and position according to the given X window dimensions (the default is to make the window 80x40 characters). |
--foreground-color color
-fg color
| Set the foreground color to color. |
--background-color color
-bg color
| Set the background color to color. |
--border-color color
-bd color
| Set the border color to color. |
--cursor-color color
-cr color
| Set the cursor color to color. |
--mouse-color color
-ms color
| Set the mouse-pointer color to color. |
-d name
--display name
| Open the Emacs window on the X display corresponding to name. |
-nw
--no-windows
| In X, don't use an X client window, but open in the current terminal window instead. This option doesn't affect console sessions. |
-t file
--terminal file
| Redirect standard I/O to file instead of terminal. |




