summaryrefslogtreecommitdiffstats
path: root/chapter_13.xml
blob: fe3305f02edf0d819816d8066b554857e0adcadd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?xml version="1.0"?>
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  "/usr/share/xml/docbook/xml-dtd-4.5/docbookx.dtd">

<chapter>
  <title><application>Emacs</application></title>

  <section>
    <title>What is <application>Emacs</application>?</title>

    <para>
      <application>vi</application> and its clones are very functional
      and powerful editors.  However, they are often considered not
      particularly extensible.  <application>vim</application> is a
      successful and powerful
      <application>vi</application> variant that shrugs this trend,
      being both extremely extensible and lightweight.  However, many
      users prefer a more "heavy" and extensible editor.  This is why
      many people (including the author of this chapter) prefer
      <application>Emacs</application>.
    </para>

    <para>
      Emacs takes extensibility up to eleven.  Outside of a core of C,
      the rest of <application>Emacs</application> is written in a
      Lisp variant, nearly all of which is exposed to you, so that you
      may configure it or even extend it at will (many good Emacs Lisp
      tutorials can be found on the Internet).  People have written
      all sorts of extensions in Emacs Lisp, from syntax highlighting
      for an obscure language, to a built-in terminal.  In fact,
      there's even a <application>vi</application> emulation mode
      within <application>Emacs</application> (called viper), so you
      can still get the modal editing that comes with vi, while having
      access to the power of the <application>Emacs</application>
      core.
    </para>

    <para>
      Like <application>vi</application>, there are many variants of
      <application>Emacs</application> (termed "emacsen").  However,
      the one most commonly used (and the only one in Slackware) is
      GNU Emacs.  When people reference "Emacs" directly, they almost
      always mean GNU Emacs.
    </para>

    <para>
      Unlike <application>vi</application>,
      <application>Emacs</application> operates more like a
      traditional editor by default.  This means that most keyboard
      shortcuts can be performed without repeatedly changing modes.
      You can open up a file and start typing away without having to
      learn what the modes do, or forgetting which one you are
      currently using.
    </para>
  </section>

  <section>
    <title>Starting Out</title>
    <para>
      <application>Emacs</application> can be started simply by
      running the <application>emacs</application> command in your
      terminal.  When
      you first start it in a console without arguments, you will see
      something that resembles this:
    </para>
    
    <imagedata fileref="img/emacs-startup.png" format="PNG"/>
    
    <para>
      If you are in X windows, <application>Emacs</application> may
      start a GUI instead of running in your console.  If this is the
      case and you don't want a GUI, you can invoke it with the flag
      <arg>'-nw'</arg>.
    </para>

    <para>
      While here, you can browse around using the
      keyboard arrow keys.
      Underlined elements are links, and you can activate them by
      moving over them and pressing 'enter'.  The documentation
      mentioned is very good, and can help you get your bearings
      should you have any problems.  Also note how they describe key
      sequences such as <keycap>C-h</keycap>, meaning press the
      <keycap>h</keycap>
      key while holding down the <keycap>CTRL</keycap> key.  Same deal
      with <keycap>M-`</keycap>, meaning to hold the the
      <keycap>Meta</keycap> key (usually "<keycap>Alt</keycap>") and
      press the backtick "`" key.  When they say (e.g.)  <keycap>C-x
      C-c</keycap>, this means to press the "x" key while holding down
      the <keycap>CTRL</keycap> key, then press the "x" key while also
      holding down the <keycap>CTRL</keycap> key.  Conveniently, this
      is also one of the more important commands in
      <application>Emacs</application>: to close it.
    </para>

    <para>
      Alternatively, if you call <application>emacs</application> with a
      file name as an argument, it will open that file, just like
      <application>vi</application>.  Upon doing this, you will be
      presented with the contents of the file in question.  Here, you can
      navigate the document using traditional arrow keys and type in
      information at will without any issues.
    </para>

    <para>
      Say you make some edits, and you now want to save your file.
      The following key sequence will do that: <keycap>C-x C-s</keycap>.
      If you made a new file, you will be prompted for this in what is
      called the "minibuffer", the blank line below the gray line at
      the bottom of the screen.  Type in the file name of your choice,
      then hit <keycap>Enter</keycap>. If you don't want to
      save the file, you can press <keycap>C-g</keycap>, which aborts
      operations that ask for input.  Do note that tab-completion is
      usually available for operations that use the minibuffer.
    </para>

    <para>
      Should you want to open a new file within your same
      <application>Emacs</application> session, type in <keycap>C-x
      C-f</keycap>.  You will be prompted for a file name in the
      minibuffer.  <application>Emacs</application> doesn't care
      whether it exists or not.  If it doesn't exist, a new buffer
      will be created for it (the file will be created upon saving
      with <keycap>C-x C-s</keycap>), or it will be opened as
      expected.  However, the old file will still be open!  You can
      switch back to it using <keycap>C-x C-b</keycap>, entering in
      the file's name (or more technically, the buffer's name), then
      hitting <keycap>Enter</keycap>.
    </para>

  </section>

  <section>
    <title>How to Move Around</title>
    <para>
      Like <application>vi</application>,
      <application>Emacs</application> is also older than the arrow
      keys on your keyboard.  Also, like in
      <application>vi</application>, using the arrow keys to navigate
      files is also supported.  While the
      <application>vi</application> movement keys are more ergonomic,
      <application>emacs</application>'s are more "mnemonic".
      However, it is still very possible to operate using the main
      <application>Emacs</application> keybindings quickly and
      efficiently.  Here is a table of the basic movement keybindings:
    </para>
    
    <table pgwide="0">
      <title>Emacs Cursor Movement</title>
      <tgroup cols="2">
        <thead>
          <row>
            <entry>Command</entry>
            <entry>Result</entry>
          </row>
        </thead>
        <tbody>
          <row>
            <entry>C-f</entry>
            <entry>Move the cursor one character to the right (forward)</entry>
          </row>
          <row>
            <entry>C-b</entry>
            <entry>Move the cursor one character to the left (backward)</entry>
          </row>
          <row>
            <entry>C-n</entry>
            <entry>Move the cursor one line down (next)</entry>
          </row>
          <row>
            <entry>C-p</entry>
            <entry>Move the cursor one line up (previous)</entry>
          </row>
        </tbody>
      </tgroup>
    </table>
   
    <para>
    Of course, like with <application>vi</application> it is also
    possible to repeat these commands with a numeric argument.  If you
    type in <keycap>M-1 M-0 C-p</keycap>, or <keycap>C-u 10
    C-p</keycap>, the cursor will move ten lines up.  If you type in
    <keycap>M-5 C-f</keycap> or <keycap>C-u 5 C-f</keycap>, the cursor
    will move five characters to the right.
    </para>
  </section>

  <section>
    <title>Getting Help</title>

    <para>
      <application>Emacs</application> contains a great deal of
      documentation, to the point that it is often called a
      "self-documenting" editor. This is because it provides
      mechanisms for providing users with documentation while you are
      using it.

      Here are some useful functions that display documentation (they
      all start with <keycap>C-h</keycap>):
    </para>

      <table pgwide="0">
        <title>Accessing <application>Emacs</application> Documentation</title>
        <tgroup cols="2">
          <thead>
            <row>
              <entry>Command</entry>
              <entry>Result</entry>
            </row>
          </thead>
          <tbody>
            <row>
              <entry>C-h f FUNCTION-NAME Enter</entry>
              <entry>Show documentation for function FUNCTION-NAME</entry>
            </row>
            <row>
              <entry>C-h k C-x C-c</entry>
              <entry>Show documentation for the function bound to the keys
              <keycap>C-x C-c</keycap></entry>
            </row>
            <row>
              <entry>C-h t</entry>
              <entry>Show the Emacs tutorial</entry>
              </row>
              <row>
                <entry>C-h ?</entry>
                <entry>Show all help-related functions</entry>
              </row>
          </tbody>
        </tgroup>
      </table>
    <para>
      <keycap>C-h t</keycap> is especially useful if
      you want or need practice using <application>Emacs</application>.
    </para>

  </section>


  <section>
    <title>Calling Functions</title>
    <para>
      As noted earlier, <application>Emacs</application> exports a
      large number of functions to for interactive use.  Some of
      these, like those opening and saving files, are mapped to keys.
      Others (like the ones for moving to the beginning and end of
      lines) are not.  To call them, you have to invoke them.  Say we
      want to call the function "end-of-line".  We would do
      this:
    </para>

    <para>
      <keycap>M-x end-of-line Enter</keycap>
    </para>
    
    <para>
      And the cursor would move to the end of the line, as the function name
      suggests.
    </para>
  </section>

  <section>
    <title>Emacs Cheat Sheet</title>

    <para>
      While Emacs can be simple to use, its scope can easily be
      overwhelming.  Below are some useful Emacs commands.  Some
      aspects have been simplified, most notably regarding text
      selection.  These concepts, and more, are described the
      <application>Emacs</application> manual, and various on-line
      tutorials.  Decent summaries can also be gleaned from web
      searches.
    </para>

    <table pgwide="0">
      <title>Emacs Cheat Sheet</title>
      <tgroup cols="2">
        <thead>
          <row>
            <entry>Command</entry>
            <entry>Result</entry>
          </row>
        </thead>
        <tbody>
          <row>
            <entry>C-f</entry>
            <entry>Move the cursor one character to the right (forward)</entry>
          </row>
          <row>
            <entry>C-b</entry>
            <entry>Move the cursor one character to the left (backward)</entry>
          </row>
          <row>
            <entry>C-n</entry>
            <entry>Move the cursor one line down (next)</entry>
          </row>
          <row>
            <entry>C-p</entry>
            <entry>Move the cursor one line up (previous)</entry>
          </row>
          <row>
            <entry>C-h f FUNCTION-NAME Enter</entry>
            <entry>Show documentation for function FUNCTION-NAME</entry>
          </row>
          <row>
            <entry>C-h k C-x C-c</entry>
            <entry>Show documentation for the function bound to the keys <keycap>C-x C-c</keycap></entry>
          </row>
          <row>
            <entry>C-h t</entry>
            <entry>Show the Emacs tutorial</entry>
          </row>
          <row>
            <entry>C-h ?</entry>
            <entry>Show all help-related functions</entry>
          </row>
          <row>
            <entry>M-`</entry>
            <entry>Access the Menu Bar</entry>
          </row>
          <row>
            <entry>C-g</entry>
            <entry>Cancel the current operation.  This is most useful when in the minibuffer.</entry>
          </row>
          <row>
            <entry>M-x FUNCTION-NAME Enter</entry>
            <entry>Call the interactive function FUNCTION-NAME</entry>
          </row>
          <row>
            <entry>M-1 M-0 C-n</entry>
            <entry>Move the cursor ten lines down</entry>
          </row>
          <row>
            <entry>C-u 10 C-n</entry>
            <entry>Move the cursor ten lines down (same as above)</entry>
          </row>
          <row>
            <entry>M-x beginning-of-line</entry>
            <entry>Move the cursor to the beginning of the current line</entry>
          </row>
          <row>
            <entry>M-x end-of-line</entry>
            <entry>Move the cursor to the end of the current line</entry>
          </row>
          <row>
            <entry>M-&gt;</entry>
            <entry>Move the cursor to the end of the buffer</entry>
          </row>
          <row>
            <entry>M-&lt;</entry>
            <entry>Move the cursor to the beginning of the buffer</entry>
          </row>
          <row>
            <entry>C-k</entry>
            <entry>Remove text from the cursor to the end of the line and
            place it into the kill ring </entry>
          </row>
          <row>
            <entry>C-space</entry>
            <entry>Enter selection mode (use normal motion keys to move around).  Press <keycap>C-space</keycap> again to leave it.</entry>
          </row>
          <row>
            <entry>C-w</entry>
            <entry>While in selection mode, delete the selected region, and store the result into the kill ring</entry>
          </row>
          <row>
            <entry>M-w</entry>
            <entry>While in selection mode, store the selected region into the kill ring.</entry>
          </row>
          <row>
            <entry>C-y</entry>
            <entry>"Yanks" the contents of the kill ring and places them at the cursor's location</entry>
          </row>
          <!-- <row>
               <entry>M-y</entry>
               <entry>Replace the current contents of the kill ring with the item last yanked from it.  Useful for cycling between the key-ring </entry>
               </row> -->
          <row>
            <entry>C-/</entry>
            <entry>Undo the previous action.  Unlike most other editors, this includes previous undo actions.</entry>
          </row>
          <row>
            <entry>insert</entry>
            <entry>Enable or disable overwriting characters</entry>
          </row>
          <row>
            <entry>C-s asdf Enter</entry>
            <entry>Forward incremental search for the string "asdf".  Repeat <keycap>C-s</keycap> as needed to search for future items, or <keycap>C-r</keycap> (below) to search backwards.</entry>
          </row>
          <row>
            <entry>C-r asdf Enter</entry>
            <entry>Backward incremental search for the string "asdf".  Repeat <keycap>C-r</keycap> as needed to search for future items, or <keycap>C-s</keycap> (above) to search forwards.</entry>
          </row>
          <row>
            <entry>M-% old Enter new Enter</entry>
            <entry>Search for each instance of "old" and prompt you to replace it with "new".  You can force replacement of all items by typing "!" at the replacement prompt.  </entry>
          </row>
          <row>
            <entry>C-x C-c</entry>
            <entry>Exit <application>Emacs</application>, prompting you to save each unsaved buffer before doing so</entry>
          </row>
          <row>
            <entry>C-x C-s</entry>
            <entry>Save the currrent buffer to its file</entry>
          </row>
          <row>
            <entry>C-x C-w new-file.txt Enter</entry>
            <entry>Save the current buffer to a file "new-file.txt"</entry>
          </row>
        </tbody>
      </tgroup>
    </table>
  </section>
</chapter>