]> code.delx.au - gnu-emacs/blob - doc/emacs/misc.texi
Merge from origin/emacs-24
[gnu-emacs] / doc / emacs / misc.texi
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985-1987, 1993-1995, 1997, 2000-2014
3 @c Free Software Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5 @iftex
6 @chapter Miscellaneous Commands
7
8 This chapter contains several brief topics that do not fit anywhere
9 else: reading Usenet news, viewing PDFs and other such documents, web
10 browsing, running shell commands and shell subprocesses, using a
11 single shared Emacs for utilities that expect to run an editor as a
12 subprocess, printing, sorting text, editing binary files, saving an
13 Emacs session for later resumption, recursive editing level, following
14 hyperlinks, and various diversions and amusements.
15
16 @end iftex
17
18 @ifnottex
19 @raisesections
20 @end ifnottex
21
22 @node Gnus
23 @section Gnus
24 @cindex Gnus
25 @cindex Usenet news
26 @cindex newsreader
27
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
30 a number of other sources---email, remote directories, digests, and so
31 on. Here we introduce Gnus and describe several basic features.
32 @ifnottex
33 For full details, see @ref{Top, Gnus,, gnus, The Gnus Manual}.
34 @end ifnottex
35 @iftex
36 For full details on Gnus, type @kbd{C-h i} and then select the Gnus
37 manual.
38 @end iftex
39
40 @menu
41 * Buffers of Gnus:: The group, summary, and article buffers.
42 * Gnus Startup:: What you should know about starting Gnus.
43 * Gnus Group Buffer:: A short description of Gnus group commands.
44 * Gnus Summary Buffer:: A short description of Gnus summary commands.
45 @end menu
46
47 @node Buffers of Gnus
48 @subsection Gnus Buffers
49
50 Gnus uses several buffers to display information and to receive
51 commands. The three most commonly-used Gnus buffers are the
52 @dfn{group buffer}, the @dfn{summary buffer} and the @dfn{article
53 buffer}.
54
55 The @dfn{group buffer} contains a list of article sources (e.g.,
56 newsgroups and email inboxes), which are collectively referred to as
57 @dfn{groups}. This is the first buffer Gnus displays when it starts
58 up. It normally displays only the groups to which you subscribe and
59 that contain unread articles. From this buffer, you can select a
60 group to read.
61
62 The @dfn{summary buffer} lists the articles in a single group,
63 showing one article per line. By default, it displays each article's
64 author, subject, and line
65 @iftex
66 number.
67 @end iftex
68 @ifnottex
69 number, but this is customizable; @xref{Summary Buffer Format,,, gnus,
70 The Gnus Manual}.
71 @end ifnottex
72 The summary buffer is created when you select a group in the group
73 buffer, and is killed when you exit the group.
74
75 From the summary buffer, you can choose an article to view. The
76 article is displayed in the @dfn{article buffer}. In normal Gnus
77 usage, you view this buffer but do not select it---all useful Gnus
78 commands can be invoked from the summary buffer. But you can select
79 the article buffer, and execute Gnus commands from it, if you wish.
80
81 @node Gnus Startup
82 @subsection When Gnus Starts Up
83
84 @findex gnus
85 @cindex @file{.newsrc} file
86 If your system has been set up for reading Usenet news, getting
87 started with Gnus is easy---just type @kbd{M-x gnus}.
88
89 On starting up, Gnus reads your @dfn{news initialization file}: a
90 file named @file{.newsrc} in your home directory which lists your
91 Usenet newsgroups and subscriptions (this file is not unique to Gnus;
92 it is used by many other newsreader programs). It then tries to
93 contact the system's default news server, which is typically specified
94 by the @env{NNTPSERVER} environment variable.
95
96 If your system does not have a default news server, or if you wish
97 to use Gnus for reading email, then before invoking @kbd{M-x gnus} you
98 need to tell Gnus where to get news and/or mail. To do this,
99 customize the variables @code{gnus-select-method} and/or
100 @code{gnus-secondary-select-methods}.
101 @iftex
102 See the Gnus manual for details.
103 @end iftex
104 @ifnottex
105 @xref{Finding the News,,, gnus, The Gnus Manual}.
106 @end ifnottex
107
108 Once Gnus has started up, it displays the group buffer. By default,
109 the group buffer shows only a small number of @dfn{subscribed groups}.
110 Groups with other statuses---@dfn{unsubscribed}, @dfn{killed}, or
111 @dfn{zombie}---are hidden. The first time you start Gnus, any group
112 to which you are not subscribed is made into a killed group; any group
113 that subsequently appears on the news server becomes a zombie group.
114
115 To proceed, you must select a group in the group buffer to open the
116 summary buffer for that group; then, select an article in the summary
117 buffer to view its article buffer in a separate window. The following
118 sections explain how to use the group and summary buffers to do this.
119
120 To quit Gnus, type @kbd{q} in the group buffer. This automatically
121 records your group statuses in the files @file{.newsrc} and
122 @file{.newsrc.eld}, so that they take effect in subsequent Gnus
123 sessions.
124
125 @node Gnus Group Buffer
126 @subsection Using the Gnus Group Buffer
127
128 The following commands are available in the Gnus group buffer:
129
130 @table @kbd
131 @kindex SPC @r{(Gnus Group mode)}
132 @findex gnus-group-read-group
133 @item @key{SPC}
134 Switch to the summary buffer for the group on the current line.
135
136 @kindex l @r{(Gnus Group mode)}
137 @kindex A s @r{(Gnus Group mode)}
138 @findex gnus-group-list-groups
139 @item l
140 @itemx A s
141 In the group buffer, list only the groups to which you subscribe and
142 which contain unread articles (this is the default listing).
143
144 @kindex L @r{(Gnus Group mode)}
145 @kindex A u @r{(Gnus Group mode)}
146 @findex gnus-group-list-all-groups
147 @item L
148 @itemx A u
149 List all subscribed and unsubscribed groups, but not killed or zombie
150 groups.
151
152 @kindex A k @r{(Gnus Group mode)}
153 @findex gnus-group-list-all-groups
154 @item A k
155 List killed groups.
156
157 @kindex A z @r{(Gnus Group mode)}
158 @findex gnus-group-list-all-groups
159 @item A z
160 List zombie groups.
161
162 @kindex u @r{(Gnus Group mode)}
163 @findex gnus-group-unsubscribe-current-group
164 @cindex subscribe groups
165 @cindex unsubscribe groups
166 @item u
167 Toggle the subscription status of the group on the current line
168 (i.e., turn a subscribed group into an unsubscribed group, or vice
169 versa). Invoking this on a killed or zombie group turns it into an
170 unsubscribed group.
171
172 @kindex C-k @r{(Gnus Group mode)}
173 @findex gnus-group-kill-group
174 @item C-k
175 Kill the group on the current line. Killed groups are not recorded in
176 the @file{.newsrc} file, and they are not shown in the @kbd{l} or
177 @kbd{L} listings.
178
179 @kindex DEL @r{(Gnus Group mode)}
180 @item @key{DEL}
181 Move point to the previous group containing unread articles.
182
183 @kindex n @r{(Gnus Group mode)}
184 @findex gnus-group-next-unread-group
185 @findex gnus-summary-next-unread-article
186 @item n
187 Move point to the next unread group.
188
189 @kindex p @r{(Gnus Group mode)}
190 @findex gnus-group-prev-unread-group
191 @findex gnus-summary-prev-unread-article
192 @item p
193 Move point to the previous unread group.
194
195 @kindex q @r{(Gnus Group mode)}
196 @findex gnus-group-exit
197 @item q
198 Update your Gnus settings, and quit Gnus.
199 @end table
200
201 @node Gnus Summary Buffer
202 @subsection Using the Gnus Summary Buffer
203
204 The following commands are available in the Gnus summary buffer:
205
206 @table @kbd
207 @kindex SPC @r{(Gnus Summary mode)}
208 @findex gnus-group-read-group
209 @item @key{SPC}
210 If there is no article selected, select the article on the current
211 line and display its article buffer. Otherwise, try scrolling the
212 selected article buffer in its window; on reaching the end of the
213 buffer, select the next unread article.
214
215 Thus, you can read through all articles by repeatedly typing
216 @key{SPC}.
217
218 @kindex DEL @r{(Gnus Summary mode)}
219 @findex gnus-summary-prev-page
220 @item @key{DEL}
221 Scroll the text of the article backwards.
222
223 @kindex n @r{(Gnus Summary mode)}
224 @findex gnus-group-next-unread-group
225 @findex gnus-summary-next-unread-article
226 @item n
227 Select the next unread article.
228
229 @kindex p @r{(Gnus Summary mode)}
230 @findex gnus-group-prev-unread-group
231 @findex gnus-summary-prev-unread-article
232 @item p
233 Select the previous unread article.
234
235 @kindex s @r{(Gnus Summary mode)}
236 @findex gnus-summary-isearch-article
237 @item s
238 Do an incremental search on the selected article buffer, as if you
239 switched to the buffer and typed @kbd{C-s} (@pxref{Incremental
240 Search}).
241
242 @kindex M-s @r{(Gnus Summary mode)}
243 @findex gnus-summary-search-article-forward
244 @item M-s @var{regexp} @key{RET}
245 Search forward for articles containing a match for @var{regexp}.
246
247 @kindex q @r{(Gnus Summary mode)}
248 @item q
249 Exit the summary buffer and return to the group buffer.
250 @end table
251
252
253 @node Network Security
254 @section Network Security
255 @cindex network security manager
256 @cindex NSM
257 @cindex encryption
258 @cindex SSL
259 @cindex TLS
260 @cindex STARTTLS
261
262 Whenever Emacs establishes any network connection, it passes the
263 established connection to the @dfn{Network Security Manager}
264 (@acronym{NSM}). @acronym{NSM} is responsible for enforcing the
265 network security under your control.
266
267 @vindex network-security-level
268 The @code{network-security-level} variable determines the security
269 level that @acronym{NSM} enforces. If its value is @code{low}, no
270 security checks are performed.
271
272 If this variable is @code{medium} (which is the default), a number of
273 checks will be performed. If as result @acronym{NSM} determines that
274 the network connection might not be trustworthy, it will make you
275 aware of that, and will ask you what to do about the network
276 connection.
277
278 You can decide to register a permanent security exception for an
279 unverified connection, a temporary exception, or refuse the connection
280 entirely.
281
282 Below is a list of the checks done on the @code{medium} level.
283
284 @table @asis
285
286 @item unable to verify a @acronym{TLS} certificate
287 If the connection is a @acronym{TLS}, @acronym{SSL} or
288 @acronym{STARTTLS} connection, @acronym{NSM} will check whether
289 the certificate used to establish the identity of the server we're
290 connecting to can be verified.
291
292 While an invalid certificate is often the cause for concern (there
293 could be a Man-in-the-Middle hijacking your network connection and
294 stealing your password), there may be valid reasons for going ahead
295 with the connection anyway. For instance, the server may be using a
296 self-signed certificate, or the certificate may have expired. It's up
297 to you to determine whether it's acceptable to continue with the
298 connection.
299
300 @item a self-signed certificate has changed
301 If you've previously accepted a self-signed certificate, but it has
302 now changed, that could mean that the server has just changed the
303 certificate, but it might also mean that the network connection has
304 been hijacked.
305
306 @item previously encrypted connection now unencrypted
307 If the connection is unencrypted, but it was encrypted in previous
308 sessions, this might mean that there is a proxy between you and the
309 server that strips away @acronym{STARTTLS} announcements, leaving the
310 connection unencrypted. This is usually very suspicious.
311
312 @item talking to an unencrypted service when sending a password
313 When connecting to an @acronym{IMAP} or @acronym{POP3} server, these
314 should usually be encrypted, because it's common to send passwords
315 over these connections. Similarly, if you're sending email via
316 @acronym{SMTP} that requires a password, you usually want that
317 connection to be encrypted. If the connection isn't encrypted,
318 @acronym{NSM} will warn you.
319
320 @end table
321
322 If @code{network-security-level} is @code{high}, the following checks
323 will be made, in addition to the above:
324
325 @table @asis
326 @item a validated certificate changes the public key
327 Servers change their keys occasionally, and that is normally nothing
328 to be concerned about. However, if you are worried that your network
329 connections are being hijacked by agencies who have access to pliable
330 Certificate Authorities which issue new certificates for third-party
331 services, you may want to keep track of these changes.
332 @end table
333
334 Finally, if @code{network-security-level} is @code{paranoid}, you will
335 also be notified the first time @acronym{NSM} sees any new
336 certificate. This will allow you to inspect all the certificates from
337 all the connections that Emacs makes.
338
339 The following additional variables can be used to control details of
340 @acronym{NSM} operation:
341
342 @table @code
343 @item nsm-settings-file
344 @vindex nsm-settings-file
345 This is the file where @acronym{NSM} stores details about connections.
346 It defaults to @file{~/.emacs.d/network-security.data}.
347
348 @item nsm-save-host-names
349 @vindex nsm-save-host-names
350 By default, host names will not be saved for non-@code{STARTTLS}
351 connections. Instead a host/port hash is used to identify connections.
352 This means that one can't casually read the settings file to see what
353 servers the user has connected to. If this variable is @code{t},
354 @acronym{NSM} will also save host names in the nsm-settings-file.
355 @end table
356
357
358 @node Document View
359 @section Document Viewing
360 @cindex DVI file
361 @cindex PDF file
362 @cindex PS file
363 @cindex PostScript file
364 @cindex OpenDocument file
365 @cindex Microsoft Office file
366 @cindex DocView mode
367 @cindex mode, DocView
368 @cindex document viewer (DocView)
369 @findex doc-view-mode
370
371 DocView mode is a major mode for viewing DVI, PostScript (PS), PDF,
372 OpenDocument, and Microsoft Office documents. It provides features
373 such as slicing, zooming, and searching inside documents. It works by
374 converting the document to a set of images using the @command{gs}
375 (GhostScript) or @command{mudraw}/@command{pdfdraw} (MuPDF) commands
376 and other external tools @footnote{For PostScript files, GhostScript
377 is a hard requirement. For DVI files, @code{dvipdf} or @code{dvipdfm}
378 is needed. For OpenDocument and Microsoft Office documents, the
379 @code{unoconv} tool is needed.}, and displaying those images.
380
381 @findex doc-view-toggle-display
382 @findex doc-view-toggle-display
383 @cindex doc-view-minor-mode
384 When you visit a document file that can be displayed with DocView
385 mode, Emacs automatically uses DocView mode @footnote{The needed
386 external tools for the document type must be available, and Emacs must
387 be running in a graphical frame and have PNG image support. If any of
388 these requirements is not fulfilled, Emacs falls back to another major
389 mode.}. As an exception, when you visit a PostScript file, Emacs
390 switches to PS mode, a major mode for editing PostScript files as
391 text; however, it also enables DocView minor mode, so you can type
392 @kbd{C-c C-c} to view the document with DocView. In either DocView
393 mode or DocView minor mode, repeating @kbd{C-c C-c}
394 (@code{doc-view-toggle-display}) toggles between DocView and the
395 underlying file contents.
396
397 @findex doc-view-open-text
398 When you visit a file which would normally be handled by DocView
399 mode but some requirement is not met (e.g., you operate in a terminal
400 frame or emacs has no PNG support), you are queried if you want to
401 view the document's contents as plain text. If you confirm, the
402 buffer is put in text mode and DocView minor mode is activated. Thus,
403 by typing @kbd{C-c C-c} you switch to the fallback mode. With another
404 @kbd{C-c C-c} you return to DocView mode. The plain text contents can
405 also be displayed from within DocView mode by typing @kbd{C-c C-t}
406 (@code{doc-view-open-text}).
407
408 You can explicitly enable DocView mode with the command @code{M-x
409 doc-view-mode}. You can toggle DocView minor mode with @code{M-x
410 doc-view-minor-mode}.
411
412 When DocView mode starts, it displays a welcome screen and begins
413 formatting the file, page by page. It displays the first page once
414 that has been formatted.
415
416 To kill the DocView buffer, type @kbd{k}
417 (@code{doc-view-kill-proc-and-buffer}). To bury it, type @kbd{q}
418 (@code{quit-window}).
419
420 @menu
421 * Navigation: DocView Navigation. Navigating DocView buffers.
422 * Searching: DocView Searching. Searching inside documents.
423 * Slicing: DocView Slicing. Specifying which part of a page is displayed.
424 * Conversion: DocView Conversion. Influencing and triggering conversion.
425 @end menu
426
427 @node DocView Navigation
428 @subsection DocView Navigation
429
430 In DocView mode, you can scroll the current page using the usual
431 Emacs movement keys: @kbd{C-p}, @kbd{C-n}, @kbd{C-b}, @kbd{C-f}, and
432 the arrow keys.
433
434 @vindex doc-view-continuous
435 By default, the line-motion keys @kbd{C-p} and @kbd{C-n} stop
436 scrolling at the beginning and end of the current page, respectively.
437 However, if you change the variable @code{doc-view-continuous} to a
438 non-@code{nil} value, then @kbd{C-p} displays the previous page if you
439 are already at the beginning of the current page, and @kbd{C-n}
440 displays the next page if you are at the end of the current page.
441
442 @findex doc-view-next-page
443 @findex doc-view-previous-page
444 @kindex n @r{(DocView mode)}
445 @kindex p @r{(DocView mode)}
446 @kindex C-x ] @r{(DocView mode)}
447 @kindex C-x [ @r{(DocView mode)}
448 You can also display the next page by typing @kbd{n}, @key{next} or
449 @kbd{C-x ]} (@code{doc-view-next-page}). To display the previous
450 page, type @kbd{p}, @key{prior} or @kbd{C-x [}
451 (@code{doc-view-previous-page}).
452
453 @findex doc-view-scroll-up-or-next-page
454 @findex doc-view-scroll-down-or-previous-page
455 @kindex SPC @r{(DocView mode)}
456 @kindex DEL @r{(DocView mode)}
457 @key{SPC} (@code{doc-view-scroll-up-or-next-page}) is a convenient
458 way to advance through the document. It scrolls within the current
459 page or advances to the next. @key{DEL} moves backwards in a similar
460 way (@code{doc-view-scroll-down-or-previous-page}).
461
462 @findex doc-view-first-page
463 @findex doc-view-last-page
464 @findex doc-view-goto-page
465 @kindex M-< @r{(DocView mode)}
466 @kindex M-> @r{(DocView mode)}
467 To go to the first page, type @kbd{M-<}
468 (@code{doc-view-first-page}); to go to the last one, type @kbd{M->}
469 (@code{doc-view-last-page}). To jump to a page by its number, type
470 @kbd{M-g M-g} or @kbd{M-g g} (@code{doc-view-goto-page}).
471
472 @findex doc-view-enlarge
473 @findex doc-view-shrink
474 @vindex doc-view-resolution
475 @kindex + @r{(DocView mode)}
476 @kindex - @r{(DocView mode)}
477 You can enlarge or shrink the document with @kbd{+}
478 (@code{doc-view-enlarge}) and @kbd{-} (@code{doc-view-shrink}). These
479 commands work by reconverting the document at the new size. To
480 specify the default size for DocView, customize the variable
481 @code{doc-view-resolution}.
482
483 @node DocView Searching
484 @subsection DocView Searching
485
486 In DocView mode, you can search the file's text for a regular
487 expression (@pxref{Regexps}). The interface for searching is inspired
488 by @code{isearch} (@pxref{Incremental Search}).
489
490 @findex doc-view-search
491 @findex doc-view-search-backward
492 @findex doc-view-show-tooltip
493 To begin a search, type @kbd{C-s} (@code{doc-view-search}) or
494 @kbd{C-r} (@code{doc-view-search-backward}). This reads a regular
495 expression using a minibuffer, then echoes the number of matches found
496 within the document. You can move forward and back among the matches
497 by typing @kbd{C-s} and @kbd{C-r}. DocView mode has no way to show
498 the match inside the page image; instead, it displays a tooltip (at
499 the mouse position) listing all matching lines in the current page.
500 To force display of this tooltip, type @kbd{C-t}
501 (@code{doc-view-show-tooltip}).
502
503 To start a new search, use the search command with a prefix
504 argument; i.e., @kbd{C-u C-s} for a forward search or @kbd{C-u C-r}
505 for a backward search.
506
507 @node DocView Slicing
508 @subsection DocView Slicing
509
510 Documents often have wide margins for printing. They are annoying
511 when reading the document on the screen, because they use up screen
512 space and can cause inconvenient scrolling.
513
514 @findex doc-view-set-slice
515 @findex doc-view-set-slice-using-mouse
516 With DocView you can hide these margins by selecting a @dfn{slice}
517 of pages to display. A slice is a rectangle within the page area;
518 once you specify a slice in DocView, it applies to whichever page you
519 look at.
520
521 To specify the slice numerically, type @kbd{s s}
522 (@code{doc-view-set-slice}); then enter the top left pixel position
523 and the slice's width and height.
524 @c ??? how does this work?
525
526 A more convenient graphical way to specify the slice is with @kbd{s
527 m} (@code{doc-view-set-slice-using-mouse}), where you use the mouse to
528 select the slice. Simply press and hold the left mouse button at the
529 upper-left corner of the region you want to have in the slice, then
530 move the mouse pointer to the lower-right corner and release the
531 button.
532
533 The most convenient way is to set the optimal slice by using
534 BoundingBox information automatically determined from the document by
535 typing @kbd{s b} (@code{doc-view-set-slice-from-bounding-box}).
536
537 @findex doc-view-reset-slice
538 To cancel the selected slice, type @kbd{s r}
539 (@code{doc-view-reset-slice}). Then DocView shows the entire page
540 including its entire margins.
541
542 @node DocView Conversion
543 @subsection DocView Conversion
544
545 @vindex doc-view-cache-directory
546 @findex doc-view-clear-cache
547 For efficiency, DocView caches the images produced by @command{gs}.
548 The name of this directory is given by the variable
549 @code{doc-view-cache-directory}. You can clear the cache directory by
550 typing @code{M-x doc-view-clear-cache}.
551
552 @findex doc-view-kill-proc
553 @findex doc-view-kill-proc-and-buffer
554 To force reconversion of the currently viewed document, type @kbd{r}
555 or @kbd{g} (@code{revert-buffer}). To kill the converter process
556 associated with the current buffer, type @kbd{K}
557 (@code{doc-view-kill-proc}). The command @kbd{k}
558 (@code{doc-view-kill-proc-and-buffer}) kills the converter process and
559 the DocView buffer.
560
561 @node EWW
562 @section Web Browsing with EWW
563
564 @findex eww
565 @findex eww-open-file
566 @dfn{EWW}, the Emacs Web Wowser, is a web browser package for Emacs.
567 It allows browsing URLs within an Emacs buffer. The command @kbd{M-x
568 eww} will open a URL or search the web. You can open a file
569 using the command @kbd{M-x eww-open-file}. You can use EWW as the
570 web browser for @code{browse-url}, @pxref{Browse-URL}. For full
571 details, @pxref{Top, EWW,, eww, The Emacs Web Wowser Manual}.
572
573 @node Shell
574 @section Running Shell Commands from Emacs
575 @cindex subshell
576 @cindex shell commands
577
578 Emacs has commands for passing single command lines to shell
579 subprocesses, and for running a shell interactively with input and
580 output to an Emacs buffer, and for running a shell in a terminal
581 emulator window.
582
583 @table @kbd
584 @item M-! @var{cmd} @key{RET}
585 Run the shell command @var{cmd} and display the output
586 (@code{shell-command}).
587 @item M-| @var{cmd} @key{RET}
588 Run the shell command @var{cmd} with region contents as input;
589 optionally replace the region with the output
590 (@code{shell-command-on-region}).
591 @item M-& @var{cmd} @key{RET}
592 Run the shell command @var{cmd} asynchronously, and display the output
593 (@code{async-shell-command}).
594 @item M-x shell
595 Run a subshell with input and output through an Emacs buffer. You can
596 then give commands interactively.
597 @item M-x term
598 Run a subshell with input and output through an Emacs buffer. You can
599 then give commands interactively. Full terminal emulation is
600 available.
601 @end table
602
603 @vindex exec-path
604 Whenever you specify a relative file name for an executable program
605 (either in the @var{cmd} argument to one of the above commands, or in
606 other contexts), Emacs searches for the program in the directories
607 specified by the variable @code{exec-path}. The value of this
608 variable must be a list of directory names; the default value is
609 initialized from the environment variable @env{PATH} when Emacs is
610 started (@pxref{General Variables}).
611
612 @kbd{M-x eshell} invokes a shell implemented entirely in Emacs. It
613 is documented in its own manual.
614 @ifnottex
615 @xref{Top,Eshell,Eshell, eshell, Eshell: The Emacs Shell}.
616 @end ifnottex
617 @iftex
618 See the Eshell Info manual, which is distributed with Emacs.
619 @end iftex
620
621 @menu
622 * Single Shell:: How to run one shell command and return.
623 * Interactive Shell:: Permanent shell taking input via Emacs.
624 * Shell Mode:: Special Emacs commands used with permanent shell.
625 * Shell Prompts:: Two ways to recognize shell prompts.
626 * History: Shell History. Repeating previous commands in a shell buffer.
627 * Directory Tracking:: Keeping track when the subshell changes directory.
628 * Options: Shell Options. Options for customizing Shell mode.
629 * Terminal emulator:: An Emacs window as a terminal emulator.
630 * Term Mode:: Special Emacs commands used in Term mode.
631 * Remote Host:: Connecting to another computer.
632 * Serial Terminal:: Connecting to a serial port.
633 @end menu
634
635 @node Single Shell
636 @subsection Single Shell Commands
637
638 @kindex M-!
639 @findex shell-command
640 @kbd{M-!} (@code{shell-command}) reads a line of text using the
641 minibuffer and executes it as a shell command, in a subshell made just
642 for that command. Standard input for the command comes from the null
643 device. If the shell command produces any output, the output appears
644 either in the echo area (if it is short), or in an Emacs buffer named
645 @file{*Shell Command Output*}, displayed in another window (if the
646 output is long).
647
648 For instance, one way to decompress a file named @file{foo.gz} is to
649 type @kbd{M-! gunzip foo.gz @key{RET}}. That shell command normally
650 creates the file @file{foo} and produces no terminal output.
651
652 A numeric argument to @code{shell-command}, e.g., @kbd{M-1 M-!},
653 causes it to insert terminal output into the current buffer instead of
654 a separate buffer. It puts point before the output, and sets the mark
655 after the output. For instance, @kbd{M-1 M-! gunzip < foo.gz
656 @key{RET}} would insert the uncompressed form of the file
657 @file{foo.gz} into the current buffer.
658
659 Provided the specified shell command does not end with @samp{&}, it
660 runs @dfn{synchronously}, and you must wait for it to exit before
661 continuing to use Emacs. To stop waiting, type @kbd{C-g} to quit;
662 this sends a @code{SIGINT} signal to terminate the shell command (this
663 is the same signal that @kbd{C-c} normally generates in the shell).
664 Emacs then waits until the command actually terminates. If the shell
665 command doesn't stop (because it ignores the @code{SIGINT} signal),
666 type @kbd{C-g} again; this sends the command a @code{SIGKILL} signal,
667 which is impossible to ignore.
668
669 @kindex M-&
670 @findex async-shell-command
671 A shell command that ends in @samp{&} is executed
672 @dfn{asynchronously}, and you can continue to use Emacs as it runs.
673 You can also type @kbd{M-&} (@code{async-shell-command}) to execute a
674 shell command asynchronously; this is exactly like calling @kbd{M-!}
675 with a trailing @samp{&}, except that you do not need the @samp{&}.
676 The default output buffer for asynchronous shell commands is named
677 @samp{*Async Shell Command*}. Emacs inserts the output into this
678 buffer as it comes in, whether or not the buffer is visible in a
679 window.
680
681 @vindex async-shell-command-buffer
682 If you want to run more than one asynchronous shell command at the
683 same time, they could end up competing for the output buffer. The
684 option @code{async-shell-command-buffer} specifies what to do about
685 this; e.g., whether to rename the pre-existing output buffer, or to
686 use a different buffer for the new command. Consult the variable's
687 documentation for more possibilities.
688
689 @kindex M-|
690 @findex shell-command-on-region
691 @kbd{M-|} (@code{shell-command-on-region}) is like @kbd{M-!}, but
692 passes the contents of the region as the standard input to the shell
693 command, instead of no input. With a numeric argument, it deletes the
694 old region and replaces it with the output from the shell command.
695
696 For example, you can use @kbd{M-|} with the @command{gpg} program to
697 see what keys are in the buffer. If the buffer contains a GnuPG key,
698 type @kbd{C-x h M-| gpg @key{RET}} to feed the entire buffer contents
699 to @command{gpg}. This will output the list of keys to the
700 @file{*Shell Command Output*} buffer.
701
702 @vindex shell-file-name
703 The above commands use the shell specified by the variable
704 @code{shell-file-name}. Its default value is determined by the
705 @env{SHELL} environment variable when Emacs is started. If the file
706 name is relative, Emacs searches the directories listed in
707 @code{exec-path} (@pxref{Shell}).
708
709 To specify a coding system for @kbd{M-!} or @kbd{M-|}, use the command
710 @kbd{C-x @key{RET} c} immediately beforehand. @xref{Communication Coding}.
711
712 @vindex shell-command-default-error-buffer
713 By default, error output is intermixed with the regular output in
714 the output buffer. But if you change the value of the variable
715 @code{shell-command-default-error-buffer} to a string, error output is
716 inserted into a buffer of that name.
717
718 @node Interactive Shell
719 @subsection Interactive Subshell
720
721 @findex shell
722 To run a subshell interactively, type @kbd{M-x shell}. This creates
723 (or reuses) a buffer named @file{*shell*}, and runs a shell subprocess
724 with input coming from and output going to that buffer. That is to
725 say, any terminal output from the subshell goes into the buffer,
726 advancing point, and any terminal input for the subshell comes from
727 text in the buffer. To give input to the subshell, go to the end of
728 the buffer and type the input, terminated by @key{RET}.
729
730 While the subshell is waiting or running a command, you can switch
731 windows or buffers and perform other editing in Emacs. Emacs inserts
732 the output from the subshell into the Shell buffer whenever it has
733 time to process it (e.g., while waiting for keyboard input).
734
735 @cindex @code{comint-highlight-input} face
736 @cindex @code{comint-highlight-prompt} face
737 In the Shell buffer, prompts are displayed with the face
738 @code{comint-highlight-prompt}, and submitted input lines are
739 displayed with the face @code{comint-highlight-input}. This makes it
740 easier to distinguish input lines from the shell output.
741 @xref{Faces}.
742
743 To make multiple subshells, invoke @kbd{M-x shell} with a prefix
744 argument (e.g., @kbd{C-u M-x shell}). Then the command will read a
745 buffer name, and create (or reuse) a subshell in that buffer. You can
746 also rename the @file{*shell*} buffer using @kbd{M-x rename-uniquely},
747 then create a new @file{*shell*} buffer using plain @kbd{M-x shell}.
748 Subshells in different buffers run independently and in parallel.
749
750 @vindex explicit-shell-file-name
751 @cindex environment variables for subshells
752 @cindex @env{ESHELL} environment variable
753 @cindex @env{SHELL} environment variable
754 To specify the shell file name used by @kbd{M-x shell}, customize
755 the variable @code{explicit-shell-file-name}. If this is @code{nil}
756 (the default), Emacs uses the environment variable @env{ESHELL} if it
757 exists. Otherwise, it usually uses the variable
758 @code{shell-file-name} (@pxref{Single Shell}); but if the default
759 directory is remote (@pxref{Remote Files}), it prompts you for the
760 shell file name.
761
762 Emacs sends the new shell the contents of the file
763 @file{~/.emacs_@var{shellname}} as input, if it exists, where
764 @var{shellname} is the name of the file that the shell was loaded
765 from. For example, if you use bash, the file sent to it is
766 @file{~/.emacs_bash}. If this file is not found, Emacs tries with
767 @file{~/.emacs.d/init_@var{shellname}.sh}.
768
769 To specify a coding system for the shell, you can use the command
770 @kbd{C-x @key{RET} c} immediately before @kbd{M-x shell}. You can
771 also change the coding system for a running subshell by typing
772 @kbd{C-x @key{RET} p} in the shell buffer. @xref{Communication
773 Coding}.
774
775 @cindex @env{INSIDE_EMACS} environment variable
776 @cindex @env{EMACS} environment variable
777 Emacs sets the environment variable @env{INSIDE_EMACS} in the
778 subshell to @samp{@var{version},comint}, where @var{version} is the
779 Emacs version (e.g., @samp{24.1}). Programs can check this variable
780 to determine whether they are running inside an Emacs subshell. (It
781 also sets the @env{EMACS} environment variable to @code{t}, if that
782 environment variable is not already defined. However, this
783 environment variable is deprecated; programs that use it should switch
784 to using @env{INSIDE_EMACS} instead.)
785
786 @node Shell Mode
787 @subsection Shell Mode
788 @cindex Shell mode
789 @cindex mode, Shell
790
791 The major mode for Shell buffers is Shell mode. Many of its special
792 commands are bound to the @kbd{C-c} prefix, and resemble the usual
793 editing and job control characters present in ordinary shells, except
794 that you must type @kbd{C-c} first. Here is a list of Shell mode
795 commands:
796
797 @table @kbd
798 @item @key{RET}
799 @kindex RET @r{(Shell mode)}
800 @findex comint-send-input
801 Send the current line as input to the subshell
802 (@code{comint-send-input}). Any shell prompt at the beginning of the
803 line is omitted (@pxref{Shell Prompts}). If point is at the end of
804 buffer, this is like submitting the command line in an ordinary
805 interactive shell. However, you can also invoke @key{RET} elsewhere
806 in the shell buffer to submit the current line as input.
807
808 @item @key{TAB}
809 @kindex TAB @r{(Shell mode)}
810 @findex completion-at-point
811 @cindex shell completion
812 Complete the command name or file name before point in the shell
813 buffer (@code{completion-at-point}). This uses the usual Emacs
814 completion rules (@pxref{Completion}), with the completion
815 alternatives being file names, environment variable names, the shell
816 command history, and history references (@pxref{History References}).
817 For options controlling the completion, @pxref{Shell Options}.
818
819 @item M-?
820 @kindex M-? @r{(Shell mode)}
821 @findex comint-dynamic-list-filename@dots{}
822 Display temporarily a list of the possible completions of the file
823 name before point (@code{comint-dynamic-list-filename-completions}).
824
825 @item C-d
826 @kindex C-d @r{(Shell mode)}
827 @findex comint-delchar-or-maybe-eof
828 Either delete a character or send @acronym{EOF}
829 (@code{comint-delchar-or-maybe-eof}). Typed at the end of the shell
830 buffer, this sends @acronym{EOF} to the subshell. Typed at any other
831 position in the buffer, this deletes a character as usual.
832
833 @item C-c C-a
834 @kindex C-c C-a @r{(Shell mode)}
835 @findex comint-bol-or-process-mark
836 Move to the beginning of the line, but after the prompt if any
837 (@code{comint-bol-or-process-mark}). If you repeat this command twice
838 in a row, the second time it moves back to the process mark, which is
839 the beginning of the input that you have not yet sent to the subshell.
840 (Normally that is the same place---the end of the prompt on this
841 line---but after @kbd{C-c @key{SPC}} the process mark may be in a
842 previous line.)
843
844 @item C-c @key{SPC}
845 Accumulate multiple lines of input, then send them together. This
846 command inserts a newline before point, but does not send the preceding
847 text as input to the subshell---at least, not yet. Both lines, the one
848 before this newline and the one after, will be sent together (along with
849 the newline that separates them), when you type @key{RET}.
850
851 @item C-c C-u
852 @kindex C-c C-u @r{(Shell mode)}
853 @findex comint-kill-input
854 Kill all text pending at end of buffer to be sent as input
855 (@code{comint-kill-input}). If point is not at end of buffer,
856 this only kills the part of this text that precedes point.
857
858 @item C-c C-w
859 @kindex C-c C-w @r{(Shell mode)}
860 Kill a word before point (@code{backward-kill-word}).
861
862 @item C-c C-c
863 @kindex C-c C-c @r{(Shell mode)}
864 @findex comint-interrupt-subjob
865 Interrupt the shell or its current subjob if any
866 (@code{comint-interrupt-subjob}). This command also kills
867 any shell input pending in the shell buffer and not yet sent.
868
869 @item C-c C-z
870 @kindex C-c C-z @r{(Shell mode)}
871 @findex comint-stop-subjob
872 Stop the shell or its current subjob if any (@code{comint-stop-subjob}).
873 This command also kills any shell input pending in the shell buffer and
874 not yet sent.
875
876 @item C-c C-\
877 @findex comint-quit-subjob
878 @kindex C-c C-\ @r{(Shell mode)}
879 Send quit signal to the shell or its current subjob if any
880 (@code{comint-quit-subjob}). This command also kills any shell input
881 pending in the shell buffer and not yet sent.
882
883 @item C-c C-o
884 @kindex C-c C-o @r{(Shell mode)}
885 @findex comint-delete-output
886 Delete the last batch of output from a shell command
887 (@code{comint-delete-output}). This is useful if a shell command spews
888 out lots of output that just gets in the way.
889
890 @item C-c C-s
891 @kindex C-c C-s @r{(Shell mode)}
892 @findex comint-write-output
893 Write the last batch of output from a shell command to a file
894 (@code{comint-write-output}). With a prefix argument, the file is
895 appended to instead. Any prompt at the end of the output is not
896 written.
897
898 @item C-c C-r
899 @itemx C-M-l
900 @kindex C-c C-r @r{(Shell mode)}
901 @kindex C-M-l @r{(Shell mode)}
902 @findex comint-show-output
903 Scroll to display the beginning of the last batch of output at the top
904 of the window; also move the cursor there (@code{comint-show-output}).
905
906 @item C-c C-e
907 @kindex C-c C-e @r{(Shell mode)}
908 @findex comint-show-maximum-output
909 Scroll to put the end of the buffer at the bottom of the window
910 (@code{comint-show-maximum-output}).
911
912 @item C-c C-f
913 @kindex C-c C-f @r{(Shell mode)}
914 @findex shell-forward-command
915 @vindex shell-command-regexp
916 Move forward across one shell command, but not beyond the current line
917 (@code{shell-forward-command}). The variable @code{shell-command-regexp}
918 specifies how to recognize the end of a command.
919
920 @item C-c C-b
921 @kindex C-c C-b @r{(Shell mode)}
922 @findex shell-backward-command
923 Move backward across one shell command, but not beyond the current line
924 (@code{shell-backward-command}).
925
926 @item M-x dirs
927 Ask the shell for its working directory, and update the Shell buffer's
928 default directory. @xref{Directory Tracking}.
929
930 @item M-x send-invisible @key{RET} @var{text} @key{RET}
931 @findex send-invisible
932 Send @var{text} as input to the shell, after reading it without
933 echoing. This is useful when a shell command runs a program that asks
934 for a password.
935
936 Please note that Emacs will not echo passwords by default. If you
937 really want them to be echoed, evaluate (@pxref{Lisp Eval}) the
938 following Lisp expression:
939
940 @example
941 (remove-hook 'comint-output-filter-functions
942 'comint-watch-for-password-prompt)
943 @end example
944
945 @item M-x comint-continue-subjob
946 @findex comint-continue-subjob
947 Continue the shell process. This is useful if you accidentally suspend
948 the shell process.@footnote{You should not suspend the shell process.
949 Suspending a subjob of the shell is a completely different matter---that
950 is normal practice, but you must use the shell to continue the subjob;
951 this command won't do it.}
952
953 @item M-x comint-strip-ctrl-m
954 @findex comint-strip-ctrl-m
955 Discard all control-M characters from the current group of shell output.
956 The most convenient way to use this command is to make it run
957 automatically when you get output from the subshell. To do that,
958 evaluate this Lisp expression:
959
960 @example
961 (add-hook 'comint-output-filter-functions
962 'comint-strip-ctrl-m)
963 @end example
964
965 @item M-x comint-truncate-buffer
966 @findex comint-truncate-buffer
967 This command truncates the shell buffer to a certain maximum number of
968 lines, specified by the variable @code{comint-buffer-maximum-size}.
969 Here's how to do this automatically each time you get output from the
970 subshell:
971
972 @example
973 (add-hook 'comint-output-filter-functions
974 'comint-truncate-buffer)
975 @end example
976 @end table
977
978 @cindex Comint mode
979 @cindex mode, Comint
980 Shell mode is a derivative of Comint mode, a general-purpose mode for
981 communicating with interactive subprocesses. Most of the features of
982 Shell mode actually come from Comint mode, as you can see from the
983 command names listed above. The special features of Shell mode include
984 the directory tracking feature, and a few user commands.
985
986 Other Emacs features that use variants of Comint mode include GUD
987 (@pxref{Debuggers}) and @kbd{M-x run-lisp} (@pxref{External Lisp}).
988
989 @findex comint-run
990 You can use @kbd{M-x comint-run} to execute any program of your choice
991 in a subprocess using unmodified Comint mode---without the
992 specializations of Shell mode.
993
994 @node Shell Prompts
995 @subsection Shell Prompts
996
997 @cindex prompt, shell
998 A prompt is text output by a program to show that it is ready to
999 accept new user input. Normally, Comint mode (and thus Shell mode)
1000 automatically figures out part of the buffer is a prompt, based on the
1001 output of the subprocess. (Specifically, it assumes that any received
1002 output line which doesn't end with a newline is a prompt.)
1003
1004 Comint mode divides the buffer into two types of @dfn{fields}: input
1005 fields (where user input is typed) and output fields (everywhere
1006 else). Prompts are part of the output fields. Most Emacs motion
1007 commands do not cross field boundaries, unless they move over multiple
1008 lines. For instance, when point is in the input field on a shell
1009 command line, @kbd{C-a} puts point at the beginning of the input
1010 field, after the prompt. Internally, the fields are implemented using
1011 the @code{field} text property (@pxref{Text Properties,,, elisp, the
1012 Emacs Lisp Reference Manual}).
1013
1014 @vindex comint-use-prompt-regexp
1015 @vindex shell-prompt-pattern
1016 If you change the variable @code{comint-use-prompt-regexp} to a
1017 non-@code{nil} value, then Comint mode recognize prompts using a
1018 regular expression (@pxref{Regexps}). In Shell mode, the regular
1019 expression is specified by the variable @code{shell-prompt-pattern}.
1020 The default value of @code{comint-use-prompt-regexp} is @code{nil},
1021 because this method for recognizing prompts is unreliable, but you may
1022 want to set it to a non-@code{nil} value in unusual circumstances. In
1023 that case, Emacs does not divide the Comint buffer into fields, so the
1024 general motion commands behave as they normally do in buffers without
1025 special text properties. However, you can use the paragraph motion
1026 commands to conveniently navigate the buffer (@pxref{Paragraphs}); in
1027 Shell mode, Emacs uses @code{shell-prompt-pattern} as paragraph
1028 boundaries.
1029
1030 @node Shell History
1031 @subsection Shell Command History
1032
1033 Shell buffers support three ways of repeating earlier commands. You
1034 can use keys like those used for the minibuffer history; these work
1035 much as they do in the minibuffer, inserting text from prior commands
1036 while point remains always at the end of the buffer. You can move
1037 through the buffer to previous inputs in their original place, then
1038 resubmit them or copy them to the end. Or you can use a
1039 @samp{!}-style history reference.
1040
1041 @menu
1042 * Ring: Shell Ring. Fetching commands from the history list.
1043 * Copy: Shell History Copying. Moving to a command and then copying it.
1044 * History References:: Expanding @samp{!}-style history references.
1045 @end menu
1046
1047 @node Shell Ring
1048 @subsubsection Shell History Ring
1049
1050 @table @kbd
1051 @findex comint-previous-input
1052 @kindex M-p @r{(Shell mode)}
1053 @item M-p
1054 @itemx C-@key{UP}
1055 Fetch the next earlier old shell command.
1056
1057 @kindex M-n @r{(Shell mode)}
1058 @findex comint-next-input
1059 @item M-n
1060 @itemx C-@key{DOWN}
1061 Fetch the next later old shell command.
1062
1063 @kindex M-r @r{(Shell mode)}
1064 @findex comint-history-isearch-backward-regexp
1065 @item M-r
1066 Begin an incremental regexp search of old shell commands.
1067
1068 @item C-c C-x
1069 @kindex C-c C-x @r{(Shell mode)}
1070 @findex comint-get-next-from-history
1071 Fetch the next subsequent command from the history.
1072
1073 @item C-c .
1074 @kindex C-c . @r{(Shell mode)}
1075 @findex comint-input-previous-argument
1076 Fetch one argument from an old shell command.
1077
1078 @item C-c C-l
1079 @kindex C-c C-l @r{(Shell mode)}
1080 @findex comint-dynamic-list-input-ring
1081 Display the buffer's history of shell commands in another window
1082 (@code{comint-dynamic-list-input-ring}).
1083 @end table
1084
1085 Shell buffers provide a history of previously entered shell
1086 commands. To reuse shell commands from the history, use the editing
1087 commands @kbd{M-p}, @kbd{M-n}, @kbd{M-r} and @kbd{M-s}. These work
1088 just like the minibuffer history commands (@pxref{Minibuffer
1089 History}), except that they operate within the Shell buffer rather
1090 than the minibuffer.
1091
1092 @kbd{M-p} fetches an earlier shell command to the end of the shell
1093 buffer. Successive use of @kbd{M-p} fetches successively earlier
1094 shell commands, each replacing any text that was already present as
1095 potential shell input. @kbd{M-n} does likewise except that it finds
1096 successively more recent shell commands from the buffer.
1097 @kbd{C-@key{UP}} works like @kbd{M-p}, and @kbd{C-@key{DOWN}} like
1098 @kbd{M-n}.
1099
1100 The history search command @kbd{M-r} begins an incremental regular
1101 expression search of previous shell commands. After typing @kbd{M-r},
1102 start typing the desired string or regular expression; the last
1103 matching shell command will be displayed in the current line.
1104 Incremental search commands have their usual effects---for instance,
1105 @kbd{C-s} and @kbd{C-r} search forward and backward for the next match
1106 (@pxref{Incremental Search}). When you find the desired input, type
1107 @key{RET} to terminate the search. This puts the input in the command
1108 line. Any partial input you were composing before navigating the
1109 history list is restored when you go to the beginning or end of the
1110 history ring.
1111
1112 Often it is useful to reexecute several successive shell commands that
1113 were previously executed in sequence. To do this, first find and
1114 reexecute the first command of the sequence. Then type @kbd{C-c C-x};
1115 that will fetch the following command---the one that follows the command
1116 you just repeated. Then type @key{RET} to reexecute this command. You
1117 can reexecute several successive commands by typing @kbd{C-c C-x
1118 @key{RET}} over and over.
1119
1120 The command @kbd{C-c .}@: (@code{comint-input-previous-argument})
1121 copies an individual argument from a previous command, like
1122 @kbd{@key{ESC} .} in Bash. The simplest use copies the last argument from the
1123 previous shell command. With a prefix argument @var{n}, it copies the
1124 @var{n}th argument instead. Repeating @kbd{C-c .} copies from an
1125 earlier shell command instead, always using the same value of @var{n}
1126 (don't give a prefix argument when you repeat the @kbd{C-c .}
1127 command).
1128
1129 These commands get the text of previous shell commands from a special
1130 history list, not from the shell buffer itself. Thus, editing the shell
1131 buffer, or even killing large parts of it, does not affect the history
1132 that these commands access.
1133
1134 @vindex shell-input-ring-file-name
1135 Some shells store their command histories in files so that you can
1136 refer to commands from previous shell sessions. Emacs reads
1137 the command history file for your chosen shell, to initialize its own
1138 command history. The file name is @file{~/.bash_history} for bash,
1139 @file{~/.sh_history} for ksh, and @file{~/.history} for other shells.
1140
1141 @node Shell History Copying
1142 @subsubsection Shell History Copying
1143
1144 @table @kbd
1145 @kindex C-c C-p @r{(Shell mode)}
1146 @findex comint-previous-prompt
1147 @item C-c C-p
1148 Move point to the previous prompt (@code{comint-previous-prompt}).
1149
1150 @kindex C-c C-n @r{(Shell mode)}
1151 @findex comint-next-prompt
1152 @item C-c C-n
1153 Move point to the following prompt (@code{comint-next-prompt}).
1154
1155 @kindex C-c RET @r{(Shell mode)}
1156 @findex comint-copy-old-input
1157 @item C-c @key{RET}
1158 Copy the input command at point, inserting the copy at the end of the
1159 buffer (@code{comint-copy-old-input}). This is useful if you move
1160 point back to a previous command. After you copy the command, you can
1161 submit the copy as input with @key{RET}. If you wish, you can edit
1162 the copy before resubmitting it. If you use this command on an output
1163 line, it copies that line to the end of the buffer.
1164
1165 @item Mouse-2
1166 If @code{comint-use-prompt-regexp} is @code{nil} (the default), copy
1167 the old input command that you click on, inserting the copy at the end
1168 of the buffer (@code{comint-insert-input}). If
1169 @code{comint-use-prompt-regexp} is non-@code{nil}, or if the click is
1170 not over old input, just yank as usual.
1171 @end table
1172
1173 Moving to a previous input and then copying it with @kbd{C-c
1174 @key{RET}} or @kbd{Mouse-2} produces the same results---the same
1175 buffer contents---that you would get by using @kbd{M-p} enough times
1176 to fetch that previous input from the history list. However, @kbd{C-c
1177 @key{RET}} copies the text from the buffer, which can be different
1178 from what is in the history list if you edit the input text in the
1179 buffer after it has been sent.
1180
1181 @node History References
1182 @subsubsection Shell History References
1183 @cindex history reference
1184
1185 Various shells including csh and bash support @dfn{history
1186 references} that begin with @samp{!} and @samp{^}. Shell mode
1187 recognizes these constructs, and can perform the history substitution
1188 for you.
1189
1190 If you insert a history reference and type @key{TAB}, this searches
1191 the input history for a matching command, performs substitution if
1192 necessary, and places the result in the buffer in place of the history
1193 reference. For example, you can fetch the most recent command
1194 beginning with @samp{mv} with @kbd{! m v @key{TAB}}. You can edit the
1195 command if you wish, and then resubmit the command to the shell by
1196 typing @key{RET}.
1197
1198 @vindex comint-input-autoexpand
1199 @findex comint-magic-space
1200 Shell mode can optionally expand history references in the buffer
1201 when you send them to the shell. To request this, set the variable
1202 @code{comint-input-autoexpand} to @code{input}. You can make
1203 @key{SPC} perform history expansion by binding @key{SPC} to the
1204 command @code{comint-magic-space}.
1205
1206 Shell mode recognizes history references when they follow a prompt.
1207 @xref{Shell Prompts}, for how Shell mode recognizes prompts.
1208
1209 @node Directory Tracking
1210 @subsection Directory Tracking
1211 @cindex directory tracking
1212
1213 @vindex shell-pushd-regexp
1214 @vindex shell-popd-regexp
1215 @vindex shell-cd-regexp
1216 Shell mode keeps track of @samp{cd}, @samp{pushd} and @samp{popd}
1217 commands given to the subshell, in order to keep the Shell buffer's
1218 default directory (@pxref{File Names}) the same as the shell's working
1219 directory. It recognizes these commands by examining lines of input
1220 that you send.
1221
1222 If you use aliases for these commands, you can tell Emacs to
1223 recognize them also, by setting the variables
1224 @code{shell-pushd-regexp}, @code{shell-popd-regexp}, and
1225 @code{shell-cd-regexp} to the appropriate regular expressions
1226 (@pxref{Regexps}). For example, if @code{shell-pushd-regexp} matches
1227 the beginning of a shell command line, that line is regarded as a
1228 @code{pushd} command. These commands are recognized only at the
1229 beginning of a shell command line.
1230
1231 @findex dirs
1232 If Emacs gets confused about changes in the working directory of the
1233 subshell, type @kbd{M-x dirs}. This command asks the shell for its
1234 working directory and updates the default directory accordingly. It
1235 works for shells that support the most common command syntax, but may
1236 not work for unusual shells.
1237
1238 @findex dirtrack-mode
1239 @cindex Dirtrack mode
1240 @cindex mode, Dirtrack
1241 @vindex dirtrack-list
1242 You can also use Dirtrack mode, a buffer-local minor mode that
1243 implements an alternative method of tracking the shell's working
1244 directory. To use this method, your shell prompt must contain the
1245 working directory at all times, and you must supply a regular
1246 expression for recognizing which part of the prompt contains the
1247 working directory; see the documentation of the variable
1248 @code{dirtrack-list} for details. To use Dirtrack mode, type @kbd{M-x
1249 dirtrack-mode} in the Shell buffer, or add @code{dirtrack-mode} to
1250 @code{shell-mode-hook} (@pxref{Hooks}).
1251
1252 @node Shell Options
1253 @subsection Shell Mode Options
1254
1255 @vindex comint-scroll-to-bottom-on-input
1256 If the variable @code{comint-scroll-to-bottom-on-input} is
1257 non-@code{nil}, insertion and yank commands scroll the selected window
1258 to the bottom before inserting. The default is @code{nil}.
1259
1260 @vindex comint-scroll-show-maximum-output
1261 If @code{comint-scroll-show-maximum-output} is non-@code{nil}, then
1262 arrival of output when point is at the end tries to scroll the last
1263 line of text to the bottom line of the window, showing as much useful
1264 text as possible. (This mimics the scrolling behavior of most
1265 terminals.) The default is @code{t}.
1266
1267 @vindex comint-move-point-for-output
1268 By setting @code{comint-move-point-for-output}, you can opt for
1269 having point jump to the end of the buffer whenever output arrives---no
1270 matter where in the buffer point was before. If the value is
1271 @code{this}, point jumps in the selected window. If the value is
1272 @code{all}, point jumps in each window that shows the Comint buffer. If
1273 the value is @code{other}, point jumps in all nonselected windows that
1274 show the current buffer. The default value is @code{nil}, which means
1275 point does not jump to the end.
1276
1277 @vindex comint-prompt-read-only
1278 If you set @code{comint-prompt-read-only}, the prompts in the Comint
1279 buffer are read-only.
1280
1281 @vindex comint-input-ignoredups
1282 The variable @code{comint-input-ignoredups} controls whether successive
1283 identical inputs are stored in the input history. A non-@code{nil}
1284 value means to omit an input that is the same as the previous input.
1285 The default is @code{nil}, which means to store each input even if it is
1286 equal to the previous input.
1287
1288 @vindex comint-completion-addsuffix
1289 @vindex comint-completion-recexact
1290 @vindex comint-completion-autolist
1291 Three variables customize file name completion. The variable
1292 @code{comint-completion-addsuffix} controls whether completion inserts a
1293 space or a slash to indicate a fully completed file or directory name
1294 (non-@code{nil} means do insert a space or slash).
1295 @code{comint-completion-recexact}, if non-@code{nil}, directs @key{TAB}
1296 to choose the shortest possible completion if the usual Emacs completion
1297 algorithm cannot add even a single character.
1298 @code{comint-completion-autolist}, if non-@code{nil}, says to list all
1299 the possible completions whenever completion is not exact.
1300
1301 @vindex shell-completion-execonly
1302 Command completion normally considers only executable files.
1303 If you set @code{shell-completion-execonly} to @code{nil},
1304 it considers nonexecutable files as well.
1305
1306 @vindex shell-completion-fignore
1307 @vindex comint-completion-fignore
1308 The variable @code{shell-completion-fignore} specifies a list of file
1309 name extensions to ignore in Shell mode completion. The default
1310 setting is @code{nil}, but some users prefer @code{("~" "#" "%")} to
1311 ignore file names ending in @samp{~}, @samp{#} or @samp{%}. Other
1312 related Comint modes use the variable @code{comint-completion-fignore}
1313 instead.
1314
1315 @findex shell-dynamic-complete-command
1316 Some implementation details of the shell command completion may also be found
1317 in the lisp documentation of the @code{shell-dynamic-complete-command}
1318 function.
1319
1320 @findex shell-pushd-tohome
1321 @findex shell-pushd-dextract
1322 @findex shell-pushd-dunique
1323 You can configure the behavior of @samp{pushd}. Variables control
1324 whether @samp{pushd} behaves like @samp{cd} if no argument is given
1325 (@code{shell-pushd-tohome}), pop rather than rotate with a numeric
1326 argument (@code{shell-pushd-dextract}), and only add directories to the
1327 directory stack if they are not already on it
1328 (@code{shell-pushd-dunique}). The values you choose should match the
1329 underlying shell, of course.
1330
1331 @node Terminal emulator
1332 @subsection Emacs Terminal Emulator
1333 @findex term
1334
1335 To run a subshell in a text terminal emulator, use @kbd{M-x term}.
1336 This creates (or reuses) a buffer named @file{*terminal*}, and runs a
1337 subshell with input coming from your keyboard, and output going to
1338 that buffer.
1339
1340 @cindex line mode @r{(terminal emulator)}
1341 @cindex char mode @r{(terminal emulator)}
1342 The terminal emulator uses Term mode, which has two input modes. In
1343 @dfn{line mode}, Term basically acts like Shell mode (@pxref{Shell
1344 Mode}). In @dfn{char mode}, each character is sent directly to the
1345 subshell, as terminal input; the sole exception is the terminal escape
1346 character, which by default is @kbd{C-c} (@pxref{Term Mode}). Any
1347 echoing of your input is the responsibility of the subshell; any
1348 terminal output from the subshell goes into the buffer, advancing
1349 point.
1350
1351 Some programs (such as Emacs itself) need to control the appearance
1352 of the terminal screen in detail. They do this by emitting special
1353 control codes. Term mode recognizes and handles ANSI-standard
1354 VT100-style escape sequences, which are accepted by most modern
1355 terminals, including @command{xterm}. (Hence, you can actually run
1356 Emacs inside an Emacs Term window.)
1357
1358 The @code{term} face specifies the default appearance of text
1359 in the terminal emulator (the default is the same appearance as the
1360 @code{default} face). When terminal control codes are used to change
1361 the appearance of text, these are represented in the terminal emulator
1362 by the faces @code{term-color-black}, @code{term-color-red},
1363 @code{term-color-green}, @code{term-color-yellow}
1364 @code{term-color-blue}, @code{term-color-magenta},
1365 @code{term-color-cyan}, @code{term-color-white},
1366 @code{term-color-underline}, and @code{term-color-bold}.
1367 @xref{Faces}.
1368
1369 You can also Term mode to communicate with a device connected to a
1370 serial port. @xref{Serial Terminal}.
1371
1372 The file name used to load the subshell is determined the same way
1373 as for Shell mode. To make multiple terminal emulators, rename the
1374 buffer @file{*terminal*} to something different using @kbd{M-x
1375 rename-uniquely}, just as with Shell mode.
1376
1377 Unlike Shell mode, Term mode does not track the current directory by
1378 examining your input. But some shells can tell Term what the current
1379 directory is. This is done automatically by @code{bash} version 1.15
1380 and later.
1381
1382
1383
1384
1385 @node Term Mode
1386 @subsection Term Mode
1387 @cindex Term mode
1388 @cindex mode, Term
1389
1390 The terminal emulator uses Term mode, which has two input modes. In
1391 line mode, Term basically acts like Shell mode (@pxref{Shell Mode}).
1392 In char mode, each character is sent directly to the subshell, except
1393 for the Term escape character, normally @kbd{C-c}.
1394
1395 To switch between line and char mode, use these commands:
1396
1397 @table @kbd
1398 @kindex C-c C-j @r{(Term mode)}
1399 @findex term-line-mode
1400 @item C-c C-j
1401 Switch to line mode (@code{term-line-mode}). Do nothing if already in
1402 line mode.
1403
1404 @kindex C-c C-k @r{(Term mode)}
1405 @findex term-char-mode
1406 @item C-c C-k
1407 Switch to char mode (@code{term-char-mode}). Do nothing if already in
1408 char mode.
1409 @end table
1410
1411 The following commands are only available in char mode:
1412
1413 @table @kbd
1414 @item C-c C-c
1415 Send a literal @key{C-c} to the sub-shell.
1416
1417 @item C-c @var{char}
1418 This is equivalent to @kbd{C-x @var{char}} in normal Emacs. For
1419 example, @kbd{C-c o} invokes the global binding of @kbd{C-x o}, which
1420 is normally @samp{other-window}.
1421 @end table
1422
1423 @cindex paging in Term mode
1424 Term mode has a page-at-a-time feature. When enabled, it makes
1425 output pause at the end of each screenful:
1426
1427 @table @kbd
1428 @kindex C-c C-q @r{(Term mode)}
1429 @findex term-pager-toggle
1430 @item C-c C-q
1431 Toggle the page-at-a-time feature. This command works in both line
1432 and char modes. When the feature is enabled, the mode-line displays
1433 the word @samp{page}, and each time Term receives more than a
1434 screenful of output, it pauses and displays @samp{**MORE**} in the
1435 mode-line. Type @key{SPC} to display the next screenful of output, or
1436 @kbd{?} to see your other options. The interface is similar to the
1437 @code{more} program.
1438 @end table
1439
1440 @node Remote Host
1441 @subsection Remote Host Shell
1442 @cindex remote host
1443 @cindex connecting to remote host
1444 @cindex Telnet
1445 @cindex Rlogin
1446
1447 You can login to a remote computer, using whatever commands you
1448 would from a regular terminal (e.g., using the @code{telnet} or
1449 @code{rlogin} commands), from a Term window.
1450
1451 A program that asks you for a password will normally suppress
1452 echoing of the password, so the password will not show up in the
1453 buffer. This will happen just as if you were using a real terminal,
1454 if the buffer is in char mode. If it is in line mode, the password is
1455 temporarily visible, but will be erased when you hit return. (This
1456 happens automatically; there is no special password processing.)
1457
1458 When you log in to a different machine, you need to specify the type
1459 of terminal you're using, by setting the @env{TERM} environment
1460 variable in the environment for the remote login command. (If you use
1461 bash, you do that by writing the variable assignment before the remote
1462 login command, without a separating comma.) Terminal types
1463 @samp{ansi} or @samp{vt100} will work on most systems.
1464
1465 @node Serial Terminal
1466 @subsection Serial Terminal
1467 @cindex terminal, serial
1468 @findex serial-term
1469
1470 If you have a device connected to a serial port of your computer,
1471 you can communicate with it by typing @kbd{M-x serial-term}. This
1472 command asks for a serial port name and speed, and switches to a new
1473 Term mode buffer. Emacs communicates with the serial device through
1474 this buffer just like it does with a terminal in ordinary Term mode.
1475
1476 The speed of the serial port is measured in bits per second. The
1477 most common speed is 9600 bits per second. You can change the speed
1478 interactively by clicking on the mode line.
1479
1480 A serial port can be configured even more by clicking on ``8N1'' in
1481 the mode line. By default, a serial port is configured as ``8N1'',
1482 which means that each byte consists of 8 data bits, No parity check
1483 bit, and 1 stopbit.
1484
1485 If the speed or the configuration is wrong, you cannot communicate
1486 with your device and will probably only see garbage output in the
1487 window.
1488
1489 @node Emacs Server
1490 @section Using Emacs as a Server
1491 @pindex emacsclient
1492 @cindex Emacs as a server
1493 @cindex server, using Emacs as
1494 @cindex @env{EDITOR} environment variable
1495
1496 Various programs can invoke your choice of editor to edit a
1497 particular piece of text. For instance, version control programs
1498 invoke an editor to enter version control logs (@pxref{Version
1499 Control}), and the Unix @command{mail} utility invokes an editor to
1500 enter a message to send. By convention, your choice of editor is
1501 specified by the environment variable @env{EDITOR}. If you set
1502 @env{EDITOR} to @samp{emacs}, Emacs would be invoked, but in an
1503 inconvenient way---by starting a new Emacs process. This is
1504 inconvenient because the new Emacs process doesn't share buffers, a
1505 command history, or other kinds of information with any existing Emacs
1506 process.
1507
1508 You can solve this problem by setting up Emacs as an @dfn{edit
1509 server}, so that it ``listens'' for external edit requests and acts
1510 accordingly. There are two ways to start an Emacs server:
1511
1512 @itemize
1513 @findex server-start
1514 @item
1515 Run the command @code{server-start} in an existing Emacs process:
1516 either type @kbd{M-x server-start}, or put the expression
1517 @code{(server-start)} in your init file (@pxref{Init File}). The
1518 existing Emacs process is the server; when you exit Emacs, the server
1519 dies with the Emacs process.
1520
1521 @cindex daemon, Emacs
1522 @item
1523 Run Emacs as a @dfn{daemon}, using the @samp{--daemon} command-line
1524 option. @xref{Initial Options}. When Emacs is started this way, it
1525 calls @code{server-start} after initialization, and returns control to
1526 the calling terminal instead of opening an initial frame; it then
1527 waits in the background, listening for edit requests.
1528 @end itemize
1529
1530 @cindex @env{TEXEDIT} environment variable
1531 Either way, once an Emacs server is started, you can use a shell
1532 command called @command{emacsclient} to connect to the Emacs process
1533 and tell it to visit a file. You can then set the @env{EDITOR}
1534 environment variable to @samp{emacsclient}, so that external programs
1535 will use the existing Emacs process for editing.@footnote{Some
1536 programs use a different environment variable; for example, to make
1537 @TeX{} use @samp{emacsclient}, set the @env{TEXEDIT} environment
1538 variable to @samp{emacsclient +%d %s}.}
1539
1540 @vindex server-name
1541 You can run multiple Emacs servers on the same machine by giving
1542 each one a unique ``server name'', using the variable
1543 @code{server-name}. For example, @kbd{M-x set-variable @key{RET}
1544 server-name @key{RET} foo @key{RET}} sets the server name to
1545 @samp{foo}. The @code{emacsclient} program can specify a server by
1546 name, using the @samp{-s} option (@pxref{emacsclient Options}).
1547
1548 @findex server-eval-at
1549 If you have defined a server by a unique server name, it is possible
1550 to connect to the server from another Emacs instance and evaluate Lisp
1551 expressions on the server, using the @code{server-eval-at} function.
1552 For instance, @code{(server-eval-at "foo" '(+ 1 2))} evaluates the
1553 expression @code{(+ 1 2)} on the @samp{foo} server, and returns
1554 @code{3}. (If there is no server with that name, an error is
1555 signaled.) Currently, this feature is mainly useful for developers.
1556
1557 @menu
1558 * Invoking emacsclient:: Connecting to the Emacs server.
1559 * emacsclient Options:: Emacs client startup options.
1560 @end menu
1561
1562 @node Invoking emacsclient
1563 @subsection Invoking @code{emacsclient}
1564 @cindex @code{emacsclient} invocation
1565
1566 The simplest way to use the @command{emacsclient} program is to run
1567 the shell command @samp{emacsclient @var{file}}, where @var{file} is a
1568 file name. This connects to an Emacs server, and tells that Emacs
1569 process to visit @var{file} in one of its existing frames---either a
1570 graphical frame, or one in a text terminal (@pxref{Frames}). You
1571 can then select that frame to begin editing.
1572
1573 If there is no Emacs server, the @command{emacsclient} program halts
1574 with an error message. If the Emacs process has no existing
1575 frame---which can happen if it was started as a daemon (@pxref{Emacs
1576 Server})---then Emacs opens a frame on the terminal in which you
1577 called @command{emacsclient}.
1578
1579 You can also force @command{emacsclient} to open a new frame on a
1580 graphical display, or on a text terminal, using the @samp{-c} and
1581 @samp{-t} options. @xref{emacsclient Options}.
1582
1583 If you are running on a single text terminal, you can switch between
1584 @command{emacsclient}'s shell and the Emacs server using one of two
1585 methods: (i) run the Emacs server and @command{emacsclient} on
1586 different virtual terminals, and switch to the Emacs server's virtual
1587 terminal after calling @command{emacsclient}; or (ii) call
1588 @command{emacsclient} from within the Emacs server itself, using Shell
1589 mode (@pxref{Interactive Shell}) or Term mode (@pxref{Term Mode});
1590 @code{emacsclient} blocks only the subshell under Emacs, and you can
1591 still use Emacs to edit the file.
1592
1593 @kindex C-x #
1594 @findex server-edit
1595 When you finish editing @var{file} in the Emacs server, type
1596 @kbd{C-x #} (@code{server-edit}) in its buffer. This saves the file
1597 and sends a message back to the @command{emacsclient} program, telling
1598 it to exit. Programs that use @env{EDITOR} usually wait for the
1599 ``editor''---in this case @command{emacsclient}---to exit before doing
1600 something else.
1601
1602 You can also call @command{emacsclient} with multiple file name
1603 arguments: @samp{emacsclient @var{file1} @var{file2} ...} tells the
1604 Emacs server to visit @var{file1}, @var{file2}, and so forth. Emacs
1605 selects the buffer visiting @var{file1}, and buries the other buffers
1606 at the bottom of the buffer list (@pxref{Buffers}). The
1607 @command{emacsclient} program exits once all the specified files are
1608 finished (i.e., once you have typed @kbd{C-x #} in each server
1609 buffer).
1610
1611 @vindex server-kill-new-buffers
1612 @vindex server-temp-file-regexp
1613 Finishing with a server buffer also kills the buffer, unless it
1614 already existed in the Emacs session before the server was asked to
1615 create it. However, if you set @code{server-kill-new-buffers} to
1616 @code{nil}, then a different criterion is used: finishing with a
1617 server buffer kills it if the file name matches the regular expression
1618 @code{server-temp-file-regexp}. This is set up to distinguish certain
1619 ``temporary'' files.
1620
1621 Each @kbd{C-x #} checks for other pending external requests to edit
1622 various files, and selects the next such file. You can switch to a
1623 server buffer manually if you wish; you don't have to arrive at it
1624 with @kbd{C-x #}. But @kbd{C-x #} is the way to tell
1625 @command{emacsclient} that you are finished.
1626
1627 @vindex server-window
1628 If you set the value of the variable @code{server-window} to a
1629 window or a frame, @kbd{C-x #} always displays the next server buffer
1630 in that window or in that frame.
1631
1632 @node emacsclient Options
1633 @subsection @code{emacsclient} Options
1634 @cindex @code{emacsclient} options
1635
1636 You can pass some optional arguments to the @command{emacsclient}
1637 program, such as:
1638
1639 @example
1640 emacsclient -c +12 @var{file1} +4:3 @var{file2}
1641 @end example
1642
1643 @noindent
1644 The @samp{+@var{line}} or @samp{+@var{line}:@var{column}} arguments
1645 specify line numbers, or line and column numbers, for the next file
1646 argument. These behave like the command line arguments for Emacs
1647 itself. @xref{Action Arguments}.
1648
1649 The other optional arguments recognized by @command{emacsclient} are
1650 listed below:
1651
1652 @table @samp
1653 @item -a @var{command}
1654 @itemx --alternate-editor=@var{command}
1655 Specify a command to run if @code{emacsclient} fails to contact Emacs.
1656 This is useful when running @code{emacsclient} in a script.
1657
1658 As a special exception, if @var{command} is the empty string, then
1659 @code{emacsclient} starts Emacs in daemon mode (as @command{emacs
1660 --daemon}) and then tries connecting again.
1661
1662 @cindex @env{ALTERNATE_EDITOR} environment variable
1663 The environment variable @env{ALTERNATE_EDITOR} has the same effect as
1664 the @samp{-a} option. If both are present, the latter takes
1665 precedence.
1666
1667 @cindex client frame
1668 @item -c
1669 Create a new graphical @dfn{client frame}, instead of using an
1670 existing Emacs frame. See below for the special behavior of @kbd{C-x
1671 C-c} in a client frame. If Emacs cannot create a new graphical frame
1672 (e.g., if it cannot connect to the X server), it tries to create a
1673 text terminal client frame, as though you had supplied the @samp{-t}
1674 option instead.
1675
1676 On MS-Windows, a single Emacs session cannot display frames on both
1677 graphical and text terminals, nor on multiple text terminals. Thus,
1678 if the Emacs server is running on a text terminal, the @samp{-c}
1679 option, like the @samp{-t} option, creates a new frame in the server's
1680 current text terminal. @xref{Windows Startup}.
1681
1682 If you omit a filename argument while supplying the @samp{-c} option,
1683 the new frame displays the @file{*scratch*} buffer by default. You
1684 can customize this behavior with the variable @code{initial-buffer-choice}
1685 (@pxref{Entering Emacs}).
1686
1687 @item -F @var{alist}
1688 @itemx --frame-parameters=@var{alist}
1689 Set the parameters for a newly-created graphical frame
1690 (@pxref{Frame Parameters}).
1691
1692 @item -d @var{display}
1693 @itemx --display=@var{display}
1694 Tell Emacs to open the given files on the X display @var{display}
1695 (assuming there is more than one X display available).
1696
1697 @item -e
1698 @itemx --eval
1699 Tell Emacs to evaluate some Emacs Lisp code, instead of visiting some
1700 files. When this option is given, the arguments to
1701 @command{emacsclient} are interpreted as a list of expressions to
1702 evaluate, @emph{not} as a list of files to visit.
1703
1704 @item -f @var{server-file}
1705 @itemx --server-file=@var{server-file}
1706 @cindex @env{EMACS_SERVER_FILE} environment variable
1707 Specify a @dfn{server file} for connecting to an Emacs server via TCP.
1708
1709 An Emacs server usually uses an operating system feature called a
1710 ``local socket'' to listen for connections. Some operating systems,
1711 such as Microsoft Windows, do not support local sockets; in that case,
1712 the server communicates with @command{emacsclient} via TCP.
1713
1714 @vindex server-auth-dir
1715 @cindex server file
1716 @vindex server-port
1717 When you start a TCP Emacs server, Emacs creates a @dfn{server file}
1718 containing the TCP information to be used by @command{emacsclient} to
1719 connect to the server. The variable @code{server-auth-dir} specifies
1720 the directory containing the server file; by default, this is
1721 @file{~/.emacs.d/server/}. To tell @command{emacsclient} to connect
1722 to the server over TCP with a specific server file, use the @samp{-f}
1723 or @samp{--server-file} option, or set the @env{EMACS_SERVER_FILE}
1724 environment variable.
1725
1726 @item -n
1727 @itemx --no-wait
1728 Let @command{emacsclient} exit immediately, instead of waiting until
1729 all server buffers are finished. You can take as long as you like to
1730 edit the server buffers within Emacs, and they are @emph{not} killed
1731 when you type @kbd{C-x #} in them.
1732
1733 @item --parent-id @var{id}
1734 Open an @command{emacsclient} frame as a client frame in the parent X
1735 window with id @var{id}, via the XEmbed protocol. Currently, this
1736 option is mainly useful for developers.
1737
1738 @item -q
1739 @itemx --quiet
1740 Do not let @command{emacsclient} display messages about waiting for
1741 Emacs or connecting to remote server sockets.
1742
1743 @item -s @var{server-name}
1744 @itemx --socket-name=@var{server-name}
1745 Connect to the Emacs server named @var{server-name}. The server name
1746 is given by the variable @code{server-name} on the Emacs server. If
1747 this option is omitted, @command{emacsclient} connects to the first
1748 server it finds. (This option is not supported on MS-Windows.)
1749
1750 @item -t
1751 @itemx --tty
1752 @itemx -nw
1753 Create a new client frame on the current text terminal, instead of
1754 using an existing Emacs frame. This behaves just like the @samp{-c}
1755 option, described above, except that it creates a text terminal frame
1756 (@pxref{Non-Window Terminals}).
1757
1758 On MS-Windows, @samp{-t} behaves just like @samp{-c} if the Emacs
1759 server is using the graphical display, but if the Emacs server is
1760 running on a text terminal, it creates a new frame in the current text
1761 terminal.
1762 @end table
1763
1764 The new graphical or text terminal frames created by the @samp{-c}
1765 or @samp{-t} options are considered @dfn{client frames}. Any new
1766 frame that you create from a client frame is also considered a client
1767 frame. If you type @kbd{C-x C-c} (@code{save-buffers-kill-terminal})
1768 in a client frame, that command does not kill the Emacs session as it
1769 normally does (@pxref{Exiting}). Instead, Emacs deletes the client
1770 frame; furthermore, if the client frame has an @command{emacsclient}
1771 waiting to regain control (i.e., if you did not supply the @samp{-n}
1772 option), Emacs deletes all other frames of the same client, and marks
1773 the client's server buffers as finished, as though you had typed
1774 @kbd{C-x #} in all of them. If it so happens that there are no
1775 remaining frames after the client frame(s) are deleted, the Emacs
1776 session exits.
1777
1778 As an exception, when Emacs is started as a daemon, all frames are
1779 considered client frames, and @kbd{C-x C-c} never kills Emacs. To
1780 kill a daemon session, type @kbd{M-x kill-emacs}.
1781
1782 Note that the @samp{-t} and @samp{-n} options are contradictory:
1783 @samp{-t} says to take control of the current text terminal to create
1784 a new client frame, while @samp{-n} says not to take control of the
1785 text terminal. If you supply both options, Emacs visits the specified
1786 files(s) in an existing frame rather than a new client frame, negating
1787 the effect of @samp{-t}.
1788
1789 @node Printing
1790 @section Printing Hard Copies
1791 @cindex hardcopy
1792 @cindex printing
1793
1794 Emacs provides commands for printing hardcopies of either an entire
1795 buffer or part of one. You can invoke the printing commands directly,
1796 as detailed below, or using the @samp{File} menu on the menu bar.
1797
1798 @findex htmlfontify-buffer
1799 Aside from the commands described in this section, you can also
1800 print hardcopies from Dired (@pxref{Operating on Files}) and the diary
1801 (@pxref{Displaying the Diary}). You can also ``print'' an Emacs
1802 buffer to HTML with the command @kbd{M-x htmlfontify-buffer}, which
1803 converts the current buffer to a HTML file, replacing Emacs faces with
1804 CSS-based markup. Furthermore, Org mode allows you to ``print'' Org
1805 files to a variety of formats, such as PDF (@pxref{Org Mode}).
1806
1807 @table @kbd
1808 @item M-x print-buffer
1809 Print hardcopy of current buffer with page headings containing the
1810 file name and page number.
1811 @item M-x lpr-buffer
1812 Print hardcopy of current buffer without page headings.
1813 @item M-x print-region
1814 Like @code{print-buffer} but print only the current region.
1815 @item M-x lpr-region
1816 Like @code{lpr-buffer} but print only the current region.
1817 @end table
1818
1819 @findex print-buffer
1820 @findex print-region
1821 @findex lpr-buffer
1822 @findex lpr-region
1823 @vindex lpr-switches
1824 @vindex lpr-commands
1825 On most operating system, the above hardcopy commands submit files
1826 for printing by calling the @command{lpr} program. To change the
1827 printer program, customize the variable @code{lpr-command}. To
1828 specify extra switches to give the printer program, customize the list
1829 variable @code{lpr-switches}. Its value should be a list of option
1830 strings, each of which should start with @samp{-} (e.g., the option
1831 string @code{"-w80"} specifies a line width of 80 columns). The
1832 default is the empty list, @code{nil}.
1833
1834 @vindex printer-name
1835 @vindex lpr-printer-switch
1836 To specify the printer to use, set the variable @code{printer-name}.
1837 The default, @code{nil}, specifies the default printer. If you set it
1838 to a printer name (a string), that name is passed to @command{lpr}
1839 with the @samp{-P} switch; if you are not using @command{lpr}, you
1840 should specify the switch with @code{lpr-printer-switch}.
1841
1842 @vindex lpr-headers-switches
1843 @vindex lpr-add-switches
1844 The variable @code{lpr-headers-switches} similarly specifies the
1845 extra switches to use to make page headers. The variable
1846 @code{lpr-add-switches} controls whether to supply @samp{-T} and
1847 @samp{-J} options (suitable for @command{lpr}) to the printer program:
1848 @code{nil} means don't add them (this should be the value if your
1849 printer program is not compatible with @command{lpr}).
1850
1851 @menu
1852 * PostScript:: Printing buffers or regions as PostScript.
1853 * PostScript Variables:: Customizing the PostScript printing commands.
1854 * Printing Package:: An optional advanced printing interface.
1855 @end menu
1856
1857 @node PostScript
1858 @subsection PostScript Hardcopy
1859
1860 These commands convert buffer contents to PostScript,
1861 either printing it or leaving it in another Emacs buffer.
1862
1863 @table @kbd
1864 @item M-x ps-print-buffer
1865 Print hardcopy of the current buffer in PostScript form.
1866 @item M-x ps-print-region
1867 Print hardcopy of the current region in PostScript form.
1868 @item M-x ps-print-buffer-with-faces
1869 Print hardcopy of the current buffer in PostScript form, showing the
1870 faces used in the text by means of PostScript features.
1871 @item M-x ps-print-region-with-faces
1872 Print hardcopy of the current region in PostScript form, showing the
1873 faces used in the text.
1874 @item M-x ps-spool-buffer
1875 Generate and spool a PostScript image for the current buffer text.
1876 @item M-x ps-spool-region
1877 Generate and spool a PostScript image for the current region.
1878 @item M-x ps-spool-buffer-with-faces
1879 Generate and spool a PostScript image for the current buffer, showing the faces used.
1880 @item M-x ps-spool-region-with-faces
1881 Generate and spool a PostScript image for the current region, showing the faces used.
1882 @item M-x ps-despool
1883 Send the spooled PostScript to the printer.
1884 @item M-x handwrite
1885 Generate/print PostScript for the current buffer as if handwritten.
1886 @end table
1887
1888 @findex ps-print-region
1889 @findex ps-print-buffer
1890 @findex ps-print-region-with-faces
1891 @findex ps-print-buffer-with-faces
1892 The @code{ps-print-buffer} and @code{ps-print-region} commands print
1893 buffer contents in PostScript form. One command prints the entire
1894 buffer; the other, just the region. The commands
1895 @code{ps-print-buffer-with-faces} and
1896 @code{ps-print-region-with-faces} behave similarly, but use PostScript
1897 features to show the faces (fonts and colors) of the buffer text.
1898
1899 Interactively, when you use a prefix argument (@kbd{C-u}), the command
1900 prompts the user for a file name, and saves the PostScript image in that file
1901 instead of sending it to the printer.
1902
1903 @findex ps-spool-region
1904 @findex ps-spool-buffer
1905 @findex ps-spool-region-with-faces
1906 @findex ps-spool-buffer-with-faces
1907 The commands whose names have @samp{spool} instead of @samp{print},
1908 generate the PostScript output in an Emacs buffer instead of sending
1909 it to the printer.
1910
1911 @findex ps-despool
1912 Use the command @code{ps-despool} to send the spooled images to the
1913 printer. This command sends the PostScript generated by
1914 @samp{-spool-} commands (see commands above) to the printer. With a
1915 prefix argument (@kbd{C-u}), it prompts for a file name, and saves the
1916 spooled PostScript image in that file instead of sending it to the
1917 printer.
1918
1919 @findex handwrite
1920 @cindex handwriting
1921 @kbd{M-x handwrite} is more frivolous. It generates a PostScript
1922 rendition of the current buffer as a cursive handwritten document. It
1923 can be customized in group @code{handwrite}. This function only
1924 supports ISO 8859-1 characters.
1925
1926 @node PostScript Variables
1927 @subsection Variables for PostScript Hardcopy
1928
1929 @vindex ps-lpr-command
1930 @vindex ps-lpr-switches
1931 @vindex ps-printer-name
1932 All the PostScript hardcopy commands use the variables
1933 @code{ps-lpr-command} and @code{ps-lpr-switches} to specify how to print
1934 the output. @code{ps-lpr-command} specifies the command name to run,
1935 @code{ps-lpr-switches} specifies command line options to use, and
1936 @code{ps-printer-name} specifies the printer. If you don't set the
1937 first two variables yourself, they take their initial values from
1938 @code{lpr-command} and @code{lpr-switches}. If @code{ps-printer-name}
1939 is @code{nil}, @code{printer-name} is used.
1940
1941 @vindex ps-print-header
1942 The variable @code{ps-print-header} controls whether these commands
1943 add header lines to each page---set it to @code{nil} to turn headers
1944 off.
1945
1946 @cindex color emulation on black-and-white printers
1947 @vindex ps-print-color-p
1948 If your printer doesn't support colors, you should turn off color
1949 processing by setting @code{ps-print-color-p} to @code{nil}. By
1950 default, if the display supports colors, Emacs produces hardcopy output
1951 with color information; on black-and-white printers, colors are emulated
1952 with shades of gray. This might produce illegible output, even if your
1953 screen colors only use shades of gray.
1954
1955 Alternatively, you can set @code{ps-print-color-p} to @code{black-white} to
1956 print colors on black/white printers.
1957
1958 @vindex ps-use-face-background
1959 By default, PostScript printing ignores the background colors of the
1960 faces, unless the variable @code{ps-use-face-background} is
1961 non-@code{nil}. This is to avoid unwanted interference with the zebra
1962 stripes and background image/text.
1963
1964 @vindex ps-paper-type
1965 @vindex ps-page-dimensions-database
1966 The variable @code{ps-paper-type} specifies which size of paper to
1967 format for; legitimate values include @code{a4}, @code{a3},
1968 @code{a4small}, @code{b4}, @code{b5}, @code{executive}, @code{ledger},
1969 @code{legal}, @code{letter}, @code{letter-small}, @code{statement},
1970 @code{tabloid}. The default is @code{letter}. You can define
1971 additional paper sizes by changing the variable
1972 @code{ps-page-dimensions-database}.
1973
1974 @vindex ps-landscape-mode
1975 The variable @code{ps-landscape-mode} specifies the orientation of
1976 printing on the page. The default is @code{nil}, which stands for
1977 ``portrait'' mode. Any non-@code{nil} value specifies ``landscape''
1978 mode.
1979
1980 @vindex ps-number-of-columns
1981 The variable @code{ps-number-of-columns} specifies the number of
1982 columns; it takes effect in both landscape and portrait mode. The
1983 default is 1.
1984
1985 @vindex ps-font-family
1986 @vindex ps-font-size
1987 @vindex ps-font-info-database
1988 The variable @code{ps-font-family} specifies which font family to use
1989 for printing ordinary text. Legitimate values include @code{Courier},
1990 @code{Helvetica}, @code{NewCenturySchlbk}, @code{Palatino} and
1991 @code{Times}. The variable @code{ps-font-size} specifies the size of
1992 the font for ordinary text. It defaults to 8.5 points.
1993
1994 @vindex ps-multibyte-buffer
1995 @cindex Intlfonts for PostScript printing
1996 @cindex fonts for PostScript printing
1997 Emacs supports more scripts and characters than a typical PostScript
1998 printer. Thus, some of the characters in your buffer might not be
1999 printable using the fonts built into your printer. You can augment
2000 the fonts supplied with the printer with those from the GNU Intlfonts
2001 package, or you can instruct Emacs to use Intlfonts exclusively. The
2002 variable @code{ps-multibyte-buffer} controls this: the default value,
2003 @code{nil}, is appropriate for printing @acronym{ASCII} and Latin-1
2004 characters; a value of @code{non-latin-printer} is for printers which
2005 have the fonts for @acronym{ASCII}, Latin-1, Japanese, and Korean
2006 characters built into them. A value of @code{bdf-font} arranges for
2007 the BDF fonts from the Intlfonts package to be used for @emph{all}
2008 characters. Finally, a value of @code{bdf-font-except-latin}
2009 instructs the printer to use built-in fonts for @acronym{ASCII} and Latin-1
2010 characters, and Intlfonts BDF fonts for the rest.
2011
2012 @vindex bdf-directory-list
2013 To be able to use the BDF fonts, Emacs needs to know where to find
2014 them. The variable @code{bdf-directory-list} holds the list of
2015 directories where Emacs should look for the fonts; the default value
2016 includes a single directory @file{/usr/local/share/emacs/fonts/bdf}.
2017
2018 Many other customization variables for these commands are defined and
2019 described in the Lisp files @file{ps-print.el} and @file{ps-mule.el}.
2020
2021 @node Printing Package
2022 @subsection Printing Package
2023 @cindex Printing package
2024
2025 The basic Emacs facilities for printing hardcopy can be extended
2026 using the Printing package. This provides an easy-to-use interface
2027 for choosing what to print, previewing PostScript files before
2028 printing, and setting various printing options such as print headers,
2029 landscape or portrait modes, duplex modes, and so forth. On GNU/Linux
2030 or Unix systems, the Printing package relies on the @file{gs} and
2031 @file{gv} utilities, which are distributed as part of the GhostScript
2032 program. On MS-Windows, the @file{gstools} port of Ghostscript can be
2033 used.
2034
2035 @findex pr-interface
2036 To use the Printing package, add @code{(require 'printing)} to your
2037 init file (@pxref{Init File}), followed by @code{(pr-update-menus)}.
2038 This function replaces the usual printing commands in the menu bar
2039 with a @samp{Printing} submenu that contains various printing options.
2040 You can also type @kbd{M-x pr-interface @key{RET}}; this creates a
2041 @file{*Printing Interface*} buffer, similar to a customization buffer,
2042 where you can set the printing options. After selecting what and how
2043 to print, you start the print job using the @samp{Print} button (click
2044 @kbd{Mouse-2} on it, or move point over it and type @key{RET}). For
2045 further information on the various options, use the @samp{Interface
2046 Help} button.
2047
2048 @node Sorting
2049 @section Sorting Text
2050 @cindex sorting
2051
2052 Emacs provides several commands for sorting text in the buffer. All
2053 operate on the contents of the region.
2054 They divide the text of the region into many @dfn{sort records},
2055 identify a @dfn{sort key} for each record, and then reorder the records
2056 into the order determined by the sort keys. The records are ordered so
2057 that their keys are in alphabetical order, or, for numeric sorting, in
2058 numeric order. In alphabetic sorting, all upper-case letters @samp{A}
2059 through @samp{Z} come before lower-case @samp{a}, in accordance with the
2060 @acronym{ASCII} character sequence.
2061
2062 The various sort commands differ in how they divide the text into sort
2063 records and in which part of each record is used as the sort key. Most of
2064 the commands make each line a separate sort record, but some commands use
2065 paragraphs or pages as sort records. Most of the sort commands use each
2066 entire sort record as its own sort key, but some use only a portion of the
2067 record as the sort key.
2068
2069 @findex sort-lines
2070 @findex sort-paragraphs
2071 @findex sort-pages
2072 @findex sort-fields
2073 @findex sort-numeric-fields
2074 @vindex sort-numeric-base
2075 @table @kbd
2076 @item M-x sort-lines
2077 Divide the region into lines, and sort by comparing the entire
2078 text of a line. A numeric argument means sort into descending order.
2079
2080 @item M-x sort-paragraphs
2081 Divide the region into paragraphs, and sort by comparing the entire
2082 text of a paragraph (except for leading blank lines). A numeric
2083 argument means sort into descending order.
2084
2085 @item M-x sort-pages
2086 Divide the region into pages, and sort by comparing the entire
2087 text of a page (except for leading blank lines). A numeric
2088 argument means sort into descending order.
2089
2090 @item M-x sort-fields
2091 Divide the region into lines, and sort by comparing the contents of
2092 one field in each line. Fields are defined as separated by
2093 whitespace, so the first run of consecutive non-whitespace characters
2094 in a line constitutes field 1, the second such run constitutes field
2095 2, etc.
2096
2097 Specify which field to sort by with a numeric argument: 1 to sort by
2098 field 1, etc. A negative argument means count fields from the right
2099 instead of from the left; thus, minus 1 means sort by the last field.
2100 If several lines have identical contents in the field being sorted, they
2101 keep the same relative order that they had in the original buffer.
2102
2103 @item M-x sort-numeric-fields
2104 Like @kbd{M-x sort-fields} except the specified field is converted
2105 to an integer for each line, and the numbers are compared. @samp{10}
2106 comes before @samp{2} when considered as text, but after it when
2107 considered as a number. By default, numbers are interpreted according
2108 to @code{sort-numeric-base}, but numbers beginning with @samp{0x} or
2109 @samp{0} are interpreted as hexadecimal and octal, respectively.
2110
2111 @item M-x sort-columns
2112 Like @kbd{M-x sort-fields} except that the text within each line
2113 used for comparison comes from a fixed range of columns. See below
2114 for an explanation.
2115
2116 @findex reverse-region
2117 @item M-x reverse-region
2118 Reverse the order of the lines in the region. This is useful for
2119 sorting into descending order by fields or columns, since those sort
2120 commands do not have a feature for doing that.
2121 @end table
2122
2123 For example, if the buffer contains this:
2124
2125 @smallexample
2126 On systems where clash detection (locking of files being edited) is
2127 implemented, Emacs also checks the first time you modify a buffer
2128 whether the file has changed on disk since it was last visited or
2129 saved. If it has, you are asked to confirm that you want to change
2130 the buffer.
2131 @end smallexample
2132
2133 @noindent
2134 applying @kbd{M-x sort-lines} to the entire buffer produces this:
2135
2136 @smallexample
2137 On systems where clash detection (locking of files being edited) is
2138 implemented, Emacs also checks the first time you modify a buffer
2139 saved. If it has, you are asked to confirm that you want to change
2140 the buffer.
2141 whether the file has changed on disk since it was last visited or
2142 @end smallexample
2143
2144 @noindent
2145 where the upper-case @samp{O} sorts before all lower-case letters. If
2146 you use @kbd{C-u 2 M-x sort-fields} instead, you get this:
2147
2148 @smallexample
2149 implemented, Emacs also checks the first time you modify a buffer
2150 saved. If it has, you are asked to confirm that you want to change
2151 the buffer.
2152 On systems where clash detection (locking of files being edited) is
2153 whether the file has changed on disk since it was last visited or
2154 @end smallexample
2155
2156 @noindent
2157 where the sort keys were @samp{Emacs}, @samp{If}, @samp{buffer},
2158 @samp{systems} and @samp{the}.
2159
2160 @findex sort-columns
2161 @kbd{M-x sort-columns} requires more explanation. You specify the
2162 columns by putting point at one of the columns and the mark at the other
2163 column. Because this means you cannot put point or the mark at the
2164 beginning of the first line of the text you want to sort, this command
2165 uses an unusual definition of ``region'': all of the line point is in is
2166 considered part of the region, and so is all of the line the mark is in,
2167 as well as all the lines in between.
2168
2169 For example, to sort a table by information found in columns 10 to 15,
2170 you could put the mark on column 10 in the first line of the table, and
2171 point on column 15 in the last line of the table, and then run
2172 @code{sort-columns}. Equivalently, you could run it with the mark on
2173 column 15 in the first line and point on column 10 in the last line.
2174
2175 This can be thought of as sorting the rectangle specified by point and
2176 the mark, except that the text on each line to the left or right of the
2177 rectangle moves along with the text inside the rectangle.
2178 @xref{Rectangles}.
2179
2180 @vindex sort-fold-case
2181 Many of the sort commands ignore case differences when comparing, if
2182 @code{sort-fold-case} is non-@code{nil}.
2183
2184 @c Picture Mode documentation
2185 @ifnottex
2186 @include picture-xtra.texi
2187 @end ifnottex
2188
2189
2190 @node Editing Binary Files
2191 @section Editing Binary Files
2192
2193 @cindex Hexl mode
2194 @cindex mode, Hexl
2195 @cindex editing binary files
2196 @cindex hex editing
2197 There is a special major mode for editing binary files: Hexl mode. To
2198 use it, use @kbd{M-x hexl-find-file} instead of @kbd{C-x C-f} to visit
2199 the file. This command converts the file's contents to hexadecimal and
2200 lets you edit the translation. When you save the file, it is converted
2201 automatically back to binary.
2202
2203 You can also use @kbd{M-x hexl-mode} to translate an existing buffer
2204 into hex. This is useful if you visit a file normally and then discover
2205 it is a binary file.
2206
2207 Ordinary text characters overwrite in Hexl mode. This is to reduce
2208 the risk of accidentally spoiling the alignment of data in the file.
2209 There are special commands for insertion. Here is a list of the
2210 commands of Hexl mode:
2211
2212 @c I don't think individual index entries for these commands are useful--RMS.
2213 @table @kbd
2214 @item C-M-d
2215 Insert a byte with a code typed in decimal.
2216
2217 @item C-M-o
2218 Insert a byte with a code typed in octal.
2219
2220 @item C-M-x
2221 Insert a byte with a code typed in hex.
2222
2223 @item C-x [
2224 Move to the beginning of a 1k-byte ``page''.
2225
2226 @item C-x ]
2227 Move to the end of a 1k-byte ``page''.
2228
2229 @item M-g
2230 Move to an address specified in hex.
2231
2232 @item M-j
2233 Move to an address specified in decimal.
2234
2235 @item C-c C-c
2236 Leave Hexl mode, going back to the major mode this buffer had before you
2237 invoked @code{hexl-mode}.
2238 @end table
2239
2240 @noindent
2241 Other Hexl commands let you insert strings (sequences) of binary
2242 bytes, move by @code{short}s or @code{int}s, etc.; type @kbd{C-h a
2243 hexl-@key{RET}} for details.
2244
2245
2246 @node Saving Emacs Sessions
2247 @section Saving Emacs Sessions
2248 @cindex saving sessions
2249 @cindex restore session
2250 @cindex remember editing session
2251 @cindex reload files
2252 @cindex desktop
2253
2254 @vindex desktop-restore-frames
2255 Use the desktop library to save the state of Emacs from one session
2256 to another. Once you save the Emacs @dfn{desktop}---the buffers,
2257 their file names, major modes, buffer positions, and so on---then
2258 subsequent Emacs sessions reload the saved desktop. By default,
2259 the desktop also tries to save the frame and window configuration.
2260 To disable this, set @code{desktop-restore-frames} to @code{nil}.
2261 (See that variable's documentation for some related options
2262 that you can customize to fine-tune this behavior.)
2263
2264 @findex desktop-save
2265 @vindex desktop-save-mode
2266 You can save the desktop manually with the command @kbd{M-x
2267 desktop-save}. You can also enable automatic saving of the desktop
2268 when you exit Emacs, and automatic restoration of the last saved
2269 desktop when Emacs starts: use the Customization buffer (@pxref{Easy
2270 Customization}) to set @code{desktop-save-mode} to @code{t} for future
2271 sessions, or add this line in your init file (@pxref{Init File}):
2272
2273 @example
2274 (desktop-save-mode 1)
2275 @end example
2276
2277 @vindex desktop-auto-save-timeout
2278 @noindent
2279 When @code{desktop-save-mode} is active and the desktop file exists,
2280 Emacs auto-saves it every @code{desktop-auto-save-timeout}
2281 seconds, if that is non-@code{nil} and non-zero.
2282
2283 @findex desktop-change-dir
2284 @findex desktop-revert
2285 @vindex desktop-path
2286 If you turn on @code{desktop-save-mode} in your init file, then when
2287 Emacs starts, it looks for a saved desktop in the current directory.
2288 (More precisely, it looks in the directories specified by
2289 @var{desktop-path}, and uses the first desktop it finds.)
2290 Thus, you can have separate saved desktops in different directories,
2291 and the starting directory determines which one Emacs reloads. You
2292 can save the current desktop and reload one saved in another directory
2293 by typing @kbd{M-x desktop-change-dir}. Typing @kbd{M-x
2294 desktop-revert} reverts to the desktop previously reloaded.
2295
2296 Specify the option @samp{--no-desktop} on the command line when you
2297 don't want it to reload any saved desktop. This turns off
2298 @code{desktop-save-mode} for the current session. Starting Emacs with
2299 the @samp{--no-init-file} option also disables desktop reloading,
2300 since it bypasses the init file, where @code{desktop-save-mode} is
2301 usually turned on.
2302
2303 @vindex desktop-restore-eager
2304 By default, all the buffers in the desktop are restored at one go.
2305 However, this may be slow if there are a lot of buffers in the
2306 desktop. You can specify the maximum number of buffers to restore
2307 immediately with the variable @code{desktop-restore-eager}; the
2308 remaining buffers are restored ``lazily'', when Emacs is idle.
2309
2310 @findex desktop-clear
2311 @vindex desktop-globals-to-clear
2312 @vindex desktop-clear-preserve-buffers-regexp
2313 Type @kbd{M-x desktop-clear} to empty the Emacs desktop. This kills
2314 all buffers except for internal ones, and clears the global variables
2315 listed in @code{desktop-globals-to-clear}. If you want this to
2316 preserve certain buffers, customize the variable
2317 @code{desktop-clear-preserve-buffers-regexp}, whose value is a regular
2318 expression matching the names of buffers not to kill.
2319
2320 If you want to save minibuffer history from one session to
2321 another, use the @code{savehist} library.
2322
2323 @node Recursive Edit
2324 @section Recursive Editing Levels
2325 @cindex recursive editing level
2326 @cindex editing level, recursive
2327
2328 A @dfn{recursive edit} is a situation in which you are using Emacs
2329 commands to perform arbitrary editing while in the middle of another
2330 Emacs command. For example, when you type @kbd{C-r} inside of a
2331 @code{query-replace}, you enter a recursive edit in which you can change
2332 the current buffer. On exiting from the recursive edit, you go back to
2333 the @code{query-replace}. @xref{Query Replace}.
2334
2335 @kindex C-M-c
2336 @findex exit-recursive-edit
2337 @cindex exiting recursive edit
2338 @dfn{Exiting} the recursive edit means returning to the unfinished
2339 command, which continues execution. The command to exit is @kbd{C-M-c}
2340 (@code{exit-recursive-edit}).
2341
2342 You can also @dfn{abort} the recursive edit. This is like exiting,
2343 but also quits the unfinished command immediately. Use the command
2344 @kbd{C-]} (@code{abort-recursive-edit}) to do this. @xref{Quitting}.
2345
2346 The mode line shows you when you are in a recursive edit by displaying
2347 square brackets around the parentheses that always surround the major and
2348 minor mode names. Every window's mode line shows this in the same way,
2349 since being in a recursive edit is true of Emacs as a whole rather than
2350 any particular window or buffer.
2351
2352 It is possible to be in recursive edits within recursive edits. For
2353 example, after typing @kbd{C-r} in a @code{query-replace}, you may type a
2354 command that enters the debugger. This begins a recursive editing level
2355 for the debugger, within the recursive editing level for @kbd{C-r}.
2356 Mode lines display a pair of square brackets for each recursive editing
2357 level currently in progress.
2358
2359 Exiting the inner recursive edit (such as with the debugger @kbd{c}
2360 command) resumes the command running in the next level up. When that
2361 command finishes, you can then use @kbd{C-M-c} to exit another recursive
2362 editing level, and so on. Exiting applies to the innermost level only.
2363 Aborting also gets out of only one level of recursive edit; it returns
2364 immediately to the command level of the previous recursive edit. If you
2365 wish, you can then abort the next recursive editing level.
2366
2367 Alternatively, the command @kbd{M-x top-level} aborts all levels of
2368 recursive edits, returning immediately to the top-level command
2369 reader. It also exits the minibuffer, if it is active.
2370
2371 The text being edited inside the recursive edit need not be the same text
2372 that you were editing at top level. It depends on what the recursive edit
2373 is for. If the command that invokes the recursive edit selects a different
2374 buffer first, that is the buffer you will edit recursively. In any case,
2375 you can switch buffers within the recursive edit in the normal manner (as
2376 long as the buffer-switching keys have not been rebound). You could
2377 probably do all the rest of your editing inside the recursive edit,
2378 visiting files and all. But this could have surprising effects (such as
2379 stack overflow) from time to time. So remember to exit or abort the
2380 recursive edit when you no longer need it.
2381
2382 In general, we try to minimize the use of recursive editing levels in
2383 GNU Emacs. This is because they constrain you to ``go back'' in a
2384 particular order---from the innermost level toward the top level. When
2385 possible, we present different activities in separate buffers so that
2386 you can switch between them as you please. Some commands switch to a
2387 new major mode which provides a command to switch back. These
2388 approaches give you more flexibility to go back to unfinished tasks in
2389 the order you choose.
2390
2391 @ignore
2392 @c Apart from edt and viper, this is all obsolete.
2393 @c (Can't believe we were saying ``most other editors'' into 2014!)
2394 @c There seems no point having a node just for those, which both have
2395 @c their own manuals.
2396 @node Emulation
2397 @section Emulation
2398 @cindex emulating other editors
2399 @cindex other editors
2400 @cindex EDT
2401 @cindex vi
2402 @cindex WordStar
2403
2404 GNU Emacs can be programmed to emulate (more or less) most other
2405 editors. Standard facilities can emulate these:
2406
2407 @table @asis
2408 @item CRiSP/Brief (PC editor)
2409 @findex crisp-mode
2410 @vindex crisp-override-meta-x
2411 @findex scroll-all-mode
2412 @cindex CRiSP mode
2413 @cindex Brief emulation
2414 @cindex emulation of Brief
2415 @cindex mode, CRiSP
2416 @kbd{M-x crisp-mode} enables key bindings to emulate the CRiSP/Brief
2417 editor. Note that this rebinds @kbd{M-x} to exit Emacs unless you set
2418 the variable @code{crisp-override-meta-x}. You can also use the
2419 command @kbd{M-x scroll-all-mode} or set the variable
2420 @code{crisp-load-scroll-all} to emulate CRiSP's scroll-all feature
2421 (scrolling all windows together).
2422
2423 @item EDT (DEC VMS editor)
2424 @findex edt-emulation-on
2425 @findex edt-emulation-off
2426 Turn on EDT emulation with @kbd{M-x edt-emulation-on}; restore normal
2427 command bindings with @kbd{M-x edt-emulation-off}.
2428
2429 Most of the EDT emulation commands are keypad keys, and most standard
2430 Emacs key bindings are still available. The EDT emulation rebindings
2431 are done in the global keymap, so there is no problem switching
2432 buffers or major modes while in EDT emulation.
2433
2434 @item TPU (DEC VMS editor)
2435 @findex tpu-edt-on
2436 @cindex TPU
2437 @kbd{M-x tpu-edt-on} turns on emulation of the TPU editor emulating EDT.
2438
2439 @item vi (Berkeley editor)
2440 @findex viper-mode
2441 Viper is an emulator for vi. It implements several levels of
2442 emulation; level 1 is closest to vi itself, while level 5 departs
2443 somewhat from strict emulation to take advantage of the capabilities of
2444 Emacs. To invoke Viper, type @kbd{M-x viper-mode}; it will guide you
2445 the rest of the way and ask for the emulation level. @inforef{Top,
2446 Viper, viper}.
2447
2448 @item vi (another emulator)
2449 @findex vi-mode
2450 @kbd{M-x vi-mode} enters a major mode that replaces the previously
2451 established major mode. All of the vi commands that, in real vi, enter
2452 ``input'' mode are programmed instead to return to the previous major
2453 mode. Thus, ordinary Emacs serves as vi's ``input'' mode.
2454
2455 Because vi emulation works through major modes, it does not work
2456 to switch buffers during emulation. Return to normal Emacs first.
2457
2458 If you plan to use vi emulation much, you probably want to bind a key
2459 to the @code{vi-mode} command.
2460
2461 @item vi (alternate emulator)
2462 @findex vip-mode
2463 @kbd{M-x vip-mode} invokes another vi emulator, said to resemble real vi
2464 more thoroughly than @kbd{M-x vi-mode}. ``Input'' mode in this emulator
2465 is changed from ordinary Emacs so you can use @key{ESC} to go back to
2466 emulated vi command mode. To get from emulated vi command mode back to
2467 ordinary Emacs, type @kbd{C-z}.
2468
2469 This emulation does not work through major modes, and it is possible
2470 to switch buffers in various ways within the emulator. It is not
2471 so necessary to assign a key to the command @code{vip-mode} as
2472 it is with @code{vi-mode} because terminating insert mode does
2473 not use it.
2474
2475 @inforef{Top, VIP, vip}, for full information.
2476
2477 @item WordStar (old wordprocessor)
2478 @findex wordstar-mode
2479 @kbd{M-x wordstar-mode} provides a major mode with WordStar-like
2480 key bindings.
2481 @end table
2482 @end ignore
2483
2484
2485 @node Hyperlinking
2486 @section Hyperlinking and Navigation Features
2487
2488 The following subsections describe convenience features for handling
2489 URLs and other types of links occurring in Emacs buffer text.
2490
2491 @menu
2492 * Browse-URL:: Following URLs.
2493 * Goto Address mode:: Activating URLs.
2494 * FFAP:: Finding files etc. at point.
2495 @end menu
2496
2497 @node Browse-URL
2498 @subsection Following URLs
2499 @cindex World Wide Web
2500 @cindex Web
2501 @findex browse-url
2502 @findex browse-url-at-point
2503 @findex browse-url-at-mouse
2504 @cindex Browse-URL
2505 @cindex URLs
2506
2507 @table @kbd
2508 @item M-x browse-url @key{RET} @var{url} @key{RET}
2509 Load a URL into a Web browser.
2510 @end table
2511
2512 The Browse-URL package allows you to easily follow URLs from within
2513 Emacs. Most URLs are followed by invoking a web browser;
2514 @samp{mailto:} URLs are followed by invoking the @code{compose-mail}
2515 Emacs command to send mail to the specified address (@pxref{Sending
2516 Mail}).
2517
2518 The command @kbd{M-x browse-url} prompts for a URL, and follows it.
2519 If point is located near a plausible URL, that URL is offered as the
2520 default. The Browse-URL package also provides other commands which
2521 you might like to bind to keys, such as @code{browse-url-at-point} and
2522 @code{browse-url-at-mouse}.
2523
2524 @vindex browse-url-mailto-function
2525 @vindex browse-url-browser-function
2526 You can customize Browse-URL's behavior via various options in the
2527 @code{browse-url} Customize group. In particular, the option
2528 @code{browse-url-mailto-function} lets you define how to follow
2529 @samp{mailto:} URLs, while @code{browse-url-browser-function} lets you
2530 define how to follow other types of URLs. For more information, view
2531 the package commentary by typing @kbd{C-h P browse-url @key{RET}}.
2532
2533 @node Goto Address mode
2534 @subsection Activating URLs
2535 @findex goto-address-mode
2536 @cindex mode, Goto Address
2537 @cindex Goto Address mode
2538 @cindex URLs, activating
2539
2540 @table @kbd
2541 @item M-x goto-address-mode
2542 Activate URLs and e-mail addresses in the current buffer.
2543 @end table
2544
2545 @kindex C-c RET @r{(Goto Address mode)}
2546 @findex goto-address-at-point
2547 You can make Emacs mark out URLs specially in the current buffer, by
2548 typing @kbd{M-x goto-address-mode}. When this buffer-local minor mode
2549 is enabled, it finds all the URLs in the buffer, highlights them, and
2550 turns them into clickable buttons. You can follow the URL by typing
2551 @kbd{C-c @key{RET}} (@code{goto-address-at-point}) while point is on
2552 its text; or by clicking with @kbd{Mouse-2}, or by clicking
2553 @kbd{Mouse-1} quickly (@pxref{Mouse References}). Following a URL is
2554 done by calling @code{browse-url} as a subroutine
2555 (@pxref{Browse-URL}).
2556
2557 It can be useful to add @code{goto-address-mode} to mode hooks and
2558 hooks for displaying an incoming message
2559 (e.g., @code{rmail-show-message-hook} for Rmail, and
2560 @code{mh-show-mode-hook} for MH-E). This is not needed for Gnus,
2561 which has a similar feature of its own.
2562
2563 @node FFAP
2564 @subsection Finding Files and URLs at Point
2565 @findex find-file-at-point
2566 @findex ffap
2567 @findex dired-at-point
2568 @findex ffap-next
2569 @findex ffap-menu
2570 @cindex finding file at point
2571
2572 The FFAP package replaces certain key bindings for finding files,
2573 such as @kbd{C-x C-f}, with commands that provide more sensitive
2574 defaults. These commands behave like the ordinary ones when given a
2575 prefix argument. Otherwise, they get the default file name or URL
2576 from the text around point. If what is found in the buffer has the
2577 form of a URL rather than a file name, the commands use
2578 @code{browse-url} to view it (@pxref{Browse-URL}).
2579
2580 This feature is useful for following references in mail or news
2581 buffers, @file{README} files, @file{MANIFEST} files, and so on. For
2582 more information, view the package commentary by typing @kbd{C-h P
2583 ffap @key{RET}}.
2584
2585 @cindex FFAP minor mode
2586 @findex ffap-mode
2587 To enable FFAP, type @kbd{M-x ffap-bindings}. This makes the
2588 following key bindings, and also installs hooks for additional FFAP
2589 functionality in Rmail, Gnus and VM article buffers.
2590
2591 @table @kbd
2592 @item C-x C-f @var{filename} @key{RET}
2593 @kindex C-x C-f @r{(FFAP)}
2594 Find @var{filename}, guessing a default from text around point
2595 (@code{find-file-at-point}).
2596 @item C-x C-r
2597 @kindex C-x C-r @r{(FFAP)}
2598 @code{ffap-read-only}, analogous to @code{find-file-read-only}.
2599 @item C-x C-v
2600 @kindex C-x C-v @r{(FFAP)}
2601 @code{ffap-alternate-file}, analogous to @code{find-alternate-file}.
2602 @item C-x d @var{directory} @key{RET}
2603 @kindex C-x d @r{(FFAP)}
2604 Start Dired on @var{directory}, defaulting to the directory name at
2605 point (@code{dired-at-point}).
2606 @item C-x C-d
2607 @code{ffap-list-directory}, analogous to @code{list-directory}.
2608 @item C-x 4 f
2609 @kindex C-x 4 f @r{(FFAP)}
2610 @code{ffap-other-window}, analogous to @code{find-file-other-window}.
2611 @item C-x 4 r
2612 @code{ffap-read-only-other-window}, analogous to
2613 @code{find-file-read-only-other-window}.
2614 @item C-x 4 d
2615 @code{ffap-dired-other-window}, like @code{dired-other-window}.
2616 @item C-x 5 f
2617 @kindex C-x 5 f @r{(FFAP)}
2618 @code{ffap-other-frame}, analogous to @code{find-file-other-frame}.
2619 @item C-x 5 r
2620 @code{ffap-read-only-other-frame}, analogous to
2621 @code{find-file-read-only-other-frame}.
2622 @item C-x 5 d
2623 @code{ffap-dired-other-frame}, analogous to @code{dired-other-frame}.
2624 @item M-x ffap-next
2625 Search buffer for next file name or URL, then find that file or URL.
2626 @item S-Mouse-3
2627 @kindex S-Mouse-3 @r{(FFAP)}
2628 @code{ffap-at-mouse} finds the file guessed from text around the position
2629 of a mouse click.
2630 @item C-S-Mouse-3
2631 @kindex C-S-Mouse-3 @r{(FFAP)}
2632 Display a menu of files and URLs mentioned in current buffer, then
2633 find the one you select (@code{ffap-menu}).
2634 @end table
2635
2636 @node Amusements
2637 @section Other Amusements
2638 @cindex boredom
2639
2640 @findex animate-birthday-present
2641 @cindex animate
2642 The @code{animate} package makes text dance (e.g., @kbd{M-x
2643 animate-birthday-present}).
2644
2645 @findex blackbox
2646 @findex mpuz
2647 @findex 5x5
2648 @cindex puzzles
2649 @kbd{M-x blackbox}, @kbd{M-x mpuz} and @kbd{M-x 5x5} are puzzles.
2650 @code{blackbox} challenges you to determine the location of objects
2651 inside a box by tomography. @code{mpuz} displays a multiplication
2652 puzzle with letters standing for digits in a code that you must
2653 guess---to guess a value, type a letter and then the digit you think it
2654 stands for. The aim of @code{5x5} is to fill in all the squares.
2655
2656 @findex bubbles
2657 @kbd{M-x bubbles} is a game in which the object is to remove as many
2658 bubbles as you can in the smallest number of moves.
2659
2660 @findex decipher
2661 @cindex ciphers
2662 @cindex cryptanalysis
2663 @kbd{M-x decipher} helps you to cryptanalyze a buffer which is
2664 encrypted in a simple monoalphabetic substitution cipher.
2665
2666 @findex dissociated-press
2667 @kbd{M-x dissociated-press} scrambles the text in the current Emacs
2668 buffer, word by word or character by character, writing its output to
2669 a buffer named @file{*Dissociation*}. A positive argument tells it to
2670 operate character by character, and specifies the number of overlap
2671 characters. A negative argument tells it to operate word by word, and
2672 specifies the number of overlap words. Dissociated Press produces
2673 results fairly like those of a Markov chain, but is however, an
2674 independent, ignoriginal invention; it techniquitously copies several
2675 consecutive characters from the sample text between random jumps,
2676 unlike a Markov chain which would jump randomly after each word or
2677 character. Keep dissociwords out of your documentation, if you want
2678 it to be well userenced and properbose.
2679
2680 @findex dunnet
2681 @kbd{M-x dunnet} runs an text-based adventure game.
2682
2683 @findex gomoku
2684 @cindex Go Moku
2685 If you want a little more personal involvement, try @kbd{M-x gomoku},
2686 which plays the game Go Moku with you.
2687
2688 @cindex tower of Hanoi
2689 @findex hanoi
2690 If you are a little bit bored, you can try @kbd{M-x hanoi}. If you are
2691 considerably bored, give it a numeric argument. If you are very, very
2692 bored, try an argument of 9. Sit back and watch.
2693
2694 @findex life
2695 @cindex Life
2696 @kbd{M-x life} runs Conway's ``Life'' cellular automaton.
2697
2698 @findex landmark
2699 @cindex landmark game
2700 @kbd{M-x landmark} runs a relatively non-participatory game in which
2701 a robot attempts to maneuver towards a tree at the center of the
2702 window based on unique olfactory cues from each of the four
2703 directions.
2704
2705 @findex morse-region
2706 @findex unmorse-region
2707 @findex nato-region
2708 @cindex Morse code
2709 @cindex --/---/.-./.../.
2710 @kbd{M-x morse-region} converts the text in the region to Morse
2711 code; @kbd{M-x unmorse-region} converts it back. @kbd{M-x
2712 nato-region} converts the text in the region to NATO phonetic
2713 alphabet; @kbd{M-x denato-region} converts it back.
2714
2715 @findex pong
2716 @cindex Pong game
2717 @findex tetris
2718 @cindex Tetris
2719 @findex snake
2720 @cindex Snake
2721 @kbd{M-x pong}, @kbd{M-x snake} and @kbd{M-x tetris} are
2722 implementations of the well-known Pong, Snake and Tetris games.
2723
2724 @findex solitaire
2725 @cindex solitaire
2726 @kbd{M-x solitaire} plays a game of solitaire in which you jump pegs
2727 across other pegs.
2728
2729 @findex zone
2730 The command @kbd{M-x zone} plays games with the display when Emacs
2731 is idle.
2732
2733 @findex doctor
2734 @cindex Eliza
2735 Finally, if you find yourself frustrated, try describing your
2736 problems to the famous psychotherapist Eliza. Just do @kbd{M-x
2737 doctor}. End each input by typing @key{RET} twice.
2738
2739 @ifnottex
2740 @lowersections
2741 @end ifnottex