1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997, 2000, 2001,
3 @c 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
6 @chapter Miscellaneous Commands
8 This chapter contains several brief topics that do not fit anywhere
9 else: reading netnews, running shell commands and shell subprocesses,
10 using a single shared Emacs for utilities that expect to run an editor
11 as a subprocess, printing hardcopy, sorting text, narrowing display to
12 part of the buffer, editing double-column files and binary files,
13 saving an Emacs session for later resumption, following hyperlinks,
14 browsing images, emulating other editors, and various diversions and
23 @node Gnus, Shell, Calendar/Diary, Top
26 @cindex reading netnews
28 Gnus is an Emacs package primarily designed for reading and posting
29 Usenet news. It can also be used to read and respond to messages from a
30 number of other sources---mail, remote directories, digests, and so on.
32 Here we introduce Gnus and describe several basic features.
34 For full details, see @ref{Top, Gnus,, gnus, The Gnus Manual}.
37 For full details on Gnus, type @kbd{M-x info} and then select the Gnus
42 To start Gnus, type @kbd{M-x gnus @key{RET}}.
45 * Buffers of Gnus:: The group, summary, and article buffers.
46 * Gnus Startup:: What you should know about starting Gnus.
47 * Summary of Gnus:: A short description of the basic Gnus commands.
51 @subsection Gnus Buffers
53 Unlike most Emacs packages, Gnus uses several buffers to display
54 information and to receive commands. The three Gnus buffers users use
55 most are the @dfn{group buffer}, the @dfn{summary buffer} and the
58 The @dfn{group buffer} contains a list of newsgroups. This is the
59 first buffer Gnus displays when it starts up. It normally displays
60 only the groups to which you subscribe and that contain unread
61 articles. Use this buffer to select a specific group.
63 The @dfn{summary buffer} lists one line for each article in a single
64 group. By default, the author, the subject and the line number are
65 displayed for each article, but this is customizable, like most aspects
66 of Gnus display. The summary buffer is created when you select a group
67 in the group buffer, and is killed when you exit the group. Use this
68 buffer to select an article.
70 The @dfn{article buffer} displays the article. In normal Gnus usage,
71 you see this buffer but you don't select it---all useful
72 article-oriented commands work in the summary buffer. But you can
73 select the article buffer, and execute all Gnus commands from that
74 buffer, if you want to.
77 @subsection When Gnus Starts Up
79 At startup, Gnus reads your @file{.newsrc} news initialization file
80 and attempts to communicate with the local news server, which is a
81 repository of news articles. The news server need not be the same
82 computer you are logged in on.
84 If you start Gnus and connect to the server, but do not see any
85 newsgroups listed in the group buffer, type @kbd{L} or @kbd{A k} to get
86 a listing of all the groups. Then type @kbd{u} to toggle
87 subscription to groups.
89 The first time you start Gnus, Gnus subscribes you to a few selected
90 groups. All other groups start out as @dfn{killed groups} for you; you
91 can list them with @kbd{A k}. All new groups that subsequently come to
92 exist at the news server become @dfn{zombie groups} for you; type @kbd{A
93 z} to list them. You can subscribe to a group shown in these lists
94 using the @kbd{u} command.
96 When you quit Gnus with @kbd{q}, it automatically records in your
97 @file{.newsrc} and @file{.newsrc.eld} initialization files the
98 subscribed or unsubscribed status of all groups. You should normally
99 not edit these files manually, but you may if you know how.
101 @node Summary of Gnus
102 @subsection Summary of Gnus Commands
104 Reading news is a two-step process:
108 Choose a group in the group buffer.
111 Select articles from the summary buffer. Each article selected is
112 displayed in the article buffer in a large window, below the summary
113 buffer in its small window.
116 Each Gnus buffer has its own special commands; the meanings of any
117 given key in the various Gnus buffers are usually analogous, even if
118 not identical. Here are commands for the group and summary buffers:
121 @kindex q @r{(Gnus Group mode)}
122 @findex gnus-group-exit
124 In the group buffer, update your @file{.newsrc} initialization file
127 In the summary buffer, exit the current group and return to the
128 group buffer. Thus, typing @kbd{q} twice quits Gnus.
130 @kindex L @r{(Gnus Group mode)}
131 @findex gnus-group-list-all-groups
133 In the group buffer, list all the groups available on your news
134 server (except those you have killed). This may be a long list!
136 @kindex l @r{(Gnus Group mode)}
137 @findex gnus-group-list-groups
139 In the group buffer, list only the groups to which you subscribe and
140 which contain unread articles.
142 @kindex u @r{(Gnus Group mode)}
143 @findex gnus-group-unsubscribe-current-group
144 @cindex subscribe groups
145 @cindex unsubscribe groups
147 In the group buffer, unsubscribe from (or subscribe to) the group listed
148 in the line that point is on. When you quit Gnus by typing @kbd{q},
149 Gnus lists in your @file{.newsrc} file which groups you have subscribed
150 to. The next time you start Gnus, you won't see this group,
151 because Gnus normally displays only subscribed-to groups.
153 @kindex C-k @r{(Gnus)}
154 @findex gnus-group-kill-group
156 In the group buffer, ``kill'' the current line's group---don't
157 even list it in @file{.newsrc} from now on. This affects future
158 Gnus sessions as well as the present session.
160 When you quit Gnus by typing @kbd{q}, Gnus writes information
161 in the file @file{.newsrc} describing all newsgroups except those you
164 @kindex SPC @r{(Gnus)}
165 @findex gnus-group-read-group
167 In the group buffer, select the group on the line under the cursor
168 and display the first unread article in that group.
171 In the summary buffer,
175 Select the article on the line under the cursor if none is selected.
178 Scroll the text of the selected article (if there is one).
181 Select the next unread article if at the end of the current article.
184 Thus, you can move through all the articles by repeatedly typing @key{SPC}.
186 @kindex DEL @r{(Gnus)}
188 In the group buffer, move point to the previous group containing
191 @findex gnus-summary-prev-page
192 In the summary buffer, scroll the text of the article backwards.
195 @findex gnus-group-next-unread-group
196 @findex gnus-summary-next-unread-article
198 Move point to the next unread group, or select the next unread article.
201 @findex gnus-group-prev-unread-group
202 @findex gnus-summary-prev-unread-article
204 Move point to the previous unread group, or select the previous
207 @kindex C-n @r{(Gnus Group mode)}
208 @findex gnus-group-next-group
209 @kindex C-p @r{(Gnus Group mode)}
210 @findex gnus-group-prev-group
211 @kindex C-n @r{(Gnus Summary mode)}
212 @findex gnus-summary-next-subject
213 @kindex C-p @r{(Gnus Summary mode)}
214 @findex gnus-summary-prev-subject
217 Move point to the next or previous item, even if it is marked as read.
218 This does not select the article or group on that line.
220 @kindex s @r{(Gnus Summary mode)}
221 @findex gnus-summary-isearch-article
223 In the summary buffer, do an incremental search of the current text in
224 the article buffer, just as if you switched to the article buffer and
227 @kindex M-s @r{(Gnus Summary mode)}
228 @findex gnus-summary-search-article-forward
229 @item M-s @var{regexp} @key{RET}
230 In the summary buffer, search forward for articles containing a match
237 @subsection Where to Look Further
239 @c Too many references to the name of the manual if done with xref in TeX!
240 Gnus is powerful and customizable. Here are references to a few
246 additional topics in @cite{The Gnus Manual}:
250 Follow discussions on specific topics.@*
251 See section ``Threading.''
254 Read digests. See section ``Document Groups.''
257 Refer to and jump to the parent of the current article.@*
258 See section ``Finding the Parent.''
261 Refer to articles by using Message-IDs included in the messages.@*
262 See section ``Article Keymap.''
265 Save articles. See section ``Saving Articles.''
268 Have Gnus score articles according to various criteria, like author
269 name, subject, or string in the body of the articles.@*
270 See section ``Scoring.''
273 Send an article to a newsgroup.@*
274 See section ``Composing Messages.''
280 Follow discussions on specific topics.@*
281 @xref{Threading, , Reading Based on Conversation Threads,
282 gnus, The Gnus Manual}.
285 Read digests. @xref{Document Groups, , , gnus, The Gnus Manual}.
288 Refer to and jump to the parent of the current article.@*
289 @xref{Finding the Parent, , , gnus, The Gnus Manual}.
292 Refer to articles by using Message-IDs included in the messages.@*
293 @xref{Article Keymap, , , gnus, The Gnus Manual}.
296 Save articles. @xref{Saving Articles, , , gnus, The Gnus Manual}.
299 Have Gnus score articles according to various criteria, like author
300 name, subject, or string in the body of the articles.@*
301 @xref{Scoring, , , gnus, The Gnus Manual}.
304 Send an article to a newsgroup.@*
305 @xref{Composing Messages, , , gnus, The Gnus Manual}.
310 @node Shell, Emacs Server, Gnus, Top
311 @section Running Shell Commands from Emacs
313 @cindex shell commands
315 Emacs has commands for passing single command lines to inferior shell
316 processes; it can also run a shell interactively with input and output
317 to an Emacs buffer named @samp{*shell*} or run a shell inside a terminal
321 @item M-! @var{cmd} @key{RET}
322 Run the shell command line @var{cmd} and display the output
323 (@code{shell-command}).
324 @item M-| @var{cmd} @key{RET}
325 Run the shell command line @var{cmd} with region contents as input;
326 optionally replace the region with the output
327 (@code{shell-command-on-region}).
329 Run a subshell with input and output through an Emacs buffer.
330 You can then give commands interactively.
332 Run a subshell with input and output through an Emacs buffer.
333 You can then give commands interactively.
334 Full terminal emulation is available.
337 @kbd{M-x eshell} invokes a shell implemented entirely in Emacs. It
338 is documented in a separate manual. @xref{Top,Eshell,Eshell, eshell,
339 Eshell: The Emacs Shell}.
342 * Single Shell:: How to run one shell command and return.
343 * Interactive Shell:: Permanent shell taking input via Emacs.
344 * Shell Mode:: Special Emacs commands used with permanent shell.
345 * Shell Prompts:: Two ways to recognize shell prompts.
346 * History: Shell History. Repeating previous commands in a shell buffer.
347 * Directory Tracking:: Keeping track when the subshell changes directory.
348 * Options: Shell Options. Options for customizing Shell mode.
349 * Terminal emulator:: An Emacs window as a terminal emulator.
350 * Term Mode:: Special Emacs commands used in Term mode.
351 * Paging in Term:: Paging in the terminal emulator.
352 * Remote Host:: Connecting to another computer.
356 @subsection Single Shell Commands
359 @findex shell-command
360 @kbd{M-!} (@code{shell-command}) reads a line of text using the
361 minibuffer and executes it as a shell command in a subshell made just
362 for that command. Standard input for the command comes from the null
363 device. If the shell command produces any output, the output appears
364 either in the echo area (if it is short), or in an Emacs buffer named
365 @samp{*Shell Command Output*}, which is displayed in another window
366 but not selected (if the output is long).
368 For instance, one way to decompress a file @file{foo.gz} from Emacs
369 is to type @kbd{M-! gunzip foo.gz @key{RET}}. That shell command
370 normally creates the file @file{foo} and produces no terminal output.
372 A numeric argument, as in @kbd{M-1 M-!}, says to insert terminal
373 output into the current buffer instead of a separate buffer. It puts
374 point before the output, and sets the mark after the output. For
375 instance, @kbd{M-1 M-! gunzip < foo.gz @key{RET}} would insert the
376 uncompressed equivalent of @file{foo.gz} into the current buffer.
378 If the shell command line ends in @samp{&}, it runs asynchronously.
379 For a synchronous shell command, @code{shell-command} returns the
380 command's exit status (0 means success), when it is called from a Lisp
381 program. You do not get any status information for an asynchronous
382 command, since it hasn't finished yet when @code{shell-command} returns.
385 @findex shell-command-on-region
386 @kbd{M-|} (@code{shell-command-on-region}) is like @kbd{M-!} but
387 passes the contents of the region as the standard input to the shell
388 command, instead of no input. With a numeric argument, meaning insert
389 the output in the current buffer, it deletes the old region and the
390 output replaces it as the contents of the region. It returns the
391 command's exit status, like @kbd{M-!}.
393 One use for @kbd{M-|} is to run @code{gpg} to see what keys are in
394 the buffer. For instance, if the buffer contains a GPG key, type
395 @kbd{C-x h M-| gpg @key{RET}} to feed the entire buffer contents to
396 the @code{gpg} program. That program will ignore everything except
397 the encoded keys, and will output a list of the keys the buffer
400 @vindex shell-file-name
401 Both @kbd{M-!} and @kbd{M-|} use @code{shell-file-name} to specify
402 the shell to use. This variable is initialized based on your
403 @env{SHELL} environment variable when Emacs is started. If the file
404 name is relative, Emacs searches the directories in the list
405 @code{exec-path}; this list is initialized based on the environment
406 variable @env{PATH} when Emacs is started. Your @file{.emacs} file
407 can override either or both of these default initializations.
409 Both @kbd{M-!} and @kbd{M-|} wait for the shell command to complete,
410 unless you end the command with @samp{&} to make it asynchronous. To
411 stop waiting, type @kbd{C-g} to quit; that terminates the shell
412 command with the signal @code{SIGINT}---the same signal that @kbd{C-c}
413 normally generates in the shell. Emacs then waits until the command
414 actually terminates. If the shell command doesn't stop (because it
415 ignores the @code{SIGINT} signal), type @kbd{C-g} again; this sends
416 the command a @code{SIGKILL} signal which is impossible to ignore.
418 Asynchronous commands ending in @samp{&} feed their output into
419 the buffer @samp{*Async Shell Command*}. Output arrives in that
420 buffer regardless of whether it is visible in a window.
422 To specify a coding system for @kbd{M-!} or @kbd{M-|}, use the command
423 @kbd{C-x @key{RET} c} immediately beforehand. @xref{Communication Coding}.
425 @vindex shell-command-default-error-buffer
426 Error output from these commands is normally intermixed with the
427 regular output. But if the variable
428 @code{shell-command-default-error-buffer} has a string as value, and
429 it's the name of a buffer, @kbd{M-!} and @kbd{M-|} insert error output
430 before point in that buffer.
432 @node Interactive Shell
433 @subsection Interactive Inferior Shell
436 To run a subshell interactively, putting its typescript in an Emacs
437 buffer, use @kbd{M-x shell}. This creates (or reuses) a buffer named
438 @samp{*shell*} and runs a subshell with input coming from and output going
439 to that buffer. That is to say, any ``terminal output'' from the subshell
440 goes into the buffer, advancing point, and any ``terminal input'' for
441 the subshell comes from text in the buffer. To give input to the subshell,
442 go to the end of the buffer and type the input, terminated by @key{RET}.
444 Emacs does not wait for the subshell to do anything. You can switch
445 windows or buffers and edit them while the shell is waiting, or while it is
446 running a command. Output from the subshell waits until Emacs has time to
447 process it; this happens whenever Emacs is waiting for keyboard input or
450 @cindex @code{comint-highlight-input} face
451 @cindex @code{comint-highlight-prompt} face
452 Input lines, once you submit them, are displayed using the face
453 @code{comint-highlight-input}, and prompts are displayed using the
454 face @code{comint-highlight-prompt}. This makes it easier to see
455 previous input lines in the buffer. @xref{Faces}.
457 To make multiple subshells, you can invoke @kbd{M-x shell} with a
458 prefix argument (e.g. @kbd{C-u M-x shell}), which will read a buffer
459 name and create (or reuse) a subshell in that buffer. You can also
460 rename the @samp{*shell*} buffer using @kbd{M-x rename-uniquely}, then
461 create a new @samp{*shell*} buffer using plain @kbd{M-x shell}.
462 Subshells in different buffers run independently and in parallel.
464 @vindex explicit-shell-file-name
465 @cindex environment variables for subshells
466 @cindex @env{ESHELL} environment variable
467 @cindex @env{SHELL} environment variable
468 The file name used to load the subshell is the value of the variable
469 @code{explicit-shell-file-name}, if that is non-@code{nil}. Otherwise,
470 the environment variable @env{ESHELL} is used, or the environment
471 variable @env{SHELL} if there is no @env{ESHELL}. If the file name
472 specified is relative, the directories in the list @code{exec-path} are
473 searched; this list is initialized based on the environment variable
474 @env{PATH} when Emacs is started. Your @file{.emacs} file can override
475 either or both of these default initializations.
477 Emacs sends the new shell the contents of the file
478 @file{~/.emacs_@var{shellname}} as input, if it exists, where
479 @var{shellname} is the name of the file that the shell was loaded
480 from. For example, if you use bash, the file sent to it is
481 @file{~/.emacs_bash}. If this file is not found, Emacs tries to fallback
482 on @file{~/.emacs.d/init_@var{shellname}.sh}.
484 To specify a coding system for the shell, you can use the command
485 @kbd{C-x @key{RET} c} immediately before @kbd{M-x shell}. You can
486 also change the coding system for a running subshell by typing
487 @kbd{C-x @key{RET} p} in the shell buffer. @xref{Communication
490 @cindex @env{INSIDE_EMACS} environment variable
491 Emacs sets the environment variable @env{INSIDE_EMACS} in the
492 subshell to a comma-separated list including the Emacs version.
493 Programs can check this variable to determine whether they are running
494 inside an Emacs subshell.
496 @cindex @env{EMACS} environment variable
497 Emacs also sets the @env{EMACS} environment variable (to @code{t}) if
498 it is not already defined. @strong{Warning:} This environment
499 variable is deprecated. Programs that check this variable should be
500 changed to check @env{INSIDE_EMACS} instead.
503 @subsection Shell Mode
507 Shell buffers use Shell mode, which defines several special keys
508 attached to the @kbd{C-c} prefix. They are chosen to resemble the usual
509 editing and job control characters present in shells that are not under
510 Emacs, except that you must type @kbd{C-c} first. Here is a complete list
511 of the special key bindings of Shell mode:
515 @kindex RET @r{(Shell mode)}
516 @findex comint-send-input
517 At end of buffer send line as input; otherwise, copy current line to
518 end of buffer and send it (@code{comint-send-input}). Copying a line
519 in this way omits any prompt at the beginning of the line (text output
520 by programs preceding your input). @xref{Shell Prompts}, for how
521 Shell mode recognizes prompts.
524 @kindex TAB @r{(Shell mode)}
525 @findex comint-dynamic-complete
526 Complete the command name or file name before point in the shell buffer
527 (@code{comint-dynamic-complete}). @key{TAB} also completes history
528 references (@pxref{History References}) and environment variable names.
530 @vindex shell-completion-fignore
531 @vindex comint-completion-fignore
532 The variable @code{shell-completion-fignore} specifies a list of file
533 name extensions to ignore in Shell mode completion. The default
534 setting is @code{nil}, but some users prefer @code{("~" "#" "%")} to
535 ignore file names ending in @samp{~}, @samp{#} or @samp{%}. Other
536 related Comint modes use the variable @code{comint-completion-fignore}
540 @kindex M-? @r{(Shell mode)}
541 @findex comint-dynamic-list-filename@dots{}
542 Display temporarily a list of the possible completions of the file name
543 before point in the shell buffer
544 (@code{comint-dynamic-list-filename-completions}).
547 @kindex C-d @r{(Shell mode)}
548 @findex comint-delchar-or-maybe-eof
549 Either delete a character or send @acronym{EOF}
550 (@code{comint-delchar-or-maybe-eof}). Typed at the end of the shell
551 buffer, @kbd{C-d} sends @acronym{EOF} to the subshell. Typed at any other
552 position in the buffer, @kbd{C-d} deletes a character as usual.
555 @kindex C-c C-a @r{(Shell mode)}
556 @findex comint-bol-or-process-mark
557 Move to the beginning of the line, but after the prompt if any
558 (@code{comint-bol-or-process-mark}). If you repeat this command twice
559 in a row, the second time it moves back to the process mark, which is
560 the beginning of the input that you have not yet sent to the subshell.
561 (Normally that is the same place---the end of the prompt on this
562 line---but after @kbd{C-c @key{SPC}} the process mark may be in a
566 Accumulate multiple lines of input, then send them together. This
567 command inserts a newline before point, but does not send the preceding
568 text as input to the subshell---at least, not yet. Both lines, the one
569 before this newline and the one after, will be sent together (along with
570 the newline that separates them), when you type @key{RET}.
573 @kindex C-c C-u @r{(Shell mode)}
574 @findex comint-kill-input
575 Kill all text pending at end of buffer to be sent as input
576 (@code{comint-kill-input}). If point is not at end of buffer,
577 this only kills the part of this text that precedes point.
580 @kindex C-c C-w @r{(Shell mode)}
581 Kill a word before point (@code{backward-kill-word}).
584 @kindex C-c C-c @r{(Shell mode)}
585 @findex comint-interrupt-subjob
586 Interrupt the shell or its current subjob if any
587 (@code{comint-interrupt-subjob}). This command also kills
588 any shell input pending in the shell buffer and not yet sent.
591 @kindex C-c C-z @r{(Shell mode)}
592 @findex comint-stop-subjob
593 Stop the shell or its current subjob if any (@code{comint-stop-subjob}).
594 This command also kills any shell input pending in the shell buffer and
598 @findex comint-quit-subjob
599 @kindex C-c C-\ @r{(Shell mode)}
600 Send quit signal to the shell or its current subjob if any
601 (@code{comint-quit-subjob}). This command also kills any shell input
602 pending in the shell buffer and not yet sent.
605 @kindex C-c C-o @r{(Shell mode)}
606 @findex comint-delete-output
607 Delete the last batch of output from a shell command
608 (@code{comint-delete-output}). This is useful if a shell command spews
609 out lots of output that just gets in the way. This command used to be
610 called @code{comint-kill-output}.
613 @kindex C-c C-s @r{(Shell mode)}
614 @findex comint-write-output
615 Write the last batch of output from a shell command to a file
616 (@code{comint-write-output}). With a prefix argument, the file is
617 appended to instead. Any prompt at the end of the output is not
622 @kindex C-c C-r @r{(Shell mode)}
623 @kindex C-M-l @r{(Shell mode)}
624 @findex comint-show-output
625 Scroll to display the beginning of the last batch of output at the top
626 of the window; also move the cursor there (@code{comint-show-output}).
629 @kindex C-c C-e @r{(Shell mode)}
630 @findex comint-show-maximum-output
631 Scroll to put the end of the buffer at the bottom of the window
632 (@code{comint-show-maximum-output}).
635 @kindex C-c C-f @r{(Shell mode)}
636 @findex shell-forward-command
637 @vindex shell-command-regexp
638 Move forward across one shell command, but not beyond the current line
639 (@code{shell-forward-command}). The variable @code{shell-command-regexp}
640 specifies how to recognize the end of a command.
643 @kindex C-c C-b @r{(Shell mode)}
644 @findex shell-backward-command
645 Move backward across one shell command, but not beyond the current line
646 (@code{shell-backward-command}).
649 Ask the shell what its current directory is, so that Emacs can agree
652 @item M-x send-invisible @key{RET} @var{text} @key{RET}
653 @findex send-invisible
654 Send @var{text} as input to the shell, after reading it without
655 echoing. This is useful when a shell command runs a program that asks
658 Please note that Emacs will not echo passwords by default. If you
659 really want them to be echoed, evaluate the following Lisp
663 (remove-hook 'comint-output-filter-functions
664 'comint-watch-for-password-prompt)
667 @item M-x comint-continue-subjob
668 @findex comint-continue-subjob
669 Continue the shell process. This is useful if you accidentally suspend
670 the shell process.@footnote{You should not suspend the shell process.
671 Suspending a subjob of the shell is a completely different matter---that
672 is normal practice, but you must use the shell to continue the subjob;
673 this command won't do it.}
675 @item M-x comint-strip-ctrl-m
676 @findex comint-strip-ctrl-m
677 Discard all control-M characters from the current group of shell output.
678 The most convenient way to use this command is to make it run
679 automatically when you get output from the subshell. To do that,
680 evaluate this Lisp expression:
683 (add-hook 'comint-output-filter-functions
684 'comint-strip-ctrl-m)
687 @item M-x comint-truncate-buffer
688 @findex comint-truncate-buffer
689 This command truncates the shell buffer to a certain maximum number of
690 lines, specified by the variable @code{comint-buffer-maximum-size}.
691 Here's how to do this automatically each time you get output from the
695 (add-hook 'comint-output-filter-functions
696 'comint-truncate-buffer)
702 Shell mode is a derivative of Comint mode, a general-purpose mode for
703 communicating with interactive subprocesses. Most of the features of
704 Shell mode actually come from Comint mode, as you can see from the
705 command names listed above. The special features of Shell mode include
706 the directory tracking feature, and a few user commands.
708 Other Emacs features that use variants of Comint mode include GUD
709 (@pxref{Debuggers}) and @kbd{M-x run-lisp} (@pxref{External Lisp}).
712 You can use @kbd{M-x comint-run} to execute any program of your choice
713 in a subprocess using unmodified Comint mode---without the
714 specializations of Shell mode.
717 @subsection Shell Prompts
719 @vindex shell-prompt-pattern
720 @vindex comint-prompt-regexp
721 @vindex comint-use-prompt-regexp
722 @cindex prompt, shell
723 A prompt is text output by a program to show that it is ready to
724 accept new user input. Normally, Comint mode (and thus Shell mode)
725 considers the prompt to be any text output by a program at the
726 beginning of an input line. However, if the variable
727 @code{comint-use-prompt-regexp} is non-@code{nil}, then Comint mode
728 uses a regular expression to recognize prompts. In Shell mode,
729 @code{shell-prompt-pattern} specifies the regular expression.
731 The value of @code{comint-use-prompt-regexp} also affects many
732 motion and paragraph commands. If the value is non-@code{nil}, the
733 general Emacs motion commands behave as they normally do in buffers
734 without special text properties. However, if the value is @code{nil},
735 the default, then Comint mode divides the buffer into two types of
736 ``fields'' (ranges of consecutive characters having the same
737 @code{field} text property): input and output. Prompts are part of
738 the output. Most Emacs motion commands do not cross field boundaries,
739 unless they move over multiple lines. For instance, when point is in
740 input on the same line as a prompt, @kbd{C-a} puts point at the
741 beginning of the input if @code{comint-use-prompt-regexp} is
742 @code{nil} and at the beginning of the line otherwise.
744 In Shell mode, only shell prompts start new paragraphs. Thus, a
745 paragraph consists of a prompt and the input and output that follow
746 it. However, if @code{comint-use-prompt-regexp} is @code{nil}, the
747 default, most paragraph commands do not cross field boundaries. This
748 means that prompts, ranges of input, and ranges of non-prompt output
749 behave mostly like separate paragraphs; with this setting, numeric
750 arguments to most paragraph commands yield essentially undefined
751 behavior. For the purpose of finding paragraph boundaries, Shell mode
752 uses @code{shell-prompt-pattern}, regardless of
753 @code{comint-use-prompt-regexp}.
756 @subsection Shell Command History
758 Shell buffers support three ways of repeating earlier commands. You
759 can use keys like those used for the minibuffer history; these work
760 much as they do in the minibuffer, inserting text from prior commands
761 while point remains always at the end of the buffer. You can move
762 through the buffer to previous inputs in their original place, then
763 resubmit them or copy them to the end. Or you can use a
764 @samp{!}-style history reference.
767 * Ring: Shell Ring. Fetching commands from the history list.
768 * Copy: Shell History Copying. Moving to a command and then copying it.
769 * History References:: Expanding @samp{!}-style history references.
773 @subsubsection Shell History Ring
776 @findex comint-previous-input
777 @kindex M-p @r{(Shell mode)}
780 Fetch the next earlier old shell command.
782 @kindex M-n @r{(Shell mode)}
783 @findex comint-next-input
786 Fetch the next later old shell command.
788 @kindex M-r @r{(Shell mode)}
789 @kindex M-s @r{(Shell mode)}
790 @findex comint-previous-matching-input
791 @findex comint-next-matching-input
792 @item M-r @var{regexp} @key{RET}
793 @itemx M-s @var{regexp} @key{RET}
794 Search backwards or forwards for old shell commands that match @var{regexp}.
797 @kindex C-c C-x @r{(Shell mode)}
798 @findex comint-get-next-from-history
799 Fetch the next subsequent command from the history.
802 @kindex C-c . @r{(Shell mode)}
803 @findex comint-input-previous-argument
804 Fetch one argument from an old shell command.
807 @kindex C-c C-l @r{(Shell mode)}
808 @findex comint-dynamic-list-input-ring
809 Display the buffer's history of shell commands in another window
810 (@code{comint-dynamic-list-input-ring}).
813 Shell buffers provide a history of previously entered shell commands. To
814 reuse shell commands from the history, use the editing commands @kbd{M-p},
815 @kbd{M-n}, @kbd{M-r} and @kbd{M-s}. These work just like the minibuffer
816 history commands except that they operate on the text at the end of the
817 shell buffer, where you would normally insert text to send to the shell.
819 @kbd{M-p} fetches an earlier shell command to the end of the shell
820 buffer. Successive use of @kbd{M-p} fetches successively earlier
821 shell commands, each replacing any text that was already present as
822 potential shell input. @kbd{M-n} does likewise except that it finds
823 successively more recent shell commands from the buffer.
824 @kbd{C-@key{UP}} works like @kbd{M-p}, and @kbd{C-@key{DOWN}} like
827 The history search commands @kbd{M-r} and @kbd{M-s} read a regular
828 expression and search through the history for a matching command. Aside
829 from the choice of which command to fetch, they work just like @kbd{M-p}
830 and @kbd{M-n}. If you enter an empty regexp, these commands reuse the
831 same regexp used last time.
833 When you find the previous input you want, you can resubmit it by
834 typing @key{RET}, or you can edit it first and then resubmit it if you
835 wish. Any partial input you were composing before navigating the
836 history list is restored when you go to the beginning or end of the
839 Often it is useful to reexecute several successive shell commands that
840 were previously executed in sequence. To do this, first find and
841 reexecute the first command of the sequence. Then type @kbd{C-c C-x};
842 that will fetch the following command---the one that follows the command
843 you just repeated. Then type @key{RET} to reexecute this command. You
844 can reexecute several successive commands by typing @kbd{C-c C-x
845 @key{RET}} over and over.
847 The command @kbd{C-c .}@: (@code{comint-input-previous-argument})
848 copies an individual argument from a previous command, like @kbd{ESC
849 .} in Bash. The simplest use copies the last argument from the
850 previous shell command. With a prefix argument @var{n}, it copies the
851 @var{n}th argument instead. Repeating @kbd{C-c .} copies from an
852 earlier shell command instead, always using the same value of @var{n}
853 (don't give a prefix argument when you repeat the @kbd{C-c .}
856 These commands get the text of previous shell commands from a special
857 history list, not from the shell buffer itself. Thus, editing the shell
858 buffer, or even killing large parts of it, does not affect the history
859 that these commands access.
861 @vindex shell-input-ring-file-name
862 Some shells store their command histories in files so that you can
863 refer to commands from previous shell sessions. Emacs reads
864 the command history file for your chosen shell, to initialize its own
865 command history. The file name is @file{~/.bash_history} for bash,
866 @file{~/.sh_history} for ksh, and @file{~/.history} for other shells.
868 @node Shell History Copying
869 @subsubsection Shell History Copying
872 @kindex C-c C-p @r{(Shell mode)}
873 @findex comint-previous-prompt
875 Move point to the previous prompt (@code{comint-previous-prompt}).
877 @kindex C-c C-n @r{(Shell mode)}
878 @findex comint-next-prompt
880 Move point to the following prompt (@code{comint-next-prompt}).
882 @kindex C-c RET @r{(Shell mode)}
883 @findex comint-copy-old-input
885 Copy the input command which point is in, inserting the copy at the end
886 of the buffer (@code{comint-copy-old-input}). This is useful if you
887 move point back to a previous command. After you copy the command, you
888 can submit the copy as input with @key{RET}. If you wish, you can
889 edit the copy before resubmitting it. If you use this command on an
890 output line, it copies that line to the end of the buffer.
893 If @code{comint-use-prompt-regexp} is @code{nil} (the default), copy
894 the old input command that you click on, inserting the copy at the end
895 of the buffer (@code{comint-insert-input}). If
896 @code{comint-use-prompt-regexp} is non-@code{nil}, or if the click is
897 not over old input, just yank as usual.
900 Moving to a previous input and then copying it with @kbd{C-c
901 @key{RET}} or @kbd{Mouse-2} produces the same results---the same
902 buffer contents---that you would get by using @kbd{M-p} enough times
903 to fetch that previous input from the history list. However, @kbd{C-c
904 @key{RET}} copies the text from the buffer, which can be different
905 from what is in the history list if you edit the input text in the
906 buffer after it has been sent.
908 @node History References
909 @subsubsection Shell History References
910 @cindex history reference
912 Various shells including csh and bash support @dfn{history
913 references} that begin with @samp{!} and @samp{^}. Shell mode
914 recognizes these constructs, and can perform the history substitution
917 If you insert a history reference and type @key{TAB}, this searches
918 the input history for a matching command, performs substitution if
919 necessary, and places the result in the buffer in place of the history
920 reference. For example, you can fetch the most recent command
921 beginning with @samp{mv} with @kbd{! m v @key{TAB}}. You can edit the
922 command if you wish, and then resubmit the command to the shell by
925 @vindex comint-input-autoexpand
926 @findex comint-magic-space
927 Shell mode can optionally expand history references in the buffer
928 when you send them to the shell. To request this, set the variable
929 @code{comint-input-autoexpand} to @code{input}. You can make
930 @key{SPC} perform history expansion by binding @key{SPC} to the
931 command @code{comint-magic-space}.
933 Shell mode recognizes history references when they follow a prompt.
934 @xref{Shell Prompts}, for how Shell mode recognizes prompts.
936 @node Directory Tracking
937 @subsection Directory Tracking
938 @cindex directory tracking
940 @vindex shell-pushd-regexp
941 @vindex shell-popd-regexp
942 @vindex shell-cd-regexp
943 Shell mode keeps track of @samp{cd}, @samp{pushd} and @samp{popd}
944 commands given to the inferior shell, so it can keep the
945 @samp{*shell*} buffer's default directory the same as the shell's
946 working directory. It recognizes these commands syntactically, by
947 examining lines of input that are sent.
949 If you use aliases for these commands, you can tell Emacs to
950 recognize them also. For example, if the value of the variable
951 @code{shell-pushd-regexp} matches the beginning of a shell command
952 line, that line is regarded as a @code{pushd} command. Change this
953 variable when you add aliases for @samp{pushd}. Likewise,
954 @code{shell-popd-regexp} and @code{shell-cd-regexp} are used to
955 recognize commands with the meaning of @samp{popd} and @samp{cd}.
956 These commands are recognized only at the beginning of a shell command
959 @ignore @c This seems to have been deleted long ago.
960 @vindex shell-set-directory-error-hook
961 If Emacs gets an error while trying to handle what it believes is a
962 @samp{cd}, @samp{pushd} or @samp{popd} command, it runs the hook
963 @code{shell-set-directory-error-hook} (@pxref{Hooks}).
967 If Emacs gets confused about changes in the current directory of the
968 subshell, use the command @kbd{M-x dirs} to ask the shell what its
969 current directory is. This command works for shells that support the
970 most common command syntax; it may not work for unusual shells.
972 @findex dirtrack-mode
973 You can also use @kbd{M-x dirtrack-mode} to enable (or disable) an
974 alternative method of tracking changes in the current directory. This
975 method relies on your shell prompt containing the full current working
976 directory at all times.
979 @subsection Shell Mode Options
981 @vindex comint-scroll-to-bottom-on-input
982 If the variable @code{comint-scroll-to-bottom-on-input} is
983 non-@code{nil}, insertion and yank commands scroll the selected window
984 to the bottom before inserting. The default is @code{nil}.
986 @vindex comint-scroll-show-maximum-output
987 If @code{comint-scroll-show-maximum-output} is non-@code{nil}, then
988 arrival of output when point is at the end tries to scroll the last
989 line of text to the bottom line of the window, showing as much useful
990 text as possible. (This mimics the scrolling behavior of most
991 terminals.) The default is @code{t}.
993 @vindex comint-move-point-for-output
994 By setting @code{comint-move-point-for-output}, you can opt for
995 having point jump to the end of the buffer whenever output arrives---no
996 matter where in the buffer point was before. If the value is
997 @code{this}, point jumps in the selected window. If the value is
998 @code{all}, point jumps in each window that shows the Comint buffer. If
999 the value is @code{other}, point jumps in all nonselected windows that
1000 show the current buffer. The default value is @code{nil}, which means
1001 point does not jump to the end.
1003 @vindex comint-prompt-read-only
1004 If you set @code{comint-prompt-read-only}, the prompts in the Comint
1005 buffer are read-only.
1007 @vindex comint-input-ignoredups
1008 The variable @code{comint-input-ignoredups} controls whether successive
1009 identical inputs are stored in the input history. A non-@code{nil}
1010 value means to omit an input that is the same as the previous input.
1011 The default is @code{nil}, which means to store each input even if it is
1012 equal to the previous input.
1014 @vindex comint-completion-addsuffix
1015 @vindex comint-completion-recexact
1016 @vindex comint-completion-autolist
1017 Three variables customize file name completion. The variable
1018 @code{comint-completion-addsuffix} controls whether completion inserts a
1019 space or a slash to indicate a fully completed file or directory name
1020 (non-@code{nil} means do insert a space or slash).
1021 @code{comint-completion-recexact}, if non-@code{nil}, directs @key{TAB}
1022 to choose the shortest possible completion if the usual Emacs completion
1023 algorithm cannot add even a single character.
1024 @code{comint-completion-autolist}, if non-@code{nil}, says to list all
1025 the possible completions whenever completion is not exact.
1027 @vindex shell-completion-execonly
1028 Command completion normally considers only executable files.
1029 If you set @code{shell-completion-execonly} to @code{nil},
1030 it considers nonexecutable files as well.
1032 @findex shell-pushd-tohome
1033 @findex shell-pushd-dextract
1034 @findex shell-pushd-dunique
1035 You can configure the behavior of @samp{pushd}. Variables control
1036 whether @samp{pushd} behaves like @samp{cd} if no argument is given
1037 (@code{shell-pushd-tohome}), pop rather than rotate with a numeric
1038 argument (@code{shell-pushd-dextract}), and only add directories to the
1039 directory stack if they are not already on it
1040 (@code{shell-pushd-dunique}). The values you choose should match the
1041 underlying shell, of course.
1043 If you want Shell mode to handle color output from shell commands,
1044 you can enable ANSI Color mode. Here is how to do this:
1047 (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
1050 @node Terminal emulator
1051 @subsection Emacs Terminal Emulator
1054 To run a subshell in a terminal emulator, putting its typescript in
1055 an Emacs buffer, use @kbd{M-x term}. This creates (or reuses) a
1056 buffer named @samp{*terminal*}, and runs a subshell with input coming
1057 from your keyboard, and output going to that buffer.
1059 The terminal emulator uses Term mode, which has two input modes. In
1060 line mode, Term basically acts like Shell mode; see @ref{Shell Mode}.
1062 In char mode, each character is sent directly to the inferior
1063 subshell, as ``terminal input.'' Any ``echoing'' of your input is the
1064 responsibility of the subshell. The sole exception is the terminal
1065 escape character, which by default is @kbd{C-c} (@pxref{Term Mode}).
1066 Any ``terminal output'' from the subshell goes into the buffer,
1069 Some programs (such as Emacs itself) need to control the appearance
1070 on the terminal screen in detail. They do this by sending special
1071 control codes. The exact control codes needed vary from terminal to
1072 terminal, but nowadays most terminals and terminal emulators
1073 (including @code{xterm}) understand the ANSI-standard (VT100-style)
1074 escape sequences. Term mode recognizes these escape sequences, and
1075 handles each one appropriately, changing the buffer so that the
1076 appearance of the window matches what it would be on a real terminal.
1077 You can actually run Emacs inside an Emacs Term window.
1079 The file name used to load the subshell is determined the same way
1080 as for Shell mode. To make multiple terminal emulators, rename the
1081 buffer @samp{*terminal*} to something different using @kbd{M-x
1082 rename-uniquely}, just as with Shell mode.
1084 Unlike Shell mode, Term mode does not track the current directory by
1085 examining your input. But some shells can tell Term what the current
1086 directory is. This is done automatically by @code{bash} version 1.15
1090 @subsection Term Mode
1094 The terminal emulator uses Term mode, which has two input modes. In
1095 line mode, Term basically acts like Shell mode; see @ref{Shell Mode}.
1096 In char mode, each character is sent directly to the inferior
1097 subshell, except for the Term escape character, normally @kbd{C-c}.
1099 To switch between line and char mode, use these commands:
1102 @kindex C-c C-j @r{(Term mode)}
1103 @findex term-char-mode
1105 Switch to line mode. Do nothing if already in line mode.
1107 @kindex C-c C-k @r{(Term mode)}
1108 @findex term-line-mode
1110 Switch to char mode. Do nothing if already in char mode.
1113 The following commands are only available in char mode:
1117 Send a literal @key{C-c} to the sub-shell.
1119 @item C-c @var{char}
1120 This is equivalent to @kbd{C-x @var{char}} in normal Emacs. For
1121 example, @kbd{C-c o} invokes the global binding of @kbd{C-x o}, which
1122 is normally @samp{other-window}.
1125 @node Paging in Term
1126 @subsection Page-At-A-Time Output
1127 @cindex page-at-a-time
1129 Term mode has a page-at-a-time feature. When enabled it makes
1130 output pause at the end of each screenful.
1133 @kindex C-c C-q @r{(Term mode)}
1134 @findex term-pager-toggle
1136 Toggle the page-at-a-time feature. This command works in both line
1137 and char modes. When page-at-a-time is enabled, the mode-line
1138 displays the word @samp{page}.
1141 With page-at-a-time enabled, whenever Term receives more than a
1142 screenful of output since your last input, it pauses, displaying
1143 @samp{**MORE**} in the mode-line. Type @key{SPC} to display the next
1144 screenful of output. Type @kbd{?} to see your other options. The
1145 interface is similar to the @code{more} program.
1148 @subsection Remote Host Shell
1150 @cindex connecting to remote host
1154 You can login to a remote computer, using whatever commands you
1155 would from a regular terminal (e.g.@: using the @code{telnet} or
1156 @code{rlogin} commands), from a Term window.
1158 A program that asks you for a password will normally suppress
1159 echoing of the password, so the password will not show up in the
1160 buffer. This will happen just as if you were using a real terminal,
1161 if the buffer is in char mode. If it is in line mode, the password is
1162 temporarily visible, but will be erased when you hit return. (This
1163 happens automatically; there is no special password processing.)
1165 When you log in to a different machine, you need to specify the type
1166 of terminal you're using, by setting the @env{TERM} environment
1167 variable in the environment for the remote login command. (If you use
1168 bash, you do that by writing the variable assignment before the remote
1169 login command, without separating comma.) Terminal types @samp{ansi}
1170 or @samp{vt100} will work on most systems.
1172 @c If you are talking to a Bourne-compatible
1173 @c shell, and your system understands the @env{TERMCAP} variable,
1174 @c you can use the command @kbd{M-x shell-send-termcap}, which
1175 @c sends a string specifying the terminal type and size.
1176 @c (This command is also useful after the window has changed size.)
1178 @c You can of course run @samp{gdb} on that remote computer. One useful
1179 @c trick: If you invoke gdb with the @code{--fullname} option,
1180 @c it will send special commands to Emacs that will cause Emacs to
1181 @c pop up the source files you're debugging. This will work
1182 @c whether or not gdb is running on a different computer than Emacs,
1183 @c as long as Emacs can access the source files specified by gdb.
1186 You cannot log in to a remote computer using the Shell mode.
1187 @c (This will change when Shell is re-written to use Term.)
1188 Instead, Emacs provides two commands for logging in to another computer
1189 and communicating with it through an Emacs buffer using Comint mode:
1192 @item M-x telnet @key{RET} @var{hostname} @key{RET}
1193 Set up a Telnet connection to the computer named @var{hostname}.
1194 @item M-x rlogin @key{RET} @var{hostname} @key{RET}
1195 Set up an Rlogin connection to the computer named @var{hostname}.
1199 Use @kbd{M-x telnet} to set up a Telnet connection to another
1200 computer. (Telnet is the standard Internet protocol for remote login.)
1201 It reads the host name of the other computer as an argument with the
1202 minibuffer. Once the connection is established, talking to the other
1203 computer works like talking to a subshell: you can edit input with the
1204 usual Emacs commands, and send it a line at a time by typing @key{RET}.
1205 The output is inserted in the Telnet buffer interspersed with the input.
1208 @vindex rlogin-explicit-args
1209 Use @kbd{M-x rlogin} to set up an Rlogin connection. Rlogin is
1210 another remote login communication protocol, essentially much like the
1211 Telnet protocol but incompatible with it, and supported only by certain
1212 systems. Rlogin's advantages are that you can arrange not to have to
1213 give your user name and password when communicating between two machines
1214 you frequently use, and that you can make an 8-bit-clean connection.
1215 (To do that in Emacs, set @code{rlogin-explicit-args} to @code{("-8")}
1216 before you run Rlogin.)
1218 @kbd{M-x rlogin} sets up the default file directory of the Emacs
1219 buffer to access the remote host via FTP (@pxref{File Names}), and it
1220 tracks the shell commands that change the current directory, just like
1223 @findex rlogin-directory-tracking-mode
1224 There are two ways of doing directory tracking in an Rlogin
1225 buffer---either with remote directory names
1226 @file{/@var{host}:@var{dir}/} or with local names (that works if the
1227 ``remote'' machine shares file systems with your machine of origin).
1228 You can use the command @code{rlogin-directory-tracking-mode} to switch
1229 modes. No argument means use remote directory names, a positive
1230 argument means use local names, and a negative argument means turn
1231 off directory tracking.
1235 @node Emacs Server, Printing, Shell, Top
1236 @section Using Emacs as a Server
1238 @cindex Emacs as a server
1239 @cindex server, using Emacs as
1240 @cindex @env{EDITOR} environment variable
1242 Various programs such as @code{mail} can invoke your choice of editor
1243 to edit a particular piece of text, such as a message that you are
1244 sending. By convention, most of these programs use the environment
1245 variable @env{EDITOR} to specify which editor to run. If you set
1246 @env{EDITOR} to @samp{emacs}, they invoke Emacs---but in an
1247 inconvenient fashion, by starting a new, separate Emacs process. This
1248 is inconvenient because it takes time and because the new Emacs process
1249 doesn't share the buffers with any existing Emacs process.
1251 You can arrange to use your existing Emacs process as the editor for
1252 programs like @code{mail} by using the Emacs client program and the
1253 server that is part of Emacs. Here is how.
1255 @cindex @env{TEXEDIT} environment variable
1256 @findex server-start
1257 First, the preparations. Within Emacs, call the function
1258 @code{server-start}. (Your @file{.emacs} init file can do this
1259 automatically if you add the expression @code{(server-start)} to it,
1260 see @ref{Init File}.) Then, outside Emacs, set the @env{EDITOR}
1261 environment variable to @samp{emacsclient}. (Note that some programs
1262 use a different environment variable; for example, to make @TeX{} use
1263 @samp{emacsclient}, you should set the @env{TEXEDIT} environment
1264 variable to @samp{emacsclient +%d %s}.)
1267 @cindex Bash command to use Emacs server
1268 As an alternative to using @code{emacsclient}, the file
1269 @file{etc/emacs.bash} defines a Bash command @code{edit} which will
1270 communicate with a running Emacs session, or start one if none exist.
1274 Now, whenever any program invokes your specified @env{EDITOR}
1275 program, the effect is to send a message to your principal Emacs telling
1276 it to visit a file. (That's what the program @code{emacsclient} does.)
1277 Emacs displays the buffer immediately and you can immediately begin
1278 editing it in the already running Emacs session.
1280 When you've finished editing that buffer, type @kbd{C-x #}
1281 (@code{server-edit}). This saves the file and sends a message back to
1282 the @code{emacsclient} program telling it to exit. The programs that
1283 use @env{EDITOR} wait for the ``editor'' (actually, @code{emacsclient})
1284 to exit. @kbd{C-x #} also checks for other pending external requests
1285 to edit various files, and selects the next such file.
1287 You can switch to a server buffer manually if you wish; you don't
1288 have to arrive at it with @kbd{C-x #}. But @kbd{C-x #} is the way to
1289 say that you are finished with one.
1291 @vindex server-kill-new-buffers
1292 @vindex server-temp-file-regexp
1293 Finishing with a server buffer also kills the buffer, unless it
1294 already existed in the Emacs session before the server asked to create
1295 it. However, if you set @code{server-kill-new-buffers} to @code{nil},
1296 then a different criterion is used: finishing with a server buffer
1297 kills it if the file name matches the regular expression
1298 @code{server-temp-file-regexp}. This is set up to distinguish certain
1299 ``temporary'' files.
1301 @vindex server-window
1302 If you set the variable @code{server-window} to a window or a frame,
1303 @kbd{C-x #} displays the server buffer in that window or in that frame.
1306 You can run multiple Emacs servers on the same machine by giving
1307 each one a unique ``server name'', using the variable
1308 @code{server-name}. For example, @kbd{M-x set-variable @key{RET}
1309 server-name @key{RET} foo @key{RET}} sets the server name to
1310 @samp{foo}. The @code{emacsclient} program can specify a server by
1311 name using the @samp{-s} option. @xref{Invoking emacsclient}.
1313 While @code{mail} or another application is waiting for
1314 @code{emacsclient} to finish, @code{emacsclient} does not read terminal
1315 input. So the terminal that @code{mail} was using is effectively
1316 blocked for the duration. In order to edit with your principal Emacs,
1317 you need to be able to use it without using that terminal. There are
1318 three ways to do this:
1322 Using a window system, run @code{mail} and the principal Emacs in two
1323 separate windows. While @code{mail} is waiting for @code{emacsclient},
1324 the window where it was running is blocked, but you can use Emacs by
1328 Using virtual terminals, run @code{mail} in one virtual terminal
1329 and run Emacs in another.
1332 Use Shell mode or Term mode in Emacs to run the other program such as
1333 @code{mail}; then, @code{emacsclient} blocks only the subshell under
1334 Emacs, and you can still use Emacs to edit the file.
1337 If you run @code{emacsclient} with the option @samp{--no-wait}, it
1338 returns immediately without waiting for you to ``finish'' the buffer
1339 in Emacs. Note that server buffers created in this way are not killed
1340 automatically when you finish with them.
1343 * Invoking emacsclient:: Emacs client startup options.
1346 @node Invoking emacsclient,, Emacs Server, Emacs Server
1347 @subsection Invoking @code{emacsclient}
1348 @cindex @code{emacsclient} invocation and options
1350 To run the @code{emacsclient} program, specify file names as arguments,
1351 and optionally line numbers as well, like this:
1354 emacsclient @r{@{}@r{[}+@var{line}@r{[}@var{column}@r{]}@r{]} @var{filename}@r{@}}@dots{}
1358 This tells Emacs to visit each of the specified files; if you specify a
1359 line number for a certain file, Emacs moves to that line in the file.
1360 If you specify a column number as well, Emacs puts point on that column
1363 Ordinarily, @code{emacsclient} does not return until you use the
1364 @kbd{C-x #} command on each of these buffers. When that happens,
1365 Emacs sends a message to the @code{emacsclient} program telling it to
1368 If you invoke @code{emacsclient} for more than one file, the
1369 additional client buffers are buried at the bottom of the buffer list
1370 (@pxref{Buffers}). If you call @kbd{C-x #} after you are done editing
1371 a client buffer, the next client buffer is automatically selected.
1373 But if you use the option @samp{-n} or @samp{--no-wait} when running
1374 @code{emacsclient}, then it returns immediately. (You can take as
1375 long as you like to edit the files in Emacs.)
1377 The option @samp{-a @var{command}} or
1378 @samp{--alternate-editor=@var{command}} specifies a command to run if
1379 @code{emacsclient} fails to contact Emacs. This is useful when
1380 running @code{emacsclient} in a script. For example, the following
1381 setting for the @env{EDITOR} environment variable will always give you
1382 an editor, even if no Emacs server is running:
1385 EDITOR="emacsclient --alternate-editor emacs +%d %s"
1389 @cindex @env{ALTERNATE_EDITOR} environment variable
1390 The environment variable @env{ALTERNATE_EDITOR} has the same effect, with
1391 the value of the @samp{--alternate-editor} option taking precedence.
1393 If you use several displays, you can tell Emacs on which display to
1394 open the given files with the @samp{-d @var{display}} or
1395 @samp{--display=@var{display}} option to @code{emacsclient}. This is
1396 handy when connecting from home to an Emacs session running on your
1397 machine at your workplace.
1399 If there is more than one Emacs server running, you can specify a
1400 server name with the @samp{-s @var{name}} or
1401 @samp{--socket-name=@var{name}} option to @code{emacsclient}. (This
1402 option is not supported on MS-Windows.)
1404 You can also use @code{emacsclient} to execute any piece of Emacs Lisp
1405 code, using the @samp{-e} or @samp{--eval} option. When this option
1406 is given, the rest of the arguments is interpreted as a list of
1407 expressions to evaluate, not a list of files to visit.
1409 @cindex @env{EMACS_SERVER_FILE} environment variable
1410 When you start the Emacs server (by calling @code{server-start}),
1411 Emacs creates a file with information about TCP connection to the
1412 server: the host where Emacs is running, the port where it is
1413 listening, and an authentication string. @code{emacsclient} uses this
1414 information if it needs to connect to the server via TCP. By default,
1415 the file goes in the @file{~/.emacs.d/server/} directory@footnote{On
1416 MS-Windows, if @env{HOME} is not set or the TCP configuration file
1417 cannot be found there, Emacs also looks for the file in the
1418 @file{.emacs.d/server/} subdirectory of the directory pointed to by
1419 the @env{APPDATA} environment variable.}. You can specify the file
1420 name to use with the @samp{-f @var{file}} or
1421 @samp{--server-file=@var{file}} options, or by setting
1422 @env{EMACS_SERVER_FILE} environment variable to the file name.
1424 @node Printing, Sorting, Emacs Server, Top
1425 @section Printing Hard Copies
1429 Emacs provides commands for printing hard copies of either an entire
1430 buffer or just part of one, with or without page headers. You can
1431 invoke the printing commands directly, as detailed in the following
1432 section, or using the @samp{File} menu on the menu bar. See also the
1433 hardcopy commands of Dired (@pxref{Misc File Ops}) and the diary
1434 (@pxref{Displaying the Diary}).
1437 @item M-x print-buffer
1438 Print hardcopy of current buffer with page headings containing the file
1439 name and page number.
1440 @item M-x lpr-buffer
1441 Print hardcopy of current buffer without page headings.
1442 @item M-x print-region
1443 Like @code{print-buffer} but print only the current region.
1444 @item M-x lpr-region
1445 Like @code{lpr-buffer} but print only the current region.
1448 @findex print-buffer
1449 @findex print-region
1452 @vindex lpr-switches
1453 The hardcopy commands (aside from the PostScript commands) pass extra
1454 switches to the @code{lpr} program based on the value of the variable
1455 @code{lpr-switches}. Its value should be a list of strings, each string
1456 an option starting with @samp{-}. For example, to specify a line width
1457 of 80 columns for all the printing you do in Emacs, set
1458 @code{lpr-switches} like this:
1461 (setq lpr-switches '("-w80"))
1464 @vindex printer-name
1465 You can specify the printer to use by setting the variable
1466 @code{printer-name}.
1468 @vindex lpr-headers-switches
1469 @vindex lpr-commands
1470 @vindex lpr-add-switches
1471 The variable @code{lpr-command} specifies the name of the printer
1472 program to run; the default value depends on your operating system type.
1473 On most systems, the default is @code{"lpr"}. The variable
1474 @code{lpr-headers-switches} similarly specifies the extra switches to
1475 use to make page headers. The variable @code{lpr-add-switches} controls
1476 whether to supply @samp{-T} and @samp{-J} options (suitable for
1477 @code{lpr}) to the printer program: @code{nil} means don't add them.
1478 @code{lpr-add-switches} should be @code{nil} if your printer program is
1479 not compatible with @code{lpr}.
1482 * PostScript:: Printing buffers or regions as PostScript.
1483 * PostScript Variables:: Customizing the PostScript printing commands.
1484 * Printing Package:: An optional advanced printing interface.
1487 @node PostScript, PostScript Variables,, Printing
1488 @section PostScript Hardcopy
1490 These commands convert buffer contents to PostScript,
1491 either printing it or leaving it in another Emacs buffer.
1494 @item M-x ps-print-buffer
1495 Print hardcopy of the current buffer in PostScript form.
1496 @item M-x ps-print-region
1497 Print hardcopy of the current region in PostScript form.
1498 @item M-x ps-print-buffer-with-faces
1499 Print hardcopy of the current buffer in PostScript form, showing the
1500 faces used in the text by means of PostScript features.
1501 @item M-x ps-print-region-with-faces
1502 Print hardcopy of the current region in PostScript form, showing the
1503 faces used in the text.
1504 @item M-x ps-spool-buffer
1505 Generate PostScript for the current buffer text.
1506 @item M-x ps-spool-region
1507 Generate PostScript for the current region.
1508 @item M-x ps-spool-buffer-with-faces
1509 Generate PostScript for the current buffer, showing the faces used.
1510 @item M-x ps-spool-region-with-faces
1511 Generate PostScript for the current region, showing the faces used.
1513 Generates/prints PostScript for the current buffer as if handwritten.
1516 @findex ps-print-region
1517 @findex ps-print-buffer
1518 @findex ps-print-region-with-faces
1519 @findex ps-print-buffer-with-faces
1520 The PostScript commands, @code{ps-print-buffer} and
1521 @code{ps-print-region}, print buffer contents in PostScript form. One
1522 command prints the entire buffer; the other, just the region. The
1523 corresponding @samp{-with-faces} commands,
1524 @code{ps-print-buffer-with-faces} and @code{ps-print-region-with-faces},
1525 use PostScript features to show the faces (fonts and colors) in the text
1526 properties of the text being printed.
1528 If you are using a color display, you can print a buffer of program
1529 code with color highlighting by turning on Font-Lock mode in that
1530 buffer, and using @code{ps-print-buffer-with-faces}.
1532 @findex ps-spool-region
1533 @findex ps-spool-buffer
1534 @findex ps-spool-region-with-faces
1535 @findex ps-spool-buffer-with-faces
1536 The commands whose names have @samp{spool} instead of @samp{print}
1537 generate the PostScript output in an Emacs buffer instead of sending
1542 @kbd{M-x handwrite} is more frivolous. It generates a PostScript
1543 rendition of the current buffer as a cursive handwritten document. It
1544 can be customized in group @code{handwrite}. This function only
1545 supports ISO 8859-1 characters.
1548 The following section describes variables for customizing these commands.
1551 @node PostScript Variables, Printing Package, PostScript, Printing
1552 @section Variables for PostScript Hardcopy
1554 @vindex ps-lpr-command
1555 @vindex ps-lpr-switches
1556 @vindex ps-printer-name
1557 All the PostScript hardcopy commands use the variables
1558 @code{ps-lpr-command} and @code{ps-lpr-switches} to specify how to print
1559 the output. @code{ps-lpr-command} specifies the command name to run,
1560 @code{ps-lpr-switches} specifies command line options to use, and
1561 @code{ps-printer-name} specifies the printer. If you don't set the
1562 first two variables yourself, they take their initial values from
1563 @code{lpr-command} and @code{lpr-switches}. If @code{ps-printer-name}
1564 is @code{nil}, @code{printer-name} is used.
1566 @vindex ps-print-header
1567 The variable @code{ps-print-header} controls whether these commands
1568 add header lines to each page---set it to @code{nil} to turn headers
1571 @cindex color emulation on black-and-white printers
1572 @vindex ps-print-color-p
1573 If your printer doesn't support colors, you should turn off color
1574 processing by setting @code{ps-print-color-p} to @code{nil}. By
1575 default, if the display supports colors, Emacs produces hardcopy output
1576 with color information; on black-and-white printers, colors are emulated
1577 with shades of gray. This might produce illegible output, even if your
1578 screen colors only use shades of gray.
1580 @vindex ps-use-face-background
1581 By default, PostScript printing ignores the background colors of the
1582 faces, unless the variable @code{ps-use-face-background} is
1583 non-@code{nil}. This is to avoid unwanted interference with the zebra
1584 stripes and background image/text.
1586 @vindex ps-paper-type
1587 @vindex ps-page-dimensions-database
1588 The variable @code{ps-paper-type} specifies which size of paper to
1589 format for; legitimate values include @code{a4}, @code{a3},
1590 @code{a4small}, @code{b4}, @code{b5}, @code{executive}, @code{ledger},
1591 @code{legal}, @code{letter}, @code{letter-small}, @code{statement},
1592 @code{tabloid}. The default is @code{letter}. You can define
1593 additional paper sizes by changing the variable
1594 @code{ps-page-dimensions-database}.
1596 @vindex ps-landscape-mode
1597 The variable @code{ps-landscape-mode} specifies the orientation of
1598 printing on the page. The default is @code{nil}, which stands for
1599 ``portrait'' mode. Any non-@code{nil} value specifies ``landscape''
1602 @vindex ps-number-of-columns
1603 The variable @code{ps-number-of-columns} specifies the number of
1604 columns; it takes effect in both landscape and portrait mode. The
1607 @vindex ps-font-family
1608 @vindex ps-font-size
1609 @vindex ps-font-info-database
1610 The variable @code{ps-font-family} specifies which font family to use
1611 for printing ordinary text. Legitimate values include @code{Courier},
1612 @code{Helvetica}, @code{NewCenturySchlbk}, @code{Palatino} and
1613 @code{Times}. The variable @code{ps-font-size} specifies the size of
1614 the font for ordinary text. It defaults to 8.5 points.
1616 @vindex ps-multibyte-buffer
1617 @cindex Intlfonts for PostScript printing
1618 @cindex fonts for PostScript printing
1619 Emacs supports more scripts and characters than a typical PostScript
1620 printer. Thus, some of the characters in your buffer might not be
1621 printable using the fonts built into your printer. You can augment
1622 the fonts supplied with the printer with those from the GNU Intlfonts
1623 package, or you can instruct Emacs to use Intlfonts exclusively. The
1624 variable @code{ps-multibyte-buffer} controls this: the default value,
1625 @code{nil}, is appropriate for printing @acronym{ASCII} and Latin-1
1626 characters; a value of @code{non-latin-printer} is for printers which
1627 have the fonts for @acronym{ASCII}, Latin-1, Japanese, and Korean
1628 characters built into them. A value of @code{bdf-font} arranges for
1629 the BDF fonts from the Intlfonts package to be used for @emph{all}
1630 characters. Finally, a value of @code{bdf-font-except-latin}
1631 instructs the printer to use built-in fonts for @acronym{ASCII} and Latin-1
1632 characters, and Intlfonts BDF fonts for the rest.
1634 @vindex bdf-directory-list
1635 To be able to use the BDF fonts, Emacs needs to know where to find
1636 them. The variable @code{bdf-directory-list} holds the list of
1637 directories where Emacs should look for the fonts; the default value
1638 includes a single directory @file{/usr/local/share/emacs/fonts/bdf}.
1640 Many other customization variables for these commands are defined and
1641 described in the Lisp files @file{ps-print.el} and @file{ps-mule.el}.
1643 @node Printing Package,, PostScript Variables, Printing
1644 @section Printing Package
1645 @cindex Printing package
1647 The basic Emacs facilities for printing hardcopy can be extended
1648 using the Printing package. This provides an easy-to-use interface
1649 for choosing what to print, previewing PostScript files before
1650 printing, and setting various printing options such as print headers,
1651 landscape or portrait modes, duplex modes, and so forth. On GNU/Linux
1652 or Unix systems, the Printing package relies on the @file{gs} and
1653 @file{gv} utilities, which are distributed as part of the GhostScript
1654 program. On MS-Windows, the @file{gstools} port of Ghostscript can be
1657 @findex pr-interface
1658 To use the Printing package, add @code{(require 'printing)} to your
1659 init file (@pxref{Init File}), followed by @code{(pr-update-menus)}.
1660 This function replaces the usual printing commands in the menu bar
1661 with a @samp{Printing} submenu that contains various printing options.
1662 You can also type @kbd{M-x pr-interface RET}; this creates a
1663 @samp{*Printing Interface*} buffer, similar to a customization buffer,
1664 where you can set the printing options. After selecting what and how
1665 to print, you start the print job using the @samp{Print} button (click
1666 @kbd{mouse-2} on it, or move point over it and type @kbd{RET}). For
1667 further information on the various options, use the @samp{Interface
1670 @node Sorting, Narrowing, Printing, Top
1671 @section Sorting Text
1674 Emacs provides several commands for sorting text in the buffer. All
1675 operate on the contents of the region.
1676 They divide the text of the region into many @dfn{sort records},
1677 identify a @dfn{sort key} for each record, and then reorder the records
1678 into the order determined by the sort keys. The records are ordered so
1679 that their keys are in alphabetical order, or, for numeric sorting, in
1680 numeric order. In alphabetic sorting, all upper-case letters `A' through
1681 `Z' come before lower-case `a', in accord with the @acronym{ASCII} character
1684 The various sort commands differ in how they divide the text into sort
1685 records and in which part of each record is used as the sort key. Most of
1686 the commands make each line a separate sort record, but some commands use
1687 paragraphs or pages as sort records. Most of the sort commands use each
1688 entire sort record as its own sort key, but some use only a portion of the
1689 record as the sort key.
1692 @findex sort-paragraphs
1695 @findex sort-numeric-fields
1696 @vindex sort-numeric-base
1698 @item M-x sort-lines
1699 Divide the region into lines, and sort by comparing the entire
1700 text of a line. A numeric argument means sort into descending order.
1702 @item M-x sort-paragraphs
1703 Divide the region into paragraphs, and sort by comparing the entire
1704 text of a paragraph (except for leading blank lines). A numeric
1705 argument means sort into descending order.
1707 @item M-x sort-pages
1708 Divide the region into pages, and sort by comparing the entire
1709 text of a page (except for leading blank lines). A numeric
1710 argument means sort into descending order.
1712 @item M-x sort-fields
1713 Divide the region into lines, and sort by comparing the contents of
1714 one field in each line. Fields are defined as separated by
1715 whitespace, so the first run of consecutive non-whitespace characters
1716 in a line constitutes field 1, the second such run constitutes field
1719 Specify which field to sort by with a numeric argument: 1 to sort by
1720 field 1, etc. A negative argument means count fields from the right
1721 instead of from the left; thus, minus 1 means sort by the last field.
1722 If several lines have identical contents in the field being sorted, they
1723 keep the same relative order that they had in the original buffer.
1725 @item M-x sort-numeric-fields
1726 Like @kbd{M-x sort-fields} except the specified field is converted
1727 to an integer for each line, and the numbers are compared. @samp{10}
1728 comes before @samp{2} when considered as text, but after it when
1729 considered as a number. By default, numbers are interpreted according
1730 to @code{sort-numeric-base}, but numbers beginning with @samp{0x} or
1731 @samp{0} are interpreted as hexadecimal and octal, respectively.
1733 @item M-x sort-columns
1734 Like @kbd{M-x sort-fields} except that the text within each line
1735 used for comparison comes from a fixed range of columns. See below
1738 @item M-x reverse-region
1739 Reverse the order of the lines in the region. This is useful for
1740 sorting into descending order by fields or columns, since those sort
1741 commands do not have a feature for doing that.
1744 For example, if the buffer contains this:
1747 On systems where clash detection (locking of files being edited) is
1748 implemented, Emacs also checks the first time you modify a buffer
1749 whether the file has changed on disk since it was last visited or
1750 saved. If it has, you are asked to confirm that you want to change
1755 applying @kbd{M-x sort-lines} to the entire buffer produces this:
1758 On systems where clash detection (locking of files being edited) is
1759 implemented, Emacs also checks the first time you modify a buffer
1760 saved. If it has, you are asked to confirm that you want to change
1762 whether the file has changed on disk since it was last visited or
1766 where the upper-case @samp{O} sorts before all lower-case letters. If
1767 you use @kbd{C-u 2 M-x sort-fields} instead, you get this:
1770 implemented, Emacs also checks the first time you modify a buffer
1771 saved. If it has, you are asked to confirm that you want to change
1773 On systems where clash detection (locking of files being edited) is
1774 whether the file has changed on disk since it was last visited or
1778 where the sort keys were @samp{Emacs}, @samp{If}, @samp{buffer},
1779 @samp{systems} and @samp{the}.
1781 @findex sort-columns
1782 @kbd{M-x sort-columns} requires more explanation. You specify the
1783 columns by putting point at one of the columns and the mark at the other
1784 column. Because this means you cannot put point or the mark at the
1785 beginning of the first line of the text you want to sort, this command
1786 uses an unusual definition of ``region'': all of the line point is in is
1787 considered part of the region, and so is all of the line the mark is in,
1788 as well as all the lines in between.
1790 For example, to sort a table by information found in columns 10 to 15,
1791 you could put the mark on column 10 in the first line of the table, and
1792 point on column 15 in the last line of the table, and then run
1793 @code{sort-columns}. Equivalently, you could run it with the mark on
1794 column 15 in the first line and point on column 10 in the last line.
1796 This can be thought of as sorting the rectangle specified by point and
1797 the mark, except that the text on each line to the left or right of the
1798 rectangle moves along with the text inside the rectangle.
1801 @vindex sort-fold-case
1802 Many of the sort commands ignore case differences when comparing, if
1803 @code{sort-fold-case} is non-@code{nil}.
1805 @node Narrowing, Two-Column, Sorting, Top
1810 @cindex accessible portion
1812 @dfn{Narrowing} means focusing in on some portion of the buffer,
1813 making the rest temporarily inaccessible. The portion which you can
1814 still get to is called the @dfn{accessible portion}. Canceling the
1815 narrowing, which makes the entire buffer once again accessible, is
1816 called @dfn{widening}. The bounds of narrowing in effect in a buffer
1817 are called the buffer's @dfn{restriction}.
1819 Narrowing can make it easier to concentrate on a single subroutine or
1820 paragraph by eliminating clutter. It can also be used to limit the
1821 range of operation of a replace command or repeating keyboard macro.
1825 Narrow down to between point and mark (@code{narrow-to-region}).
1827 Widen to make the entire buffer accessible again (@code{widen}).
1829 Narrow down to the current page (@code{narrow-to-page}).
1831 Narrow down to the current defun (@code{narrow-to-defun}).
1834 When you have narrowed down to a part of the buffer, that part appears
1835 to be all there is. You can't see the rest, you can't move into it
1836 (motion commands won't go outside the accessible part), you can't change
1837 it in any way. However, it is not gone, and if you save the file all
1838 the inaccessible text will be saved. The word @samp{Narrow} appears in
1839 the mode line whenever narrowing is in effect.
1842 @findex narrow-to-region
1843 The primary narrowing command is @kbd{C-x n n} (@code{narrow-to-region}).
1844 It sets the current buffer's restrictions so that the text in the current
1845 region remains accessible, but all text before the region or after the
1846 region is inaccessible. Point and mark do not change.
1849 @findex narrow-to-page
1851 @findex narrow-to-defun
1852 Alternatively, use @kbd{C-x n p} (@code{narrow-to-page}) to narrow
1853 down to the current page. @xref{Pages}, for the definition of a page.
1854 @kbd{C-x n d} (@code{narrow-to-defun}) narrows down to the defun
1855 containing point (@pxref{Defuns}).
1859 The way to cancel narrowing is to widen with @kbd{C-x n w}
1860 (@code{widen}). This makes all text in the buffer accessible again.
1862 You can get information on what part of the buffer you are narrowed down
1863 to using the @kbd{C-x =} command. @xref{Position Info}.
1865 Because narrowing can easily confuse users who do not understand it,
1866 @code{narrow-to-region} is normally a disabled command. Attempting to use
1867 this command asks for confirmation and gives you the option of enabling it;
1868 if you enable the command, confirmation will no longer be required for
1869 it. @xref{Disabling}.
1871 @node Two-Column, Editing Binary Files, Narrowing, Top
1872 @section Two-Column Editing
1873 @cindex two-column editing
1874 @cindex splitting columns
1875 @cindex columns, splitting
1877 Two-column mode lets you conveniently edit two side-by-side columns of
1878 text. It uses two side-by-side windows, each showing its own
1881 There are three ways to enter two-column mode:
1884 @item @kbd{@key{F2} 2} or @kbd{C-x 6 2}
1887 @findex 2C-two-columns
1888 Enter two-column mode with the current buffer on the left, and on the
1889 right, a buffer whose name is based on the current buffer's name
1890 (@code{2C-two-columns}). If the right-hand buffer doesn't already
1891 exist, it starts out empty; the current buffer's contents are not
1894 This command is appropriate when the current buffer is empty or contains
1895 just one column and you want to add another column.
1897 @item @kbd{@key{F2} s} or @kbd{C-x 6 s}
1901 Split the current buffer, which contains two-column text, into two
1902 buffers, and display them side by side (@code{2C-split}). The current
1903 buffer becomes the left-hand buffer, but the text in the right-hand
1904 column is moved into the right-hand buffer. The current column
1905 specifies the split point. Splitting starts with the current line and
1906 continues to the end of the buffer.
1908 This command is appropriate when you have a buffer that already contains
1909 two-column text, and you wish to separate the columns temporarily.
1911 @item @kbd{@key{F2} b @var{buffer} @key{RET}}
1912 @itemx @kbd{C-x 6 b @var{buffer} @key{RET}}
1915 @findex 2C-associate-buffer
1916 Enter two-column mode using the current buffer as the left-hand buffer,
1917 and using buffer @var{buffer} as the right-hand buffer
1918 (@code{2C-associate-buffer}).
1921 @kbd{@key{F2} s} or @kbd{C-x 6 s} looks for a column separator, which
1922 is a string that appears on each line between the two columns. You can
1923 specify the width of the separator with a numeric argument to
1924 @kbd{@key{F2} s}; that many characters, before point, constitute the
1925 separator string. By default, the width is 1, so the column separator
1926 is the character before point.
1928 When a line has the separator at the proper place, @kbd{@key{F2} s}
1929 puts the text after the separator into the right-hand buffer, and
1930 deletes the separator. Lines that don't have the column separator at
1931 the proper place remain unsplit; they stay in the left-hand buffer, and
1932 the right-hand buffer gets an empty line to correspond. (This is the
1933 way to write a line that ``spans both columns while in two-column
1934 mode'': write it in the left-hand buffer, and put an empty line in the
1940 The command @kbd{C-x 6 @key{RET}} or @kbd{@key{F2} @key{RET}}
1941 (@code{2C-newline}) inserts a newline in each of the two buffers at
1942 corresponding positions. This is the easiest way to add a new line to
1943 the two-column text while editing it in split buffers.
1948 When you have edited both buffers as you wish, merge them with
1949 @kbd{@key{F2} 1} or @kbd{C-x 6 1} (@code{2C-merge}). This copies the
1950 text from the right-hand buffer as a second column in the other buffer.
1951 To go back to two-column editing, use @kbd{@key{F2} s}.
1955 @findex 2C-dissociate
1956 Use @kbd{@key{F2} d} or @kbd{C-x 6 d} to dissociate the two buffers,
1957 leaving each as it stands (@code{2C-dissociate}). If the other buffer,
1958 the one not current when you type @kbd{@key{F2} d}, is empty,
1959 @kbd{@key{F2} d} kills it.
1961 @node Editing Binary Files, Saving Emacs Sessions, Two-Column, Top
1962 @section Editing Binary Files
1966 @cindex editing binary files
1968 There is a special major mode for editing binary files: Hexl mode. To
1969 use it, use @kbd{M-x hexl-find-file} instead of @kbd{C-x C-f} to visit
1970 the file. This command converts the file's contents to hexadecimal and
1971 lets you edit the translation. When you save the file, it is converted
1972 automatically back to binary.
1974 You can also use @kbd{M-x hexl-mode} to translate an existing buffer
1975 into hex. This is useful if you visit a file normally and then discover
1976 it is a binary file.
1978 Ordinary text characters overwrite in Hexl mode. This is to reduce
1979 the risk of accidentally spoiling the alignment of data in the file.
1980 There are special commands for insertion. Here is a list of the
1981 commands of Hexl mode:
1983 @c I don't think individual index entries for these commands are useful--RMS.
1986 Insert a byte with a code typed in decimal.
1989 Insert a byte with a code typed in octal.
1992 Insert a byte with a code typed in hex.
1995 Move to the beginning of a 1k-byte ``page.''
1998 Move to the end of a 1k-byte ``page.''
2001 Move to an address specified in hex.
2004 Move to an address specified in decimal.
2007 Leave Hexl mode, going back to the major mode this buffer had before you
2008 invoked @code{hexl-mode}.
2012 Other Hexl commands let you insert strings (sequences) of binary
2013 bytes, move by @code{short}s or @code{int}s, etc.; type @kbd{C-h a
2014 hexl-@key{RET}} for details.
2017 @node Saving Emacs Sessions, Recursive Edit, Editing Binary Files, Top
2018 @section Saving Emacs Sessions
2019 @cindex saving sessions
2020 @cindex restore session
2021 @cindex remember editing session
2022 @cindex reload files
2025 Use the desktop library to save the state of Emacs from one session
2026 to another. Once you save the Emacs @dfn{desktop}---the buffers,
2027 their file names, major modes, buffer positions, and so on---then
2028 subsequent Emacs sessions reload the saved desktop.
2030 @findex desktop-save
2031 @vindex desktop-save-mode
2032 You can save the desktop manually with the command @kbd{M-x
2033 desktop-save}. You can also enable automatic saving of the desktop
2034 when you exit Emacs, and automatic restoration of the last saved
2035 desktop when Emacs starts: use the Customization buffer (@pxref{Easy
2036 Customization}) to set @code{desktop-save-mode} to @code{t} for future
2037 sessions, or add this line in your @file{~/.emacs} file:
2040 (desktop-save-mode 1)
2043 @findex desktop-change-dir
2044 @findex desktop-revert
2045 If you turn on @code{desktop-save-mode} in your @file{~/.emacs},
2046 then when Emacs starts, it looks for a saved desktop in the current
2047 directory. Thus, you can have separate saved desktops in different
2048 directories, and the starting directory determines which one Emacs
2049 reloads. You can save the current desktop and reload one saved in
2050 another directory by typing @kbd{M-x desktop-change-dir}. Typing
2051 @kbd{M-x desktop-revert} reverts to the desktop previously reloaded.
2053 Specify the option @samp{--no-desktop} on the command line when you
2054 don't want it to reload any saved desktop. This turns off
2055 @code{desktop-save-mode} for the current session. Starting Emacs with
2056 the @samp{--no-init-file} option also disables desktop reloading,
2057 since it bypasses the @file{.emacs} init file, where
2058 @code{desktop-save-mode} is usually turned on.
2060 @vindex desktop-restore-eager
2061 By default, all the buffers in the desktop are restored at one go.
2062 However, this may be slow if there are a lot of buffers in the
2063 desktop. You can specify the maximum number of buffers to restore
2064 immediately with the variable @code{desktop-restore-eager}; the
2065 remaining buffers are restored ``lazily,'' when Emacs is idle.
2067 @findex desktop-clear
2068 @vindex desktop-globals-to-clear
2069 @vindex desktop-clear-preserve-buffers-regexp
2070 Type @kbd{M-x desktop-clear} to empty the Emacs desktop. This kills
2071 all buffers except for internal ones, and clears the global variables
2072 listed in @code{desktop-globals-to-clear}. If you want this to
2073 preserve certain buffers, customize the variable
2074 @code{desktop-clear-preserve-buffers-regexp}, whose value is a regular
2075 expression matching the names of buffers not to kill.
2077 If you want to save minibuffer history from one session to
2078 another, use the @code{savehist} library.
2080 @node Recursive Edit, Emulation, Saving Emacs Sessions, Top
2081 @section Recursive Editing Levels
2082 @cindex recursive editing level
2083 @cindex editing level, recursive
2085 A @dfn{recursive edit} is a situation in which you are using Emacs
2086 commands to perform arbitrary editing while in the middle of another
2087 Emacs command. For example, when you type @kbd{C-r} inside of a
2088 @code{query-replace}, you enter a recursive edit in which you can change
2089 the current buffer. On exiting from the recursive edit, you go back to
2090 the @code{query-replace}.
2093 @findex exit-recursive-edit
2094 @cindex exiting recursive edit
2095 @dfn{Exiting} the recursive edit means returning to the unfinished
2096 command, which continues execution. The command to exit is @kbd{C-M-c}
2097 (@code{exit-recursive-edit}).
2099 You can also @dfn{abort} the recursive edit. This is like exiting,
2100 but also quits the unfinished command immediately. Use the command
2101 @kbd{C-]} (@code{abort-recursive-edit}) to do this. @xref{Quitting}.
2103 The mode line shows you when you are in a recursive edit by displaying
2104 square brackets around the parentheses that always surround the major and
2105 minor mode names. Every window's mode line shows this in the same way,
2106 since being in a recursive edit is true of Emacs as a whole rather than
2107 any particular window or buffer.
2109 It is possible to be in recursive edits within recursive edits. For
2110 example, after typing @kbd{C-r} in a @code{query-replace}, you may type a
2111 command that enters the debugger. This begins a recursive editing level
2112 for the debugger, within the recursive editing level for @kbd{C-r}.
2113 Mode lines display a pair of square brackets for each recursive editing
2114 level currently in progress.
2116 Exiting the inner recursive edit (such as with the debugger @kbd{c}
2117 command) resumes the command running in the next level up. When that
2118 command finishes, you can then use @kbd{C-M-c} to exit another recursive
2119 editing level, and so on. Exiting applies to the innermost level only.
2120 Aborting also gets out of only one level of recursive edit; it returns
2121 immediately to the command level of the previous recursive edit. If you
2122 wish, you can then abort the next recursive editing level.
2124 Alternatively, the command @kbd{M-x top-level} aborts all levels of
2125 recursive edits, returning immediately to the top-level command reader.
2127 The text being edited inside the recursive edit need not be the same text
2128 that you were editing at top level. It depends on what the recursive edit
2129 is for. If the command that invokes the recursive edit selects a different
2130 buffer first, that is the buffer you will edit recursively. In any case,
2131 you can switch buffers within the recursive edit in the normal manner (as
2132 long as the buffer-switching keys have not been rebound). You could
2133 probably do all the rest of your editing inside the recursive edit,
2134 visiting files and all. But this could have surprising effects (such as
2135 stack overflow) from time to time. So remember to exit or abort the
2136 recursive edit when you no longer need it.
2138 In general, we try to minimize the use of recursive editing levels in
2139 GNU Emacs. This is because they constrain you to ``go back'' in a
2140 particular order---from the innermost level toward the top level. When
2141 possible, we present different activities in separate buffers so that
2142 you can switch between them as you please. Some commands switch to a
2143 new major mode which provides a command to switch back. These
2144 approaches give you more flexibility to go back to unfinished tasks in
2145 the order you choose.
2147 @node Emulation, Hyperlinking, Recursive Edit, Top
2149 @cindex emulating other editors
2150 @cindex other editors
2153 @cindex PC key bindings
2154 @cindex scrolling all windows
2155 @cindex PC selection
2156 @cindex Motif key bindings
2157 @cindex Macintosh key bindings
2160 GNU Emacs can be programmed to emulate (more or less) most other
2161 editors. Standard facilities can emulate these:
2164 @item CRiSP/Brief (PC editor)
2166 @vindex crisp-override-meta-x
2167 @findex scroll-all-mode
2169 @cindex Brief emulation
2170 @cindex emulation of Brief
2172 You can turn on key bindings to emulate the CRiSP/Brief editor with
2173 @kbd{M-x crisp-mode}. Note that this rebinds @kbd{M-x} to exit Emacs
2174 unless you set the variable @code{crisp-override-meta-x}. You can
2175 also use the command @kbd{M-x scroll-all-mode} or set the variable
2176 @code{crisp-load-scroll-all} to emulate CRiSP's scroll-all feature
2177 (scrolling all windows together).
2179 @item EDT (DEC VMS editor)
2180 @findex edt-emulation-on
2181 @findex edt-emulation-off
2182 Turn on EDT emulation with the command @kbd{M-x edt-emulation-on},
2183 while @kbd{M-x edt-emulation-off} restores normal Emacs command
2186 Most of the EDT emulation commands are keypad keys, and most standard
2187 Emacs key bindings are still available. The EDT emulation rebindings
2188 are done in the global keymap, so there is no problem switching
2189 buffers or major modes while in EDT emulation.
2191 @item TPU (DEC VMS editor)
2194 @kbd{M-x tpu-edt-on} turns on emulation of the TPU editor emulating EDT.
2196 @item vi (Berkeley editor)
2198 Viper is the newest emulator for vi. It implements several levels of
2199 emulation; level 1 is closest to vi itself, while level 5 departs
2200 somewhat from strict emulation to take advantage of the capabilities of
2201 Emacs. To invoke Viper, type @kbd{M-x viper-mode}; it will guide you
2202 the rest of the way and ask for the emulation level. @inforef{Top,
2205 @item vi (another emulator)
2207 @kbd{M-x vi-mode} enters a major mode that replaces the previously
2208 established major mode. All of the vi commands that, in real vi, enter
2209 ``input'' mode are programmed instead to return to the previous major
2210 mode. Thus, ordinary Emacs serves as vi's ``input'' mode.
2212 Because vi emulation works through major modes, it does not work
2213 to switch buffers during emulation. Return to normal Emacs first.
2215 If you plan to use vi emulation much, you probably want to bind a key
2216 to the @code{vi-mode} command.
2218 @item vi (alternate emulator)
2220 @kbd{M-x vip-mode} invokes another vi emulator, said to resemble real vi
2221 more thoroughly than @kbd{M-x vi-mode}. ``Input'' mode in this emulator
2222 is changed from ordinary Emacs so you can use @key{ESC} to go back to
2223 emulated vi command mode. To get from emulated vi command mode back to
2224 ordinary Emacs, type @kbd{C-z}.
2226 This emulation does not work through major modes, and it is possible
2227 to switch buffers in various ways within the emulator. It is not
2228 so necessary to assign a key to the command @code{vip-mode} as
2229 it is with @code{vi-mode} because terminating insert mode does
2232 @inforef{Top, VIP, vip}, for full information.
2234 @item WordStar (old wordprocessor)
2235 @findex wordstar-mode
2236 @kbd{M-x wordstar-mode} provides a major mode with WordStar-like
2240 @node Hyperlinking, Dissociated Press, Emulation, Top
2241 @section Hyperlinking and Navigation Features
2243 @cindex hyperlinking
2245 Various modes documented elsewhere have hypertext features so that
2246 you can follow links, usually by clicking @kbd{Mouse-2} on the link or
2247 typing @key{RET} while point is on the link. Clicking @kbd{Mouse-1}
2248 quickly on the link also follows it. (Hold @kbd{Mouse-1} for longer
2249 if you want to set point instead.)
2251 Info mode, Help mode and the Dired-like modes are examples of modes
2252 that have links in the buffer. The Tags facility links between uses
2253 and definitions in source files, see @ref{Tags}. Imenu provides
2254 navigation amongst items indexed in the current buffer, see
2255 @ref{Imenu}. Info-lookup provides mode-specific lookup of definitions
2256 in Info indexes, see @ref{Documentation}. Speedbar maintains a frame
2257 in which links to files, and locations in files are displayed, see
2260 Other non-mode-specific facilities described in this section enable
2261 following links from the current buffer in a context-sensitive
2265 * Browse-URL:: Following URLs.
2266 * Goto-address:: Activating URLs.
2267 * FFAP:: Finding files etc. at point.
2271 @subsection Following URLs
2272 @cindex World Wide Web
2275 @findex browse-url-at-point
2276 @findex browse-url-at-mouse
2281 @item M-x browse-url @key{RET} @var{url} @key{RET}
2282 Load a URL into a Web browser.
2285 The Browse-URL package provides facilities for following URLs specifying
2286 links on the World Wide Web. Usually this works by invoking a web
2287 browser, but you can, for instance, arrange to invoke @code{compose-mail}
2288 from @samp{mailto:} URLs.
2290 The general way to use this feature is to type @kbd{M-x browse-url},
2291 which displays a specified URL. If point is located near a plausible
2292 URL, that URL is used as the default. Other commands are available
2293 which you might like to bind to keys, such as
2294 @code{browse-url-at-point} and @code{browse-url-at-mouse}.
2296 @vindex browse-url-browser-function
2297 You can customize Browse-URL's behavior via various options in the
2298 @code{browse-url} Customize group, particularly
2299 @code{browse-url-browser-function}. You can invoke actions dependent
2300 on the type of URL by defining @code{browse-url-browser-function} as
2301 an association list. The package's commentary available via @kbd{C-h
2302 p} under the @samp{hypermedia} keyword provides more information.
2303 Packages with facilities for following URLs should always go through
2304 Browse-URL, so that the customization options for Browse-URL will
2305 affect all browsing in Emacs.
2308 @subsection Activating URLs
2309 @findex goto-address
2310 @cindex Goto-address
2311 @cindex URLs, activating
2314 @item M-x goto-address
2315 Activate URLs and e-mail addresses in the current buffer.
2318 You can make URLs in the current buffer active with @kbd{M-x
2319 goto-address}. This finds all the URLs in the buffer, and establishes
2320 bindings for @kbd{Mouse-2} and @kbd{C-c @key{RET}} on them. After
2321 activation, if you click on a URL with @kbd{Mouse-2}, or move to a URL
2322 and type @kbd{C-c @key{RET}}, that will display the web page that the URL
2323 specifies. For a @samp{mailto} URL, it sends mail instead, using your
2324 selected mail-composition method (@pxref{Mail Methods}).
2326 It can be useful to add @code{goto-address} to mode hooks and the
2327 hooks used to display an incoming message.
2328 @code{rmail-show-message-hook} is the appropriate hook for Rmail, and
2329 @code{mh-show-mode-hook} for MH-E. This is not needed for Gnus,
2330 which has a similar feature of its own.
2334 @subsection Finding Files and URLs at Point
2335 @findex find-file-at-point
2337 @findex dired-at-point
2340 @cindex finding file at point
2342 FFAP mode replaces certain key bindings for finding files, including
2343 @kbd{C-x C-f}, with commands that provide more sensitive defaults.
2344 These commands behave like the ordinary ones when given a prefix
2345 argument. Otherwise, they get the default file name or URL from the
2346 text around point. If what is found in the buffer has the form of a
2347 URL rather than a file name, the commands use @code{browse-url} to
2350 This feature is useful for following references in mail or news
2351 buffers, @file{README} files, @file{MANIFEST} files, and so on. The
2352 @samp{ffap} package's commentary available via @kbd{C-h p} under the
2353 @samp{files} keyword and the @code{ffap} Custom group provide details.
2355 @cindex FFAP minor mode
2357 You can turn on FFAP minor mode by calling @code{ffap-bindings} to
2358 make the following key bindings and to install hooks for using
2359 @code{ffap} in Rmail, Gnus and VM article buffers.
2362 @item C-x C-f @var{filename} @key{RET}
2363 @kindex C-x C-f @r{(FFAP)}
2364 Find @var{filename}, guessing a default from text around point
2365 (@code{find-file-at-point}).
2367 @kindex C-x C-r @r{(FFAP)}
2368 @code{ffap-read-only}, analogous to @code{find-file-read-only}.
2370 @kindex C-x C-v @r{(FFAP)}
2371 @code{ffap-alternate-file}, analogous to @code{find-alternate-file}.
2372 @item C-x d @var{directory} @key{RET}
2373 @kindex C-x d @r{(FFAP)}
2374 Start Dired on @var{directory}, defaulting to the directory name at
2375 point (@code{dired-at-point}).
2377 @code{ffap-list-directory}, analogous to @code{list-directory}.
2379 @kindex C-x 4 f @r{(FFAP)}
2380 @code{ffap-other-window}, analogous to @code{find-file-other-window}.
2382 @code{ffap-read-only-other-window}, analogous to
2383 @code{find-file-read-only-other-window}.
2385 @code{ffap-dired-other-window}, analogous to @code{dired-other-window}.
2387 @kindex C-x 5 f @r{(FFAP)}
2388 @code{ffap-other-frame}, analogous to @code{find-file-other-frame}.
2390 @code{ffap-read-only-other-frame}, analogous to
2391 @code{find-file-read-only-other-frame}.
2393 @code{ffap-dired-other-frame}, analogous to @code{dired-other-frame}.
2395 Search buffer for next file name or URL, then find that file or URL.
2397 @kindex S-Mouse-3 @r{(FFAP)}
2398 @code{ffap-at-mouse} finds the file guessed from text around the position
2401 @kindex C-S-Mouse-3 @r{(FFAP)}
2402 Display a menu of files and URLs mentioned in current buffer, then
2403 find the one you select (@code{ffap-menu}).
2406 @node Dissociated Press, Amusements, Hyperlinking, Top
2407 @section Dissociated Press
2409 @findex dissociated-press
2410 @kbd{M-x dissociated-press} is a command for scrambling a file of text
2411 either word by word or character by character. Starting from a buffer of
2412 straight English, it produces extremely amusing output. The input comes
2413 from the current Emacs buffer. Dissociated Press writes its output in a
2414 buffer named @samp{*Dissociation*}, and redisplays that buffer after every
2415 couple of lines (approximately) so you can read the output as it comes out.
2417 Dissociated Press asks every so often whether to continue generating
2418 output. Answer @kbd{n} to stop it. You can also stop at any time by
2419 typing @kbd{C-g}. The dissociation output remains in the
2420 @samp{*Dissociation*} buffer for you to copy elsewhere if you wish.
2422 @cindex presidentagon
2423 Dissociated Press operates by jumping at random from one point in the
2424 buffer to another. In order to produce plausible output rather than
2425 gibberish, it insists on a certain amount of overlap between the end of
2426 one run of consecutive words or characters and the start of the next.
2427 That is, if it has just output `president' and then decides to jump
2428 to a different point in the file, it might spot the `ent' in `pentagon'
2429 and continue from there, producing `presidentagon'.@footnote{This
2430 dissociword actually appeared during the Vietnam War, when it was very
2431 appropriate. Bush has made it appropriate again.} Long sample texts
2432 produce the best results.
2434 @cindex againformation
2435 A positive argument to @kbd{M-x dissociated-press} tells it to operate
2436 character by character, and specifies the number of overlap characters. A
2437 negative argument tells it to operate word by word, and specifies the number
2438 of overlap words. In this mode, whole words are treated as the elements to
2439 be permuted, rather than characters. No argument is equivalent to an
2440 argument of two. For your againformation, the output goes only into the
2441 buffer @samp{*Dissociation*}. The buffer you start with is not changed.
2443 @cindex Markov chain
2445 @cindex techniquitous
2446 Dissociated Press produces results fairly like those of a Markov
2447 chain based on a frequency table constructed from the sample text. It
2448 is, however, an independent, ignoriginal invention. Dissociated Press
2449 techniquitously copies several consecutive characters from the sample
2450 between random choices, whereas a Markov chain would choose randomly
2451 for each word or character. This makes for more plausible sounding
2452 results, and runs faster.
2458 @cindex developediment
2460 It is a mustatement that too much use of Dissociated Press can be a
2461 developediment to your real work, sometimes to the point of outragedy.
2462 And keep dissociwords out of your documentation, if you want it to be well
2463 userenced and properbose. Have fun. Your buggestions are welcome.
2465 @node Amusements, Customization, Dissociated Press, Top
2466 @section Other Amusements
2471 @cindex tower of Hanoi
2473 If you are a little bit bored, you can try @kbd{M-x hanoi}. If you are
2474 considerably bored, give it a numeric argument. If you are very, very
2475 bored, try an argument of 9. Sit back and watch.
2478 If you want a little more personal involvement, try @kbd{M-x gomoku},
2479 which plays the game Go Moku with you.
2485 @kbd{M-x blackbox}, @kbd{M-x mpuz} and @kbd{M-x 5x5} are puzzles.
2486 @code{blackbox} challenges you to determine the location of objects
2487 inside a box by tomography. @code{mpuz} displays a multiplication
2488 puzzle with letters standing for digits in a code that you must
2489 guess---to guess a value, type a letter and then the digit you think it
2490 stands for. The aim of @code{5x5} is to fill in all the squares.
2494 @cindex cryptanalysis
2495 @kbd{M-x decipher} helps you to cryptanalyze a buffer which is encrypted
2496 in a simple monoalphabetic substitution cipher.
2499 @kbd{M-x dunnet} runs an adventure-style exploration game, which is
2500 a bigger sort of puzzle.
2503 @cindex landmark game
2504 @kbd{M-x lm} runs a relatively non-participatory game in which a robot
2505 attempts to maneuver towards a tree at the center of the window based on
2506 unique olfactory cues from each of the four directions.
2510 @kbd{M-x life} runs Conway's ``Life'' cellular automaton.
2512 @findex morse-region
2513 @findex unmorse-region
2515 @cindex --/---/.-./.../.
2516 @kbd{M-x morse-region} converts text in a region to Morse code and
2517 @kbd{M-x unmorse-region} converts it back. No cause for remorse.
2521 @kbd{M-x pong} plays a Pong-like game, bouncing the ball off opposing
2526 @kbd{M-x solitaire} plays a game of solitaire in which you jump pegs
2529 @findex studlify-region
2531 @kbd{M-x studlify-region} studlify-cases the region, producing
2535 M-x stUdlIfY-RegioN stUdlIfY-CaSeS thE region.
2542 @kbd{M-x tetris} runs an implementation of the well-known Tetris game.
2543 Likewise, @kbd{M-x snake} provides an implementation of Snake.
2545 When you are frustrated, try the famous Eliza program. Just do
2546 @kbd{M-x doctor}. End each input by typing @key{RET} twice.
2549 When you are feeling strange, type @kbd{M-x yow}.
2552 The command @kbd{M-x zone} plays games with the display when Emacs is
2560 arch-tag: 8f094220-c0d5-4e9e-af7d-3e0da8187474