TUTORIAL
1 Emacs tutorial. See end for copying conditions. 2 3 Emacs commands generally involve the CONTROL key (sometimes labeled 4 CTRL or CTL) or the META key (sometimes labeled EDIT or ALT). Rather than 5 write that in full each time, we'll use the following abbreviations: 6 7 C-<chr> means hold the CONTROL key while typing the character <chr> 8 Thus, C-f would be: hold the CONTROL key and type f. 9 M-<chr> means hold the META or EDIT or ALT key down while typing <chr>. 10 If there is no META, EDIT or ALT key, instead press and release the 11 ESC key and then type <chr>. We write <ESC> for the ESC key. 12 13 Important note: to end the Emacs session, type C-x C-c. (Two characters.) 14 The characters ">>" at the left margin indicate directions for you to 15 try using a command. For instance: 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 [Middle of page left blank for didactic purposes. Text continues below] 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 >> Now type C-v (View next screen) to move to the next screen. 54 (go ahead, do it by holding down the CONTROL key while typing v). 55 From now on, you should do this again whenever you finish 56 reading the screen. 57 58 Note that there is an overlap of two lines when you move from screen 59 to screen; this provides some continuity so you can continue reading 60 the text. 61 62 The first thing that you need to know is how to move around from place 63 to place in the text. You already know how to move forward one screen, 64 with C-v. To move backwards one screen, type M-v (hold down the META key 65 and type v, or type <ESC>v if you do not have a META, EDIT, or ALT key). 66 67 >> Try typing M-v and then C-v, a few times. 68 69 70 * SUMMARY 71 --------- 72 73 The following commands are useful for viewing screenfuls: 74 75 C-v Move forward one screenful 76 M-v Move backward one screenful 77 C-l Clear screen and redisplay all the text, 78 moving the text around the cursor 79 to the center of the screen. 80 (That's CONTROL-L, not CONTROL-1.) 81 82 >> Find the cursor, and note what text is near it. 83 Then type C-l. 84 Find the cursor again and notice that the same text 85 is near the cursor now. 86 87 You can also use the PageUp and PageDn keys to move by screenfuls, if 88 your terminal has them, but you can edit more efficiently if you use 89 C-v and M-v. 90 91 92 * BASIC CURSOR CONTROL 93 ---------------------- 94 95 Moving from screenful to screenful is useful, but how do you 96 move to a specific place within the text on the screen? 97 98 There are several ways you can do this. You can use the arrow keys, 99 but it's more efficient to keep your hands in the standard position 100 and use the commands C-p, C-b, C-f, and C-n. These characters 101 are equivalent to the four arrow keys, like this: 102 103 Previous line, C-p 104 : 105 : 106 Backward, C-b .... Current cursor position .... Forward, C-f 107 : 108 : 109 Next line, C-n 110 111 >> Move the cursor to the line in the middle of that diagram 112 using C-n or C-p. Then type C-l to see the whole diagram 113 centered in the screen. 114 115 You'll find it easy to remember these letters by words they stand for: 116 P for previous, N for next, B for backward and F for forward. You 117 will be using these basic cursor positioning commands all the time. 118 119 >> Do a few C-n's to bring the cursor down to this line. 120 121 >> Move into the line with C-f's and then up with C-p's. 122 See what C-p does when the cursor is in the middle of the line. 123 124 Each line of text ends with a Newline character, which serves to 125 separate it from the following line. The last line in your file ought 126 to have a Newline at the end (but Emacs does not require it to have 127 one). 128 129 >> Try to C-b at the beginning of a line. It should move to 130 the end of the previous line. This is because it moves back 131 across the Newline character. 132 133 C-f can move across a Newline just like C-b. 134 135 >> Do a few more C-b's, so you get a feel for where the cursor is. 136 Then do C-f's to return to the end of the line. 137 Then do one more C-f to move to the following line. 138 139 When you move past the top or bottom of the screen, the text beyond 140 the edge shifts onto the screen. This is called "scrolling". It 141 enables Emacs to move the cursor to the specified place in the text 142 without moving it off the screen. 143 144 >> Try to move the cursor off the bottom of the screen with C-n, and 145 see what happens. 146 147 If moving by characters is too slow, you can move by words. M-f 148 (META-f) moves forward a word and M-b moves back a word. 149 150 >> Type a few M-f's and M-b's. 151 152 When you are in the middle of a word, M-f moves to the end of the word. 153 When you are in whitespace between words, M-f moves to the end of the 154 following word. M-b works likewise in the opposite direction. 155 156 >> Type M-f and M-b a few times, interspersed with C-f's and C-b's 157 so that you can observe the action of M-f and M-b from various 158 places inside and between words. 159 160 Notice the parallel between C-f and C-b on the one hand, and M-f and 161 M-b on the other hand. Very often Meta characters are used for 162 operations related to the units defined by language (words, sentences, 163 paragraphs), while Control characters operate on basic units that are 164 independent of what you are editing (characters, lines, etc). 165 166 This parallel applies between lines and sentences: C-a and C-e move to 167 the beginning or end of a line, and M-a and M-e move to the beginning 168 or end of a sentence. 169 170 >> Try a couple of C-a's, and then a couple of C-e's. 171 Try a couple of M-a's, and then a couple of M-e's. 172 173 See how repeated C-a's do nothing, but repeated M-a's keep moving one 174 more sentence. Although these are not quite analogous, each one seems 175 natural. 176 177 The location of the cursor in the text is also called "point". To 178 paraphrase, the cursor shows on the screen where point is located in 179 the text. 180 181 Here is a summary of simple cursor-moving operations, including the 182 word and sentence moving commands: 183 184 C-f Move forward a character 185 C-b Move backward a character 186 187 M-f Move forward a word 188 M-b Move backward a word 189 190 C-n Move to next line 191 C-p Move to previous line 192 193 C-a Move to beginning of line 194 C-e Move to end of line 195 196 M-a Move back to beginning of sentence 197 M-e Move forward to end of sentence 198 199 >> Try all of these commands now a few times for practice. 200 These are the most often used commands. 201 202 Two other important cursor motion commands are M-< (META Less-than), 203 which moves to the beginning of the whole text, and M-> (META 204 Greater-than), which moves to the end of the whole text. 205 206 On most terminals, the "<" is above the comma, so you must use the 207 shift key to type it. On these terminals you must use the shift key 208 to type M-< also; without the shift key, you would be typing M-comma. 209 210 >> Try M-< now, to move to the beginning of the tutorial. 211 Then use C-v repeatedly to move back here. 212 213 >> Try M-> now, to move to the end of the tutorial. 214 Then use M-v repeatedly to move back here. 215 216 You can also move the cursor with the arrow keys, if your terminal has 217 arrow keys. We recommend learning C-b, C-f, C-n and C-p for three 218 reasons. First, they work on all kinds of terminals. Second, once 219 you gain practice at using Emacs, you will find that typing these Control 220 characters is faster than typing the arrow keys (because you do not 221 have to move your hands away from touch-typing position). Third, once 222 you form the habit of using these Control character commands, you can 223 easily learn to use other advanced cursor motion commands as well. 224 225 Most Emacs commands accept a numeric argument; for most commands, this 226 serves as a repeat-count. The way you give a command a repeat count 227 is by typing C-u and then the digits before you type the command. If 228 you have a META (or EDIT or ALT) key, there is another, alternative way 229 to enter a numeric argument: type the digits while holding down the 230 META key. We recommend learning the C-u method because it works on 231 any terminal. The numeric argument is also called a "prefix argument", 232 because you type the argument before the command it applies to. 233 234 For instance, C-u 8 C-f moves forward eight characters. 235 236 >> Try using C-n or C-p with a numeric argument, to move the cursor 237 to a line near this one with just one command. 238 239 Most commands use the numeric argument as a repeat count, but some 240 commands use it in some other way. Several commands (but none of 241 those you have learned so far) use it as a flag--the presence of a 242 prefix argument, regardless of its value, makes the command do 243 something different. 244 245 C-v and M-v are another kind of exception. When given an argument, 246 they scroll the screen up or down by that many lines, rather than by a 247 screenful. For example, C-u 8 C-v scrolls the screen by 8 lines. 248 249 >> Try typing C-u 8 C-v now. 250 251 This should have scrolled the screen up by 8 lines. If you would like 252 to scroll it down again, you can give an argument to M-v. 253 254 If you are using a windowed display, such as X11 or MS-Windows, there 255 should be a tall rectangular area called a scroll bar at the 256 side of the Emacs window. You can scroll the text by clicking the 257 mouse in the scroll bar. 258 259 >> Try pressing the middle button at the top of the highlighted area 260 within the scroll bar. This should scroll the text to a position 261 determined by how high or low you click. 262 263 >> Try moving the mouse up and down, while holding the middle button 264 pressed down. You'll see that the text scrolls up and down as 265 you move the mouse. 266 267 268 * WHEN EMACS IS HUNG 269 -------------------- 270 271 If Emacs stops responding to your commands, you can stop it safely by 272 typing C-g. You can use C-g to stop a command which is taking too 273 long to execute. 274 275 You can also use C-g to discard a numeric argument or the beginning of 276 a command that you do not want to finish. 277 278 >> Type C-u 100 to make a numeric arg of 100, then type C-g. 279 Now type C-f. It should move just one character, 280 because you canceled the argument with C-g. 281 282 If you have typed an <ESC> by mistake, you can get rid of it 283 with a C-g. 284 285 286 * DISABLED COMMANDS 287 ------------------- 288 289 Some Emacs commands are "disabled" so that beginning users cannot use 290 them by accident. 291 292 If you type one of the disabled commands, Emacs displays a message 293 saying what the command was, and asking you whether you want to go 294 ahead and execute the command. 295 296 If you really want to try the command, type <SPC> (the Space bar) in 297 answer to the question. Normally, if you do not want to execute the 298 disabled command, answer the question with "n". 299 300 >> Type C-x C-l (which is a disabled command), 301 then type n to answer the question. 302 303 304 * WINDOWS 305 --------- 306 307 Emacs can have several windows, each displaying its own text. We will 308 explain later on how to use multiple windows. Right now we want to 309 explain how to get rid of extra windows and go back to basic 310 one-window editing. It is simple: 311 312 C-x 1 One window (i.e., kill all other windows). 313 314 That is CONTROL-x followed by the digit 1. C-x 1 expands the window 315 which contains the cursor, to occupy the full screen. It deletes all 316 other windows. 317 318 >> Move the cursor to this line and type C-u 0 C-l. 319 >> Type CONTROL-h k CONTROL-f. 320 See how this window shrinks, while a new one appears 321 to display documentation on the CONTROL-f command. 322 323 >> Type C-x 1 and see the documentation listing window disappear. 324 325 This command is unlike the other commands you have learned in that it 326 consists of two characters. It starts with the character CONTROL-x. 327 There is a whole series of commands that start with CONTROL-x; many of 328 them have to do with windows, files, buffers, and related things. 329 These commands are two, three or four characters long. 330 331 332 * INSERTING AND DELETING 333 ------------------------ 334 335 If you want to insert text, just type the text. Characters which you 336 can see, such as A, 7, *, etc. are taken by Emacs as text and inserted 337 immediately. Type <Return> (the carriage-return key) to insert a 338 Newline character. 339 340 You can delete the last character you typed by typing <Delback>. 341 <Delback> is a key on the keyboard--the same one you normally use, 342 outside Emacs, for deleting the last character you typed. It is 343 normally a large key a couple of lines up from the <Return> key, and 344 it is usually labeled "Delete", "Del" or "Backspace". 345 346 If the large key there is labeled "Backspace", then that's the one you 347 use for <Delback>. There may also be another key labeled "Delete" 348 somewhere else, but that's not <Delback>. 349 350 More generally, <Delback> deletes the character immediately before the 351 current cursor position. 352 353 >> Do this now--type a few characters, then delete them 354 by typing <Delback> a few times. Don't worry about this file 355 being changed; you will not alter the master tutorial. This is 356 your personal copy of it. 357 358 When a line of text gets too big for one line on the screen, the line 359 of text is "continued" onto a second screen line. A backslash ("\") 360 (or, if you're using a windowed display, a little curved arrow) at the 361 right margin indicates a line which has been continued. 362 363 >> Insert text until you reach the right margin, and keep on inserting. 364 You'll see a continuation line appear. 365 366 >> Use <Delback>s to delete the text until the line fits on one screen 367 line again. The continuation line goes away. 368 369 You can delete a Newline character just like any other character. 370 Deleting the Newline character between two lines merges them into 371 one line. If the resulting combined line is too long to fit in the 372 screen width, it will be displayed with a continuation line. 373 374 >> Move the cursor to the beginning of a line and type <Delback>. This 375 merges that line with the previous line. 376 377 >> Type <Return> to reinsert the Newline you deleted. 378 379 Remember that most Emacs commands can be given a repeat count; 380 this includes text characters. Repeating a text character inserts 381 it several times. 382 383 >> Try that now -- type C-u 8 * to insert ********. 384 385 You've now learned the most basic way of typing something in 386 Emacs and correcting errors. You can delete by words or lines 387 as well. Here is a summary of the delete operations: 388 389 <Delback> Delete the character just before the cursor 390 C-d Delete the next character after the cursor 391 392 M-<Delback> Kill the word immediately before the cursor 393 M-d Kill the next word after the cursor 394 395 C-k Kill from the cursor position to end of line 396 M-k Kill to the end of the current sentence 397 398 Notice that <Delback> and C-d vs M-<Delback> and M-d extend the parallel 399 started by C-f and M-f (well, <Delback> is not really a control 400 character, but let's not worry about that). C-k and M-k are like C-e 401 and M-e, sort of, in that lines are opposite sentences. 402 403 You can also kill any part of the text with one uniform method. Move 404 to one end of that part, and type C-@ or C-<SPC> (either one). (<SPC> 405 is the Space bar.) Move to the other end of that part, and type C-w. 406 That kills all the text between the two positions. 407 408 >> Move the cursor to the Y at the start of the previous paragraph. 409 >> Type C-<SPC>. Emacs should display a message "Mark set" 410 at the bottom of the screen. 411 >> Move the cursor to the n in "end", on the second line of the 412 paragraph. 413 >> Type C-w. This will kill the text starting from the Y, 414 and ending just before the n. 415 416 The difference between "killing" and "deleting" is that "killed" text 417 can be reinserted, whereas "deleted" things cannot be reinserted. 418 Reinsertion of killed text is called "yanking". Generally, the 419 commands that can remove a lot of text kill the text (they set up so 420 that you can yank the text), while the commands that remove just one 421 character, or only remove blank lines and spaces, do deletion (so you 422 cannot yank that text). <Delback> and C-d do deletion in the simplest 423 case, with no argument. When given an argument, they kill instead. 424 425 >> Move the cursor to the beginning of a line which is not empty. 426 Then type C-k to kill the text on that line. 427 >> Type C-k a second time. You'll see that it kills the Newline 428 which follows that line. 429 430 Note that a single C-k kills the contents of the line, and a second 431 C-k kills the line itself, and makes all the other lines move up. C-k 432 treats a numeric argument specially: it kills that many lines AND 433 their contents. This is not mere repetition. C-u 2 C-k kills two 434 lines and their newlines; typing C-k twice would not do that. 435 436 Bringing back killed text is called "yanking". (Think of it as 437 yanking back, or pulling back, some text that was taken away.) You 438 can yank the killed text either at the same place where it was killed, 439 or at some other place in the text you are editing, or even in a 440 different file. You can yank the same text several times; that makes 441 multiple copies of it. 442 443 The command for yanking is C-y. It reinserts the last killed text, 444 at the current cursor position. 445 446 >> Try it; type C-y to yank the text back. 447 448 If you do several C-k's in a row, all of the killed text is saved 449 together, so that one C-y will yank all of the lines at once. 450 451 >> Do this now, type C-k several times. 452 453 Now to retrieve that killed text: 454 455 >> Type C-y. Then move the cursor down a few lines and type C-y 456 again. You now see how to copy some text. 457 458 What do you do if you have some text you want to yank back, and then 459 you kill something else? C-y would yank the more recent kill. But 460 the previous text is not lost. You can get back to it using the M-y 461 command. After you have done C-y to get the most recent kill, typing 462 M-y replaces that yanked text with the previous kill. Typing M-y 463 again and again brings in earlier and earlier kills. When you have 464 reached the text you are looking for, you do not have to do anything to 465 keep it. Just go on with your editing, leaving the yanked text where 466 it is. 467 468 If you M-y enough times, you come back to the starting point (the most 469 recent kill). 470 471 >> Kill a line, move around, kill another line. 472 Then do C-y to get back the second killed line. 473 Then do M-y and it will be replaced by the first killed line. 474 Do more M-y's and see what you get. Keep doing them until 475 the second kill line comes back, and then a few more. 476 If you like, you can try giving M-y positive and negative 477 arguments. 478 479 480 * UNDO 481 ------ 482 483 If you make a change to the text, and then decide that it was a 484 mistake, you can undo the change with the undo command, C-x u. 485 486 Normally, C-x u undoes the changes made by one command; if you repeat 487 the C-x u several times in a row, each repetition undoes one 488 additional command. 489 490 But there are two exceptions: commands that do not change the text do 491 not count (this includes cursor motion commands and scrolling 492 command), and self-inserting characters are usually handled in groups 493 of up to 20. (This is to reduce the number of C-x u's you have to 494 type to undo insertion of text.) 495 496 >> Kill this line with C-k, then type C-x u and it should reappear. 497 498 C-_ is an alternative undo command; it works just the same as C-x u, 499 but it is easier to type several times in a row. The disadvantage of 500 C-_ is that on some keyboards it is not obvious how to type it. That 501 is why we provide C-x u as well. On some terminals, you can type C-_ 502 by typing / while holding down CONTROL. 503 504 A numeric argument to C-_ or C-x u acts as a repeat count. 505 506 You can undo deletion of text just as you can undo killing of text. 507 The distinction between killing something and deleting it affects 508 whether you can yank it with C-y; it makes no difference for undo. 509 510 511 * FILES 512 ------- 513 514 In order to make the text you edit permanent, you must put it in a 515 file. Otherwise, it will go away when your invocation of Emacs goes 516 away. In order to put your text in a file, you must "find" the file 517 before you enter the text. (This is also called "visiting" the file.) 518 519 Finding a file means that you see the contents of the file within 520 Emacs. In many ways, it is as if you were editing the file itself. 521 However, the changes you make using Emacs do not become permanent 522 until you "save" the file. This is so you can avoid leaving a 523 half-changed file on the system when you do not want to. Even when 524 you save, Emacs leaves the original file under a changed name in case 525 you later decide that your changes were a mistake. 526 527 If you look near the bottom of the screen you will see a line that 528 begins and ends with dashes, and starts with "--:-- TUTORIAL" or 529 something like that. This part of the screen normally shows the name 530 of the file that you are visiting. Right now, you are visiting a file 531 called "TUTORIAL" which is your personal scratch copy of the Emacs 532 tutorial. When you find a file with Emacs, that file's name will 533 appear in that precise spot. 534 535 One special thing about the command for finding a file is that you 536 have to say what file name you want. We say the command "reads an 537 argument from the terminal" (in this case, the argument is the name of 538 the file). After you type the command 539 540 C-x C-f Find a file 541 542 Emacs asks you to type the file name. The file name you type appears 543 on the bottom line of the screen. The bottom line is called the 544 minibuffer when it is used for this sort of input. You can use 545 ordinary Emacs editing commands to edit the file name. 546 547 While you are entering the file name (or any minibuffer input), 548 you can cancel the command with C-g. 549 550 >> Type C-x C-f, then type C-g. This cancels the minibuffer, 551 and also cancels the C-x C-f command that was using the 552 minibuffer. So you do not find any file. 553 554 When you have finished entering the file name, type <Return> to 555 terminate it. Then C-x C-f command goes to work, and finds the file 556 you chose. The minibuffer disappears when the C-x C-f command is 557 finished. 558 559 In a little while the file contents appear on the screen, and you can 560 edit the contents. When you wish to make your changes permanent, 561 type the command 562 563 C-x C-s Save the file 564 565 This copies the text within Emacs into the file. The first time you 566 do this, Emacs renames the original file to a new name so that it is 567 not lost. The new name is made by adding "~" to the end of the 568 original file's name. 569 570 When saving is finished, Emacs displays the name of the file written. 571 You should save fairly often, so that you will not lose very much 572 work if the system should crash. 573 574 >> Type C-x C-s, saving your copy of the tutorial. 575 This should show "Wrote ...TUTORIAL" at the bottom of the screen. 576 577 NOTE: On some systems, typing C-x C-s will freeze the screen and you 578 will see no further output from Emacs. This indicates that an 579 operating system "feature" called "flow control" is intercepting the 580 C-s and not letting it get through to Emacs. To unfreeze the screen, 581 type C-q. Then see the section "Spontaneous Entry to Incremental 582 Search" in the Emacs manual for advice on dealing with this "feature". 583 584 You can find an existing file, to view it or edit it. You can also 585 find a file which does not already exist. This is the way to create a 586 file with Emacs: find the file, which will start out empty, and then 587 begin inserting the text for the file. When you ask to "save" the 588 file, Emacs will really create the file with the text that you have 589 inserted. From then on, you can consider yourself to be editing an 590 already existing file. 591 592 593 * BUFFERS 594 --------- 595 596 If you find a second file with C-x C-f, the first file remains 597 inside Emacs. You can switch back to it by finding it again with 598 C-x C-f. This way you can get quite a number of files inside Emacs. 599 600 >> Create a file named "foo" by typing C-x C-f foo <Return>. 601 Then insert some text, edit it, and save "foo" by typing C-x C-s. 602 Finally, type C-x C-f TUTORIAL <Return> 603 to come back to the tutorial. 604 605 Emacs stores each file's text inside an object called a "buffer". 606 Finding a file makes a new buffer inside Emacs. To see a list of the 607 buffers that currently exist in your Emacs job, type 608 609 C-x C-b List buffers 610 611 >> Try C-x C-b now. 612 613 See how each buffer has a name, and it may also have a file name for 614 the file whose contents it holds. ANY text you see in an Emacs window 615 is always part of some buffer. 616 617 >> Type C-x 1 to get rid of the buffer list. 618 619 When you have several buffers, only one of them is "current" at any 620 time. That buffer is the one you edit. If you want to edit another 621 buffer, you need to "switch" to it. If you want to switch to a buffer 622 that corresponds to a file, you can do it by visiting the file again 623 with C-x C-f. But there is an easier way: use the C-x b command. 624 In that command, you have to type the buffer's name. 625 626 >> Type C-x b foo <Return> to go back to the buffer "foo" which holds 627 the text of the file "foo". Then type C-x b TUTORIAL <Return> 628 to come back to this tutorial. 629 630 Most of the time, the buffer's name is the same as the file name 631 (without the file directory part). However, this is not always true. 632 The buffer list you make with C-x C-b always shows you the name of 633 every buffer. 634 635 ANY text you see in an Emacs window is always part of some buffer. 636 Some buffers do not correspond to files. For example, the buffer 637 named "*Buffer List*" does not have any file. It is the buffer which 638 contains the buffer list that you made with C-x C-b. The buffer named 639 "*Messages*" also does not correspond to any file; it contains the 640 messages that have appeared on the bottom line during your Emacs 641 session. 642 643 my text foo 644 645 >> Type C-x b *Messages* <Return> to look at the buffer of messages. 646 Then type C-x b TUTORIAL <Return> to come back to this tutorial. 647 648 If you make changes to the text of one file, then find another file, 649 this does not save the first file. Its changes remain inside Emacs, 650 in that file's buffer. The creation or editing of the second file's 651 buffer has no effect on the first file's buffer. This is very useful, 652 but it also means that you need a convenient way to save the first 653 file's buffer. It would be a nuisance to have to switch back to 654 it with C-x C-f in order to save it with C-x C-s. So we have 655 656 C-x s Save some buffers 657 658 C-x s asks you about each buffer which contains changes that you have 659 not saved. It asks you, for each such buffer, whether to save the 660 buffer. 661 662 >> Insert a line of text, then type C-x s. 663 It should ask you whether to save the buffer named TUTORIAL. 664 Answer yes to the question by typing "y". 665 666 667 * EXTENDING THE COMMAND SET 668 --------------------------- 669 670 There are many, many more Emacs commands than could possibly be put 671 on all the control and meta characters. Emacs gets around this with 672 the X (eXtend) command. This comes in two flavors: 673 674 C-x Character eXtend. Followed by one character. 675 M-x Named command eXtend. Followed by a long name. 676 677 These are commands that are generally useful but used less than the 678 commands you have already learned about. You have already seen a few 679 of them: the file commands C-x C-f to Find and C-x C-s to Save, for 680 example. Another example is the command to end the Emacs 681 session--this is the command C-x C-c. (Do not worry about losing 682 changes you have made; C-x C-c offers to save each changed file before 683 it kills the Emacs.) 684 685 If you are using a graphical display that supports multiple 686 applications in parallel, you don't need any special command to move 687 from Emacs to another application. You can do this with the mouse or 688 with window manager commands. However, if you're using a text 689 terminal which can only show one application at a time, you need to 690 "suspend" Emacs to move to any other program. 691 692 C-z is the command to exit Emacs *temporarily*--so that you can go 693 back to the same Emacs session afterward. When Emacs is running on a 694 text terminal, C-z "suspends" Emacs; that is, it returns to the shell 695 but does not destroy the Emacs. In the most common shells, you can 696 resume Emacs with the `fg' command or with `%emacs'. 697 698 The time to use C-x C-c is when you are about to log out. It's also 699 the right thing to use to exit an Emacs invoked under mail handling 700 programs and other miscellaneous utilities, since they may not know 701 how to cope with suspension of Emacs. In ordinary circumstances, 702 though, if you are not about to log out, it is better to suspend Emacs 703 with C-z instead of exiting Emacs. 704 705 There are many C-x commands. Here is a list of the ones you have learned: 706 707 C-x C-f Find file 708 C-x C-s Save file 709 C-x s Save some buffers 710 C-x C-b List buffers 711 C-x b Switch buffer 712 C-x C-c Quit Emacs 713 C-x 1 Delete all but one window 714 C-x u Undo 715 716 Named eXtended commands are commands which are used even less 717 frequently, or commands which are used only in certain modes. An 718 example is the command replace-string, which globally replaces one 719 string with another. When you type M-x, Emacs prompts you at the 720 bottom of the screen with M-x and you should type the name of the 721 command; in this case, "replace-string". Just type "repl s<TAB>" and 722 Emacs will complete the name. (<TAB> is the Tab key, usually found 723 above the CapsLock or Shift key near the left edge of the keyboard.) 724 End the command name with <Return>. 725 726 The replace-string command requires two arguments--the string to be 727 replaced, and the string to replace it with. You must end each 728 argument with <Return>. 729 730 >> Move the cursor to the blank line two lines below this one. 731 Then type M-x repl s<Return>changed<Return>altered<Return>. 732 733 Notice how this line has altered: you've replaced 734 the word c-h-a-n-g-e-d with "altered" wherever it occurred, 735 after the initial position of the cursor. 736 737 738 * AUTO SAVE 739 ----------- 740 741 When you have made changes in a file, but you have not saved them yet, 742 they could be lost if your computer crashes. To protect you from 743 this, Emacs periodically writes an "auto save" file for each file that 744 you are editing. The auto save file name has a # at the beginning and 745 the end; for example, if your file is named "hello.c", its auto save 746 file's name is "#hello.c#". When you save the file in the normal way, 747 Emacs deletes its auto save file. 748 749 If the computer crashes, you can recover your auto-saved editing by 750 finding the file normally (the file you were editing, not the auto 751 save file) and then typing M-x recover file<Return>. When it asks for 752 confirmation, type yes<Return> to go ahead and recover the auto-save 753 data. 754 755 756 * ECHO AREA 757 ----------- 758 759 If Emacs sees that you are typing multicharacter commands slowly, it 760 shows them to you at the bottom of the screen in an area called the 761 "echo area". The echo area contains the bottom line of the screen. 762 763 764 * MODE LINE 765 ----------- 766 767 The line immediately above the echo area is called the "mode line". 768 The mode line says something like this: 769 770 --:** TUTORIAL 63% L749 (Fundamental)----------------------- 771 772 This line gives useful information about the status of Emacs and 773 the text you are editing. 774 775 You already know what the filename means--it is the file you have 776 found. NN% indicates your current position in the text; it means that 777 NN percent of the text is above the top of the screen. If the top of 778 the file is on the screen, it will say "Top" instead of " 0%". If the 779 bottom of the text is on the screen, it will say "Bot". If you are 780 looking at text so small that all of it fits on the screen, the mode 781 line says "All". 782 783 The L and digits indicate position in another way: they give the 784 current line number of point. 785 786 The stars near the front mean that you have made changes to the text. 787 Right after you visit or save a file, that part of the mode line shows 788 no stars, just dashes. 789 790 The part of the mode line inside the parentheses is to tell you what 791 editing modes you are in. The default mode is Fundamental which is 792 what you are using now. It is an example of a "major mode". 793 794 Emacs has many different major modes. Some of them are meant for 795 editing different languages and/or kinds of text, such as Lisp mode, 796 Text mode, etc. At any time one and only one major mode is active, 797 and its name can always be found in the mode line just where 798 "Fundamental" is now. 799 800 Each major mode makes a few commands behave differently. For example, 801 there are commands for creating comments in a program, and since each 802 programming language has a different idea of what a comment should 803 look like, each major mode has to insert comments differently. Each 804 major mode is the name of an extended command, which is how you can 805 switch to that mode. For example, M-x fundamental-mode is a command to 806 switch to Fundamental mode. 807 808 If you are going to be editing human-language text, such as this file, you 809 should probably use Text Mode. 810 811 >> Type M-x text mode<Return>. 812 813 Don't worry, none of the Emacs commands you have learned changes in 814 any great way. But you can observe that M-f and M-b now treat 815 apostrophes as part of words. Previously, in Fundamental mode, 816 M-f and M-b treated apostrophes as word-separators. 817 818 Major modes usually make subtle changes like that one: most commands 819 do "the same job" in each major mode, but they work a little bit 820 differently. 821 822 To view documentation on your current major mode, type C-h m. 823 824 >> Use C-u C-v once or more to bring this line near the top of screen. 825 >> Type C-h m, to see how Text mode differs from Fundamental mode. 826 >> Type C-x 1 to remove the documentation from the screen. 827 828 Major modes are called major because there are also minor modes. 829 Minor modes are not alternatives to the major modes, just minor 830 modifications of them. Each minor mode can be turned on or off by 831 itself, independent of all other minor modes, and independent of your 832 major mode. So you can use no minor modes, or one minor mode, or any 833 combination of several minor modes. 834 835 One minor mode which is very useful, especially for editing 836 human-language text, is Auto Fill mode. When this mode is on, Emacs 837 breaks the line in between words automatically whenever you insert 838 text and make a line that is too wide. 839 840 You can turn Auto Fill mode on by doing M-x auto fill mode<Return>. 841 When the mode is on, you can turn it off again by doing M-x 842 auto fill mode<Return>. If the mode is off, this command turns it on, 843 and if the mode is on, this command turns it off. We say that the 844 command "toggles the mode". 845 846 >> Type M-x auto fill mode<Return> now. Then insert a line of "asdf " 847 over again until you see it divide into two lines. You must put in 848 spaces between them because Auto Fill breaks lines only at spaces. 849 850 The margin is usually set at 70 characters, but you can change it 851 with the C-x f command. You should give the margin setting you want 852 as a numeric argument. 853 854 >> Type C-x f with an argument of 20. (C-u 2 0 C-x f). 855 Then type in some text and see Emacs fill lines of 20 856 characters with it. Then set the margin back to 70 using 857 C-x f again. 858 859 If you make changes in the middle of a paragraph, Auto Fill mode 860 does not re-fill it for you. 861 To re-fill the paragraph, type M-q (META-q) with the cursor inside 862 that paragraph. 863 864 >> Move the cursor into the previous paragraph and type M-q. 865 866 867 * SEARCHING 868 ----------- 869 870 Emacs can do searches for strings (these are groups of contiguous 871 characters or words) either forward through the text or backward 872 through it. Searching for a string is a cursor motion command; 873 it moves the cursor to the next place where that string appears. 874 875 The Emacs search command is different from the search commands 876 of most editors, in that it is "incremental". This means that the 877 search happens while you type in the string to search for. 878 879 The command to initiate a search is C-s for forward search, and C-r 880 for reverse search. BUT WAIT! Don't try them now. 881 882 When you type C-s you'll notice that the string "I-search" appears as 883 a prompt in the echo area. This tells you that Emacs is in what is 884 called an incremental search waiting for you to type the thing that 885 you want to search for. <Return> terminates a search. 886 887 >> Now type C-s to start a search. SLOWLY, one letter at a time, 888 type the word 'cursor', pausing after you type each 889 character to notice what happens to the cursor. 890 Now you have searched for "cursor", once. 891 >> Type C-s again, to search for the next occurrence of "cursor". 892 >> Now type <Delback> four times and see how the cursor moves. 893 >> Type <Return> to terminate the search. 894 895 Did you see what happened? Emacs, in an incremental search, tries to 896 go to the occurrence of the string that you've typed out so far. To 897 go to the next occurrence of 'cursor' just type C-s again. If no such 898 occurrence exists, Emacs beeps and tells you the search is currently 899 "failing". C-g would also terminate the search. 900 901 NOTE: On some systems, typing C-s will freeze the screen and you will 902 see no further output from Emacs. This indicates that an operating 903 system "feature" called "flow control" is intercepting the C-s and not 904 letting it get through to Emacs. To unfreeze the screen, type C-q. 905 Then see the section "Spontaneous Entry to Incremental Search" in the 906 Emacs manual for advice on dealing with this "feature". 907 908 If you are in the middle of an incremental search and type <Delback>, 909 you'll notice that the last character in the search string is erased 910 and the search backs up to the last place of the search. For 911 instance, suppose you have typed "c", to search for the first 912 occurrence of "c". Now if you type "u", the cursor will move 913 to the first occurrence of "cu". Now type <Delback>. This erases 914 the "u" from the search string, and the cursor moves back to 915 the first occurrence of "c". 916 917 If you are in the middle of a search and type a control or meta 918 character (with a few exceptions--characters that are special in 919 a search, such as C-s and C-r), the search is terminated. 920 921 The C-s starts a search that looks for any occurrence of the search 922 string AFTER the current cursor position. If you want to search for 923 something earlier in the text, type C-r instead. Everything that we 924 have said about C-s also applies to C-r, except that the direction of 925 the search is reversed. 926 927 928 * MULTIPLE WINDOWS 929 ------------------ 930 931 One of the nice features of Emacs is that you can display more than one 932 window on the screen at the same time. 933 934 >> Move the cursor to this line and type C-u 0 C-l (that's CONTROL-L, not 935 CONTROL-1). 936 937 >> Now type C-x 2 which splits the screen into two windows. 938 Both windows display this tutorial. The cursor stays in the top window. 939 940 >> Type C-M-v to scroll the bottom window. 941 (If you do not have a real META key, type <ESC> C-v.) 942 943 >> Type C-x o ("o" for "other") to move the cursor to the bottom window. 944 >> Use C-v and M-v in the bottom window to scroll it. 945 Keep reading these directions in the top window. 946 947 >> Type C-x o again to move the cursor back to the top window. 948 The cursor in the top window is just where it was before. 949 950 You can keep using C-x o to switch between the windows. Each 951 window has its own cursor position, but only one window actually 952 shows the cursor. All the ordinary editing commands apply to the 953 window that the cursor is in. We call this the "selected window". 954 955 The command C-M-v is very useful when you are editing text in one 956 window and using the other window just for reference. You can keep 957 the cursor always in the window where you are editing, and advance 958 through the other window sequentially with C-M-v. 959 960 C-M-v is an example of a CONTROL-META character. If you have a real 961 META key, you can type C-M-v by holding down both CONTROL and META while 962 typing v. It does not matter whether CONTROL or META "comes first," 963 because both of these keys act by modifying the characters you type. 964 965 If you do not have a real META key, and you use <ESC> instead, the 966 order does matter: you must type <ESC> followed by CONTROL-v, because 967 CONTROL-<ESC> v will not work. This is because <ESC> is a character 968 in its own right, not a modifier key. 969 970 >> Type C-x 1 (in the top window) to get rid of the bottom window. 971 972 (If you had typed C-x 1 in the bottom window, that would get rid 973 of the top one. Think of this command as "Keep just one 974 window--the window I am already in.") 975 976 You do not have to display the same buffer in both windows. If you 977 use C-x C-f to find a file in one window, the other window does not 978 change. You can find a file in each window independently. 979 980 Here is another way to use two windows to display two different 981 things: 982 983 >> Type C-x 4 C-f followed by the name of one of your files. 984 End with <Return>. See the specified file appear in the bottom 985 window. The cursor goes there, too. 986 987 >> Type C-x o to go back to the top window, and C-x 1 to delete 988 the bottom window. 989 990 991 * RECURSIVE EDITING LEVELS 992 -------------------------- 993 994 Sometimes you will get into what is called a "recursive editing 995 level". This is indicated by square brackets in the mode line, 996 surrounding the parentheses around the major mode name. For 997 example, you might see [(Fundamental)] instead of (Fundamental). 998 999 To get out of the recursive editing level, type <ESC> <ESC> <ESC>. 1000 That is an all-purpose "get out" command. You can also use it for 1001 eliminating extra windows, and getting out of the minibuffer. 1002 1003 >> Type M-x to get into a minibuffer; then type <ESC> <ESC> <ESC> to 1004 get out. 1005 1006 You cannot use C-g to get out of a recursive editing level. This is 1007 because C-g is used for canceling commands and arguments WITHIN the 1008 recursive editing level. 1009 1010 1011 * GETTING MORE HELP 1012 ------------------- 1013 1014 In this tutorial we have tried to supply just enough information to 1015 get you started using Emacs. There is so much available in Emacs that 1016 it would be impossible to explain it all here. However, you may want 1017 to learn more about Emacs since it has many other useful features. 1018 Emacs provides commands for reading documentation about Emacs 1019 commands. These "help" commands all start with the character 1020 CONTROL-h, which is called "the Help character". 1021 1022 To use the Help features, type the C-h character, and then a 1023 character saying what kind of help you want. If you are REALLY lost, 1024 type C-h ? and Emacs will tell you what kinds of help it can give. 1025 If you have typed C-h and decide you do not want any help, just 1026 type C-g to cancel it. 1027 1028 (Some sites change the meaning of the character C-h. They really 1029 should not do this as a blanket measure for all users, so you have 1030 grounds to complain to the system administrator. Meanwhile, if C-h 1031 does not display a message about help at the bottom of the screen, try 1032 typing the F1 key or M-x help <Return> instead.) 1033 1034 The most basic HELP feature is C-h c. Type C-h, the character c, and 1035 a command character or sequence; then Emacs displays a very brief 1036 description of the command. 1037 1038 >> Type C-h c C-p. 1039 1040 The message should be something like this: 1041 1042 C-p runs the command previous-line 1043 1044 This tells you the "name of the function". Function names are used 1045 mainly for customizing and extending Emacs. But since function names 1046 are chosen to indicate what the command does, they can serve also as 1047 very brief documentation--sufficient to remind you of commands you 1048 have already learned. 1049 1050 Multi-character commands such as C-x C-s and (if you have no META or 1051 EDIT or ALT key) <ESC>v are also allowed after C-h c. 1052 1053 To get more information about a command, use C-h k instead of C-h c. 1054 1055 >> Type C-h k C-p. 1056 1057 This displays the documentation of the function, as well as its 1058 name, in an Emacs window. When you are finished reading the 1059 output, type C-x 1 to get rid of the help text. You do not have 1060 to do this right away. You can do some editing while referring 1061 to the help text, and then type C-x 1. 1062 1063 Here are some other useful C-h options: 1064 1065 C-h f Describe a function. You type in the name of the 1066 function. 1067 1068 >> Try typing C-h f previous-line<Return>. 1069 This displays all the information Emacs has about the 1070 function which implements the C-p command. 1071 1072 A similar command C-h v displays the documentation of variables whose 1073 values you can set to customize Emacs behavior. You need to type in 1074 the name of the variable when Emacs prompts for it. 1075 1076 C-h a Command Apropos. Type in a keyword and Emacs will list 1077 all the commands whose names contain that keyword. 1078 These commands can all be invoked with META-x. 1079 For some commands, Command Apropos will also list a one 1080 or two character sequence which runs the same command. 1081 1082 >> Type C-h a file<Return>. 1083 1084 This displays in another window a list of all M-x commands with "file" 1085 in their names. You will see character-commands like C-x C-f listed 1086 beside the corresponding command names such as find-file. 1087 1088 >> Type C-M-v to scroll the help window. Do this a few times. 1089 1090 >> Type C-x 1 to delete the help window. 1091 1092 C-h i Read On-line Manuals (a.k.a. Info). This command puts 1093 you into a special buffer called `*info*' where you 1094 can read on-line manuals for the packages installed on 1095 your system. Type m emacs <Return> to read the Emacs 1096 manual. If you have never before used Info, type ? 1097 and Emacs will take you on a guided tour of Info mode 1098 facilities. Once you are through with this tutorial, 1099 you should consult the Emacs Info manual as your 1100 primary documentation. 1101 1102 1103 * MORE FEATURES 1104 --------------- 1105 1106 You can learn more about Emacs by reading its manual, either as a book 1107 or on-line in Info (use the Help menu or type F10 h r). Two features 1108 that you may like especially are completion, which saves typing, and 1109 dired, which simplifies file handling. 1110 1111 Completion is a way to avoid unnecessary typing. For instance, if you 1112 want to switch to the *Messages* buffer, you can type C-x b *M<Tab> 1113 and Emacs will fill in the rest of the buffer name as far as it can 1114 determine from what you have already typed. Completion is described 1115 in Info in the Emacs manual in the node called "Completion". 1116 1117 Dired enables you to list files in a directory (and optionally its 1118 subdirectories), move around that list, visit, rename, delete and 1119 otherwise operate on the files. Dired is described in Info in the 1120 Emacs manual in the node called "Dired". 1121 1122 The manual also describes many other Emacs features. 1123 1124 1125 * CONCLUSION 1126 ------------ 1127 1128 Remember, to exit Emacs permanently use C-x C-c. To exit to a shell 1129 temporarily, so that you can come back to Emacs afterward, use C-z. 1130 1131 This tutorial is meant to be understandable to all new users, so if 1132 you found something unclear, don't sit and blame yourself - complain! 1133 1134 1135 * COPYING 1136 --------- 1137 1138 This tutorial descends from a long line of Emacs tutorials 1139 starting with the one written by Stuart Cracraft for the original Emacs. 1140 1141 This version of the tutorial is a part of GNU Emacs. It is copyrighted 1142 and comes with permission to distribute copies on certain conditions: 1143 1144 Copyright (C) 1985, 1996, 1998, 2001, 2002, 2003, 2004, 1145 2005, 2006, 2007 Free Software Foundation, Inc. 1146 1147 This file is part of GNU Emacs. 1148 1149 GNU Emacs is free software; you can redistribute it and/or modify 1150 it under the terms of the GNU General Public License as published by 1151 the Free Software Foundation; either version 2, or (at your option) 1152 any later version. 1153 1154 GNU Emacs is distributed in the hope that it will be useful, 1155 but WITHOUT ANY WARRANTY; without even the implied warranty of 1156 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1157 GNU General Public License for more details. 1158 1159 You should have received a copy of the GNU General Public License 1160 along with GNU Emacs; see the file COPYING. If not, write to the 1161 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 1162 Boston, MA 02110-1301, USA. 1163 1164 Please read the file COPYING and then do give copies of GNU Emacs to 1165 your friends. Help stamp out software obstructionism ("ownership") by 1166 using, writing, and sharing free software! 1167 1168 ;;; arch-tag: a0f84628-777f-4238-8865-451a73167f55