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