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