-
Notifications
You must be signed in to change notification settings - Fork 139
/
emacs-for-proglang.html
1465 lines (1288 loc) · 50.7 KB
/
emacs-for-proglang.html
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2016-07-26 Tue 20:34 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Emacs for Programming Languages course on Coursera</title>
<meta name="generator" content="Org-mode" />
<meta name="author" content="Tu Do" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
/*]]>*/-->
</style>
<link rel="stylesheet" href="./static/worg.css">
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2013 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
displayAlign: "center",
displayIndent: "0em",
"HTML-CSS": { scale: 100,
linebreaks: { automatic: "false" },
webFont: "TeX"
},
SVG: {scale: 100,
linebreaks: { automatic: "false" },
font: "TeX"},
NativeMML: {scale: 100},
TeX: { equationNumbers: {autoNumber: "AMS"},
MultLineWidth: "85%",
TagSide: "right",
TagIndent: ".8em"
}
});
</script>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body>
<div id="preamble" class="status">
<!-- Start of StatCounter Code for Default Guide -->
<script type="text/javascript">
var sc_project=9874755;
var sc_invisible=1;
var sc_security="c2028bb7";
var scJsHost = (("https:" == document.location.protocol) ?
"https://secure." : "http://www.");
document.write("<sc"+"ript type='text/javascript' src='" + scJsHost+
"statcounter.com/counter/counter.js'></"+"script>");
</script>
<noscript><div class="statcounter"><a title="hit counter"
href="http://statcounter.com/free-hit-counter/" target="_blank"><img
class="statcounter" src="http://c.statcounter.com/9874755/0/c2028bb7/1/"
alt="hit counter"></a></div></noscript>
<!-- End of StatCounter Code for Default Guide -->
<h2><a href="index.html">Back to Table of Contents</a></h2>
</div>
<div id="content">
<h1 class="title">Emacs for Programming Languages course on Coursera</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgheadline1">Before using Emacs:</a>
<ul>
<li><a href="#orgheadline2">Emacs and SML in Linux:</a></li>
<li><a href="#orgheadline3">Emacs and SML Installation on Windows</a></li>
<li><a href="#orgheadline4">Optional but recommended: Swap Control and Capslock</a>
<ul>
<li><a href="#orgheadline5">Windows</a></li>
<li><a href="#orgheadline6">Linux</a></li>
<li><a href="#orgheadline7">Mac OS X</a></li>
<li><a href="#orgheadline8">If you don't like to swap Capslock and Control…</a></li>
</ul>
</li>
<li><a href="#orgheadline9">Optional: changing Emacs look and feel</a></li>
</ul>
</li>
<li><a href="#orgheadline10">General basic Emacs commands</a>
<ul>
<li><a href="#orgheadline11">Command</a></li>
<li><a href="#orgheadline12">Emacs Key Notation</a></li>
<li><a href="#orgheadline13">Emacs Keys are easy to remember</a></li>
<li><a href="#orgheadline14">Some basic commands</a>
<ul>
<li><a href="#orgheadline15">Basic motion commands</a></li>
<li><a href="#orgheadline16">Incremental search</a></li>
<li><a href="#orgheadline17">Basic editing commands:</a></li>
<li><a href="#orgheadline18">Buffer commands</a></li>
<li><a href="#orgheadline19">Window commands</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orgheadline20">Commands for SML</a></li>
<li><a href="#orgheadline21">Commands for Racket</a>
<ul>
<li><a href="#orgheadline22">Editing support: You don't have to fight the parentheses</a>
<ul>
<li><a href="#orgheadline23">Automatic indentation</a></li>
<li><a href="#orgheadline24">Automatic comment</a></li>
</ul>
</li>
<li><a href="#orgheadline25">Basic navigation for Lisp expressions</a></li>
<li><a href="#orgheadline26">Geiser commands</a>
<ul>
<li><a href="#orgheadline27">What is Geiser?</a></li>
<li><a href="#orgheadline28">Basic workflow</a></li>
<li><a href="#orgheadline29">Programming assistance</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orgheadline30">Conclusion</a></li>
</ul>
</div>
</div>
<p>
I wrote this tutorial for those who are learning <a href="https://www.coursera.org/course/proglang">Programming Languages</a>
course on Coursera. The course suggests students to use Emacs, but a
stock one. This can cause confusion for new users since it does not
include <code>sml-mode</code> by default. I made a Emacs kit specialized for this
course here, with external common packages used by the Emacs community
to empower Emacs: <a href="https://github.com/tuhdo/emacs-proglang.git">https://github.com/tuhdo/emacs-proglang.git</a>
</p>
<p>
The kit is battery included: after cloning it, just open your Emacs
and use it. You don't have to do anything else. I assume you know
nothing about Emacs and I will provide a basic workflow for you to use
Emacs through the course. Currently, I will write a guide for SML
first. Racket and Ruby will come later.
</p>
<div id="outline-container-orgheadline1" class="outline-2">
<h2 id="orgheadline1"><a id="ID-c638e266-96e3-4a18-be73-d51ea0481775"></a>Before using Emacs:</h2>
<div class="outline-text-2" id="text-orgheadline1">
</div>
<div id="outline-container-orgheadline2" class="outline-3">
<h3 id="orgheadline2"><a id="ID-8acc1c68-72e7-4116-8afd-2265dda008d2"></a>Emacs and SML in Linux:</h3>
<div class="outline-text-3" id="text-orgheadline2">
<p>
Easy way to install if you are using Ubuntu: <code>sudo apt-get install
emacs smlnj</code>. If you use other Linux distributions, use the package
manager of your distribution and install Emacs.
</p>
<p>
After that, clone the Emacs kit to your <code>~/.emacs.d</code>:
</p>
<div class="org-src-container">
<pre class="src src-sh">git clone https://github.com/tuhdo/emacs-proglang.git ~/.emacs.d
</pre>
</div>
</div>
</div>
<div id="outline-container-orgheadline3" class="outline-3">
<h3 id="orgheadline3"><a id="ID-51a50976-16db-4d0c-b736-4494fa57c2a7"></a>Emacs and SML Installation on Windows</h3>
<div class="outline-text-3" id="text-orgheadline3">
<ul class="org-ul">
<li>If you don't have Emacs yet, download the 64 bit version here:
<a href="http://sourceforge.net/projects/emacsbinw64/">http://sourceforge.net/projects/emacsbinw64/</a>, unpackage it to
<b>C:\Program Files\</b> , then create a shortcut in your desktop to
<b>runemacs.exe</b>, an executable in the <b>emacs\bin</b> directory.</li>
<li>Now, clone my Emacs kit:</li>
</ul>
<div class="org-src-container">
<pre class="src src-sh">git clone https://github.com/tuhdo/emacs-proglang.git ~/.emacs.d
</pre>
</div>
<ul class="org-ul">
<li>Or, if you don't have git, download release package in either zip or
tar.gz format at: <a href="https://github.com/tuhdo/emacs-proglang/releases">https://github.com/tuhdo/emacs-proglang/releases</a>.</li>
<li>You should unpackage into <b>~/.emacs.d</b>. But before that, you must
define your <b>HOME</b> and <b>PATH</b> directory.The path <b>~/</b> stands for <b>$HOME</b>
path in Linux, and is the default directory allocate for each user
and the user can put whatever he/she wants there. <b>PATH</b> is an
environment variable in Linux that makes the shell see executable
programs, so you can execute them by specifying the program name
only. We can use the same thing in Windows.</li>
<li><p>
In Windows, to set your <b>HOME</b> variable: left clicking <b>My Computer</b>
-> <b>Properties</b> -> <b>Advanced System Settings</b> -> <b>Advanced</b> ->
<b>Environment Variables</b> -> <b>New</b>. Then enter <b>HOME</b> in <code>Variable
name</code>, and a path of your choice in <code>Variable value</code>. After this,
Emacs can see the init file <code>init.el</code> in you <code>.emacs.d</code>
directory. Remember, you have to put <b>.emacs.d</b> in your home
directory. For example, if you home directory that you assigned to
the <b>HOME</b> variable is <b>C:\home\</b>, then you must put <b>.emacs.d</b>
there.
</p>
<div class="figure">
<p><a href="static/proglang/emacs_home.png"><img src="static/proglang/emacs_home.png" alt="emacs_home.png" /></a>
</p>
</div></li>
<li><p>
Now that you created a <b>HOME</b> variable, you need to create one more:
<b>PATH</b> variable. Emacs searches for any program in <b>PATH</b>
variable. Click <b>New</b>, then enter <b>PATH</b> into <code>Variable name</code>. In
<code>Variable value</code>, if you install SML using the standard installer,
then it should be at <b>C:\Program Files (x86)\SMLNJ</b>. Paste that path
into the text field. In the future, if you want to add more
locations that contain programs Emacs can see, add more path and
separate each path by a semicolon.
</p>
<div class="figure">
<p><a href="static/proglang/emacs_path.png"><img src="static/proglang/emacs_path.png" alt="emacs_path.png" /></a>
</p>
</div></li>
</ul>
<p>
After you finish these two important steps, try running SML inside
Emacs by press <b>M-x (Alt + x)</b> then enter <b>run-sml</b>. Press Enter to
confirm program to run. A REPL should appear. Congratulation, you
succeeded.
</p>
</div>
</div>
<div id="outline-container-orgheadline4" class="outline-3">
<h3 id="orgheadline4"><a id="ID-4e00a8da-dbf0-4156-81e8-c6ed0f5c58f5"></a>Optional but recommended: Swap Control and Capslock</h3>
<div class="outline-text-3" id="text-orgheadline4">
<p>
Swapping Control and Capslock, in general, is not required to make the
best out of Emacs, if you at least use regular key PC keyboard or
better. However, it is not nice at all on a laptop keyboard. If you
use a laptop keyboard for writing code, I strongly recommended to swap
Control and Capslock for better Emacs experience.
</p>
<p>
Swapping Control and Caplsock will not only benefit for using Emacs,
it is beneficial in general, as Control is a much more frequently used
key than Capslock. Popular shells like Bash or Zsh use Control a lot
for quick cursor movement.
</p>
</div>
<div id="outline-container-orgheadline5" class="outline-4">
<h4 id="orgheadline5"><a id="ID-eb9ffe1f-f726-4b15-8431-b075e9ba234d"></a>Windows</h4>
<div class="outline-text-4" id="text-orgheadline5">
<p>
Follow this guide: <a href="http://www.kodiva.com/post/swapping-caps-lock-and-control-keys">Swapping Capslock and Control keys</a>
</p>
</div>
</div>
<div id="outline-container-orgheadline6" class="outline-4">
<h4 id="orgheadline6"><a id="ID-32a786e9-ae18-4c7b-9d12-1940e4f2d301"></a>Linux</h4>
<div class="outline-text-4" id="text-orgheadline6">
<p>
Put this in your shell init file (.bashrc, .zshrc…):
</p>
<div class="org-src-container">
<pre class="src src-shell-script">/usr/bin/setxkbmap -option <span style="color: #8b2252;">"ctrl:swapcaps"</span>
</pre>
</div>
<p>
If you use Ubuntu, follow this guide: <a href="http://askubuntu.com/a/412622/13847">Swap caps lock and ctrl in
ubuntu 13.10</a>.
</p>
</div>
</div>
<div id="outline-container-orgheadline7" class="outline-4">
<h4 id="orgheadline7"><a id="ID-dc1f00fd-29a6-45e0-8398-211418cba728"></a>Mac OS X</h4>
<div class="outline-text-4" id="text-orgheadline7">
<p>
Follow this answer on StackOverflow: <a href="http://stackoverflow.com/a/162907/496700">Emacs on Mac OS X Leopard key
bindings</a>
</p>
</div>
</div>
<div id="outline-container-orgheadline8" class="outline-4">
<h4 id="orgheadline8"><a id="ID-cefa92ca-8234-44bf-9ae5-66dc76aadd0d"></a>If you don't like to swap Capslock and Control…</h4>
<div class="outline-text-4" id="text-orgheadline8">
<p>
You can use your palm to press Control in standard PC keyboard.
</p>
</div>
</div>
</div>
<div id="outline-container-orgheadline9" class="outline-3">
<h3 id="orgheadline9"><a id="ID-f0615620-f034-4adc-be6c-64de90576d12"></a>Optional: changing Emacs look and feel</h3>
<div class="outline-text-3" id="text-orgheadline9">
<p>
You can change Emacs look and feel easily:
</p>
<ul class="org-ul">
<li>Press <b>Alt + x</b>.</li>
<li>Type <b>customize-theme</b> (you can press <b>TAB</b> to complete).</li>
<li>There are some default themes there, select the one you want.</li>
</ul>
<p>
You can preview the themes at these two pages:
</p>
<ul class="org-ul">
<li><a href="http://pawelbx.github.io/emacs-theme-gallery/">http://pawelbx.github.io/emacs-theme-gallery/</a></li>
<li><a href="https://emacsthemes.com/">https://emacsthemes.com/</a></li>
</ul>
<p>
To install the themes:
</p>
<ul class="org-ul">
<li>Press <b>Alt + x</b>.</li>
<li>Type <b>list-packages</b>.</li>
<li>Search for the theme names using <b>Control + s</b>. After you press
<b>Control + s</b>, enter the theme name and press <b>Control + s</b> again
for it to jump to the matched text. The theme names are above the
preview picture in the first website; for the second website, you
replace the space by dash character. For example, if a theme named
"Lush Theme", to search for this theme in package list, enter
"lush-theme".</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-orgheadline10" class="outline-2">
<h2 id="orgheadline10"><a id="ID-4630f65a-b6c6-4ad2-813b-829d44477bca"></a>General basic Emacs commands</h2>
<div class="outline-text-2" id="text-orgheadline10">
<p>
I took some parts from my other guide <a href="emacs-tutor.html">Emacs Mini Manual (PART 1) - THE
BASICS</a>. I recommend you to follow the mini manual when you have time.
</p>
</div>
<div id="outline-container-orgheadline11" class="outline-3">
<h3 id="orgheadline11"><a id="ID-4bb52c4a-1d30-42e0-999b-b18d8831997e"></a>Command</h3>
<div class="outline-text-3" id="text-orgheadline11">
<p>
In Emacs, every user interaction is a function execution. You press a
key to insert a character, Emacs runs <code>self-insert-command</code>. There are
two types of functions in Emacs:
</p>
<ul class="org-ul">
<li><b>Normal functions</b>: These are like functions in other
programming languages, and are used for implementing features in
Emacs. Users do not need to care about these functions, unless
they want to implement something or modifying the
implementations.</li>
<li><b>Commands</b>: Commands are like functions, but interactive. It
means, commands are features provided to users and users
directly use them.</li>
</ul>
<p>
<code>execute-extended-command</code> is bound to <b>M-x</b>.
</p>
</div>
</div>
<div id="outline-container-orgheadline12" class="outline-3">
<h3 id="orgheadline12"><a id="ID-c428fffd-3636-43e4-916e-9bc67c48db4e"></a>Emacs Key Notation</h3>
<div class="outline-text-3" id="text-orgheadline12">
<p>
Taken from here: <a href="http://www.emacswiki.org/emacs/EmacsKeyNotation">EmacsWiki</a>
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Prefix</th>
<th scope="col" class="org-left">Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><code>C-</code></td>
<td class="org-left">(press and hold) the Control key</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>M-</code></td>
<td class="org-left">the Meta key (the Alt key, on most keyboards)</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>S-</code></td>
<td class="org-left">the Shift key (e.g.‘S-TAB’ means Shift Tab)</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>DEL</code></td>
<td class="org-left">the Backspace key (not the Delete key). Inside Emacs, <code>DEL</code></td>
</tr>
<tr>
<td class="org-left"> </td>
<td class="org-left">is written as <code><backspace></code>.</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>RET</code></td>
<td class="org-left">the Return or Enter key</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>SPC</code></td>
<td class="org-left">the Space bar key</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>ESC</code></td>
<td class="org-left">the Escape key</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>TAB</code></td>
<td class="org-left">the TAB key</td>
</tr>
</tbody>
</table>
<p>
A notation such as <b>C-M-x</b> (or, equivalently, <b>M-C-x</b>) means press and
hold both Control and Meta (Alt) keys while hitting the <b>x</b> key. From
now on, I won't say something like "Press <b>M-x</b>" anymore. For example,
if I say "<b>C-x C-f</b> your files", you should replace <b>C-x C-f</b> with its
command like this in your head: "<code>find-file</code> your files". All commands
use verbs, I think, so don't worry. Try to recall the command from the
key binding; it will help you get used to Emacs quicker. One exception
though, I only say "press <b>key</b>" if <b>key</b> is a single character on the
keyboard.
</p>
<p>
If you see <code>M-x command</code>, it means you need to <b>M-x</b> and type
<b>command</b>.
</p>
<p>
A prefix key is a part of a full key binding. For example, a full key
binding is <b>C-x r l</b> to run the command <code>bookmark-bmenu-list</code>, then
<b>C-x</b> and <b>C-x r</b> are its prefixes. Note that key sequence such as
<b>C-x</b> and <b>M-x</b> are considered a single character. Knowing prefix key
is handy: if you forget key bindings of some commands that use the
same prefix key, and remember the prefix, you can press the prefix key
and <b>C-h</b> to get a list of commands with that prefix.
</p>
<p>
For example, <b>C-x r</b> is the prefix for <i>register</i> and <i>bookmark</i>
commands in Emacs. However, you forget a specific key binding for a
command in those features. <b>C-x r C-h</b> list all key bindings that have
prefix <b>C-x r</b>.
</p>
<p>
Finally, <b>C-g</b> executes the command <code>keyboard-quit</code>, which cancels anything
Emacs is executing. If you press any key sequence wrongly, <b>C-g</b> to
cancel that incorrectly pressed key sequence and start again.
</p>
<p>
As you gradually learn Emacs, you will see the key bindings are really
systematically organized and mnemonic. Whenever you see key bindings
end with <b>n</b> and <b>p</b>, it usually means <b>next</b> and <b>previous</b>; <b>o</b>
means <b>open</b>; <b>h</b> means <b>help</b>; <b>C-h</b> is standard prefix for help
commands; key bindings such as <b>o</b> and <b>C-o</b> are frequently used in
many built-in tools such as <i>Dired</i>, <i>Ibuffer</i>, <i>Occur</i>…
</p>
</div>
</div>
<div id="outline-container-orgheadline13" class="outline-3">
<h3 id="orgheadline13"><a id="ID-f9a9dca0-7c8f-45bd-9ace-da5d6bb27577"></a>Emacs Keys are easy to remember</h3>
<div class="outline-text-3" id="text-orgheadline13">
<p>
The key bindings have a few simple and easy to remember rules:
</p>
<ul class="org-ul">
<li><b>C-x</b> prefix is for default and global bindings that comes with
Emacs.</li>
<li><b>C-c</b> prefix is for users to define.</li>
<li><b>C-u</b> is for altering behaviors of commands. That is, one command
can behave differently depend on how many <b>C-u</b> you pressed
first before executing a command. Mostly you just have to hit <b>C-u</b>
once.</li>
<li><b>C-<number></b> like <b>C-1</b>, <b>C-2</b>… is similar to <b>C-u</b>, but passing a
number to a command. Usually, the number specifies how many times
you want to repeat a command.</li>
</ul>
<p>
You will learn about <b>C-u</b> and <b>C-<number></b> in <code>Prefix Arguments</code>
section.
</p>
<p>
Most commands can be organized in an easy to remember way. For
example, command like <code>helm-do-grep</code> (the command belongs to <code>Helm</code>, a
3rd party extension to Emacs) can have a key binding like <b>C-c h
g</b>. The <b>h</b> stands for <code>Helm</code> and <b>g</b> stands for <code>grep</code>. So, key
bindings are not difficult to remember.
</p>
</div>
</div>
<div id="outline-container-orgheadline14" class="outline-3">
<h3 id="orgheadline14"><a id="ID-9f33c953-75d4-4418-a1fb-7a27ff17c276"></a>Some basic commands</h3>
<div class="outline-text-3" id="text-orgheadline14">
<ul class="org-ul">
<li><p>
Open file: <b>C-x C-f</b> and browse to the file. You can fuzzy match
candidates in current directory, e.g "fob" or "fbr" will complete
"foobar".
</p>
<p>
<b>C-l</b> to go back to parent directory and <b>C-j</b> to go down to current
highlighting file/directory; alternatively, you can use arrow keys.
</p>
<p>
<b>C-z</b> to see a list of actions that you can apply on highlighting
candidate, <b>TAB</b> to view a file content without visiting it.
</p>
<p>
<b>C-c o</b> on a file to open that file in another pane (in Emacs, pane
is called <b>window</b>).
</p>
<p>
<b>C-c d</b> on a file to delete it.
</p></li>
<li>Open recently opened files: <b>C-x b</b> contains a list of opened files and
<b>recently</b> opened files. To move between these lists, use left/right
arrow keys or use <b>C-o</b>. You can type part of a desired filename and
narrow down.</li>
<li>Close file: <b>C-x k</b>, select a buffer to close. Buffers are simply
your opened files, for now. By default, your current editing
buffer is at the top. Press <b>RET</b> to confirm killing the buffer.</li>
</ul>
</div>
<div id="outline-container-orgheadline15" class="outline-4">
<h4 id="orgheadline15"><a id="ID-C405EA55-1F5B-4828-A83D-4EC96C5B6AD1"></a>Basic motion commands</h4>
<div class="outline-text-4" id="text-orgheadline15">
<p>
These key bindings are also used by popular shells such as <b>bash</b> or
<b>zsh</b>. I highly recommended you to master these key bindings.
</p>
<ul class="org-ul">
<li>Move forward one char: <b>C-f</b> (f stands for <b>forward</b>)</li>
<li>Move backward one char: <b>C-b</b> (b stands for <b>backward</b>)</li>
<li>Move upward one line: <b>C-p</b> (p stands for <b>previous</b>)</li>
<li>Move downward one line: <b>C-n</b> (n stands for <b>next</b>)</li>
</ul>
<p>
The above operations can also be done with arrow keys. if you don't
like the above key bindings, the arrow keys offers equivalent features.
</p>
<ul class="org-ul">
<li>Move to beginning of line: <b>C-a</b></li>
<li>Move to end of line: <b>C-e</b></li>
<li>Move forward one word, <b>M-f</b>.</li>
<li>Move backward one word, <b>M-b</b>.</li>
</ul>
<p>
These key bindings are in Emacs only:
</p>
<ul class="org-ul">
<li>Scroll forward one screen: <b>C-v</b>, <b>page down</b></li>
<li>Scroll backward one screen: <b>M-v</b>, <b>page up</b></li>
<li>Move to the beginning of a sentence: <b>M-a</b></li>
<li>Move to the end of a sentence: <b>M-e</b></li>
<li>Recenter a screen: <b>M-l</b></li>
<li>Re-position cursor to the top, middle and bottom of the current
screen: <b>M-r</b></li>
<li>Move to top of the buffer: <b>M-<</b></li>
<li>Move to end of the buffer: <b>M-></b></li>
<li>Move to the nth character: <b>M-g c</b> (c stands for <code>character</code>)</li>
<li>Move to the nth line: <b>M-g l</b> for Emacs < 23.2, <b>M-g g</b> for emacs >=
23.2) (l/g stands for <code>line</code>)</li>
</ul>
<p>
Recenter means making the current line the cursor is on the center of
your screen.
</p>
<p>
You can quickly highlight a region by pressing <b>C-SPC</b> and use motion
commands to select a region.
</p>
<p>
<b><span class="underline">Exercise</span></b>: execute the above commands using the key bindings at
least 10 times or until you remember. You can perform these motion
commands on any buffer.
</p>
</div>
</div>
<div id="outline-container-orgheadline16" class="outline-4">
<h4 id="orgheadline16"><a id="ID-84B3D9CC-C246-4D3C-9022-49CB47813094"></a>Incremental search</h4>
<div class="outline-text-4" id="text-orgheadline16">
<p>
So, you want to look for something in the buffer? <b>C-s</b> invokes
<code>isearch-forward</code>, allows you to look forward from the current point
for something. After <b>C-s</b>, you are prompted to enter the content to
search for. Enter the content, and press <b>C-s</b> repeatedly to travel
through the matches forward.
</p>
<p>
Similarly, <b>C-r</b> invokes <code>isearch-backward</code>, allows you to look
backward from the current point. Press <b>C-r</b> repeatedly to travel
through the matches backward.
</p>
<p>
<code>isearch</code> can be invoked from any valid buffer.
</p>
</div>
</div>
<div id="outline-container-orgheadline17" class="outline-4">
<h4 id="orgheadline17"><a id="ID-32f76107-37b6-4ce4-b4ca-2fe106e768be"></a>Basic editing commands:</h4>
<div class="outline-text-4" id="text-orgheadline17">
<p>
In Emacs, <code>kill</code> means <code>Cut</code> in other editors. These key bindings also
work under the terminal.
</p>
<ul class="org-ul">
<li>Kill a character at the cursor: <b>C-d</b></li>
<li>Kill entire line: <b>C-S-DEL</b> (remember, <b>DEL</b> is your <b><backspace></b>
key), or <b>C-w</b> (not in stock Emacs)</li>
<li>Kill forward to the end of a word from current cursor: <b>M-d</b></li>
<li>Kill backward to the beginning of a word from the current cursor:
<b>M-DEL</b></li>
<li>Kill all spaces at cursor: <b>M-\</b></li>
<li>Kill all spaces except one at cursor: <b>M-SPC</b></li>
<li>Kill to the end of line: <b>C-k</b></li>
<li>Kill a sentence: <b>M-k</b></li>
<li>Undo: <b>C-/</b></li>
<li>Redo: <b>C-?</b></li>
<li>Open a list of killed texts: <b>M-y</b> (not in stock Emacs)</li>
<li>Duplicate line/region: <b>M-c</b> (not in stock Emacs)</li>
<li>Indent whole buffer: <b>C-c i</b> (not in stock Emacs)</li>
</ul>
<p>
When you kill something, the killed content is put into the Kill Ring.
</p>
<p>
If you write code, you can also quickly add comments or
comment/uncomment code with <b>M-;</b>:
</p>
<ul class="org-ul">
<li>If you do not highlight a text region, <b>M-;</b> adds a comment to the
end of line.</li>
<li>If you do not highlight a text region and comment is at the end of
line, <b>M-;</b> comments the whole line (not in stock Emacs).</li>
<li>If you do not highlight a text region and your current line is being
commented, <b>M-;</b> uncomments the whole line (not in stock Emacs).</li>
<li>If you highlight a region (i.e. with a mouse), <b>M-;</b> comments out
the region.</li>
</ul>
</div>
</div>
<div id="outline-container-orgheadline18" class="outline-4">
<h4 id="orgheadline18"><a id="ID-839730d6-81a3-46df-89df-f96d2df4d624"></a>Buffer commands</h4>
<div class="outline-text-4" id="text-orgheadline18">
<p>
Buffer is where you edit your file content. Buffer holds content
of a file temporarily. Anything you write into the buffer won't make
it into file until you explicitly save it with <code>save-buffer</code>
command. <b>C-x C-s</b> executes the command <code>save-buffer</code>, so you can <b>C-x
C-s</b> your files. You can also execute this from <code>M-x*</code>
</p>
<p>
To save a buffer as other file ("Save As" in other editors), <b>C-x
C-w</b>, which runs the commands <code>write-file</code>.
</p>
<p>
To kill a buffer, <b>C-x k</b>. If you want to kill the current buffer,
<b>RET</b> immediately. Otherwise, type into the prompt the buffer name you
want to kill.
</p>
<p>
In the above section, I said that point is in your file,
well, actually point is not in a file but a buffer. From now on,
keep file and buffer two separate and distinct concepts. When I say
file, I refer to physical file and when I say buffer, I refer to the
temporary content of the file that is being displayed.
</p>
<p>
<b><span class="underline">Exercise</span></b>: Practice <b>C-x b</b> to get used to it.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Key</th>
<th scope="col" class="org-left">Binding</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><code>C-x C-s</code></td>
<td class="org-left"><b>Command</b>: <code>save-buffer</code></td>
</tr>
<tr>
<td class="org-left"> </td>
<td class="org-left">Save the buffer at point</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>C-x C-w</code></td>
<td class="org-left"><b>Command</b>: <code>write-file</code></td>
</tr>
<tr>
<td class="org-left"> </td>
<td class="org-left">Save the buffer to a different file</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>C-x b</code></td>
<td class="org-left"><b>Command</b>: <code>switch-to-buffer</code></td>
</tr>
<tr>
<td class="org-left"> </td>
<td class="org-left">Switch to a different buffer</td>
</tr>
</tbody>
<tbody>
<tr>
<td class="org-left"><code>C-x k</code></td>
<td class="org-left"><b>Command</b>: <code>kill-buffer</code></td>
</tr>
<tr>
<td class="org-left"> </td>
<td class="org-left">Kill a buffer. <b>RET</b> to kill the</td>
</tr>
<tr>
<td class="org-left"> </td>
<td class="org-left">currently active one</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="outline-container-orgheadline19" class="outline-4">
<h4 id="orgheadline19"><a id="ID-f988d119-d683-4568-8b38-cb3b700b7abf"></a>Window commands</h4>
<div class="outline-text-4" id="text-orgheadline19">
<p>
Unlike other editors, Emacs can split your frame area into multiple smaller
areas. Each such area is called a <code>window</code>. You can divide a frame
into as many windows as you want and each window can have anything in
it, i.e. your current editing buffer, file management buffer, help
buffer, a shell… Basically anything that Emacs can display. Let's try them out:
</p>
<p>
<b><span class="underline">Exercise</span></b>:
</p>
<p>
<b>C-x 2</b> to split the current window into two horizontal windows. After
splitting, you will have the exact duplicate of your current editing
buffer. <code>split-window-below</code> is bound to <b>C-x 2</b>.
</p>
<p>
<b>C-x 3</b> to split your current window into two vertical windows. After
splitting, you will have the exact duplicate of your current editing
buffer. <code>split-window-right</code> is bound to <b>C-x 3</b>.
</p>
<p>
Now, after you execute the two commands above, you will have three
windows: two above and one below. Each window can hold a buffer. With
the above two commands ,you can create arbitrary window layout. In
Emacs, a window layout is called a <b>window configuration</b>.
</p>
<p>
To navigate through the windows, use <b>C-x o</b> which runs the command
<code>other-window</code>. Try cycling around the windows a few times to get used
to it.
</p>
<p>
In Emacs, <code><next></code> is the <b>PageDown</b> key, <code><prior></code> is the <b>PageUp</b>
key. <b>M-<next></b> runs <code>scroll-other-window</code> and scroll the other
window forward; <b>M-<prior></b> runs <code>scroll-other-window-down</code> and scroll
the other window backward. Other window is the window that you visit
when <b>C-x o</b>.
</p>
<p>
<b>C-x 0</b> closes the window at point.
</p>
<p>
<b>C-x 1</b> closes all other windows except the currently selected
one. Try creating another window, try <b>C-x 1</b>. Only in my
distribution: <b>C-x 1</b> again opens previous window layout.
</p>
</div>
</div>
</div>
</div>