/ tutorial.org
tutorial.org
1 #+TITLE: Emacs Beginner's Tutorial 2 #+AUTHOR: Michael Raitza 3 #+SUBTITLE: Barkhausen Institut, Dresden 4 #+OPTIONS: ^:nil 5 #+STARTUP: showall indent 6 7 * Introduction 8 9 Concise tutorial showing the absolute fundamentals 10 11 So you can follow along this class, take notes and work on exercises 12 13 There's also a [[https://www.youtube.com/watch?v=48JlgiBpw_I][video]] on this. (By somebody a lot more capable) 14 15 * Step 1 16 17 Open Emacs inside your VM 18 19 But leave it otherwise untouched for now 20 21 Open the application finder and type =emacs=. 22 #+attr_org: :width 75% :align center 23 [[file:img/0_finder.png]] 24 25 It will take several minutes to start for the first time 26 27 *Try this now!* 28 29 Your action begins every time I have talked beyond this catch phrase 🙂 30 31 * What do you see? 32 33 #+attr_org: :width 75% :align center 34 [[file:img/0_emacs.png]] 35 36 * Visual elements 37 38 ** *Buffer* 39 40 Everything you do, happens in some buffer 41 42 Each buffer has a *unique* name (e.g. **scratch**) 43 44 Determines the *content* you work with 45 46 47 ** *Window* 48 49 You can divide your workspace into several windows 50 51 Each window displays exactly one buffer at any time 52 53 Multiple windows can display the *same* buffer 54 55 *Exactly one* window is always active 56 57 Determines the *geometry* of a buffer 58 59 60 ** *Frame* 61 62 All windows are collected in a frame 63 64 Boundary to your desktop software 65 66 67 ** *Minibuffer / Echo area* 68 69 Displays feedback ("echoes") from Emacs to you 70 71 E.g. the pressed key sequence =M-s-<Sys_Req> is undefined= 72 73 Is the *command interaction space* between Emacs and you 74 75 76 ** *Mode line* 77 78 Displays the buffer state 79 80 =U:---= :: most important 5 buffer states. E.g. 81 =U:---= :: buffer is unmodified 82 =U:**-= :: buffer is *modified* (i.e. has *unsaved* changes) 83 84 =*scratch*= :: buffer name 85 86 =All= :: where you are in the buffer. (You see *all* its content.) 87 88 =(4,0)= :: where your cursor is. (Line 4, at the start) 89 90 =(Lisp ...)= :: The active *modes* in this buffer 91 92 Modes control *how* you and Emacs interact with a buffer 93 94 * Command interaction 95 96 When you press a key that starts a key sequence, the minibuffer informs you about it 97 98 E.g. press ~Ctrl~ followed by ~x~ (while holding ~Ctrl~ pressed) 99 #+ATTR_ORG: :width 25% 100 [[file:img/0_minibuffer.png]] 101 102 Two things happen: 103 1. =C-x-= is displayed, telling you that Emacs is waiting for another key 104 105 2. minibuffer expands, displaying possible further keys and what command they perform 106 107 *Try this now!* 108 109 * Conversing with Emacs 110 #+ATTR_ORG: :width 25% 111 [[file:img/0_minibuffer.png]] 112 113 *Notice* 114 The echo area (bottom line displaying =C-x- (1 of 2) ...=) is your main communication interface 115 116 Most of the time Emacs *responds* to input / events on this line 117 118 So it should get your attention 119 120 * Key sequence notation 121 122 ~C-~ :: ~Ctrl~ key (left or right, does not matter) 123 ~M-~ :: (read "Meta") ~Alt~ key (only the left!) 124 ~S-~ :: ~Shift~ key (arrow upwards, left and right) 125 ~s-~ :: (read "Super") ~Windows~ key 126 127 '~C-x~' :: keep ~Ctrl~ pressed and press ~x~ 128 129 '~C-x u~' :: keep ~Ctrl~ pressed and press ~x~. Now *release* ~Ctrl~ and press ~u~ 130 131 '~C-x C-f~' :: keep ~Ctrl~ pressed and press ~x~ followed by ~f~ 132 133 '~C-M-i~' :: keep ~Ctrl~ *and* ~Alt~ pressed and press ~i~ 134 135 '~C-c C-x I~' :: keep ~Ctrl~ pressed and press ~c~ followed by ~x~. Now keep ~Shift~ pressed and press ~i~ 136 137 * Recovering from mistakes 138 139 Abort an ongoing key sequence or any interaction with Emacs by pressing 140 141 ~C-g~ 142 143 *Try this now!* 144 145 Terminate the ongoing key sequence '=C-x-=' by pressing ~C-g~ 146 147 The minibuffer displays =Quit= 148 149 * Run commands by name 150 151 You can run any available command by its name 152 153 ~M-x~ :: prompts you for a command name 154 155 *Try it now!* 156 157 Over 4000 available commands! 158 Type ~g l o SPC l i n~ 159 160 ~C-n~, ~C-p~ :: select the command =global-display-line-number-mode= 161 162 ~RET~ :: (the ~Return~ or ~Enter~ key) execute command 163 164 ~M-x~ :: again 165 ~TAB~ :: complete the last command (or any partial command you have typed) 166 167 * Getting help 168 169 See what key bindings are available in a buffer by pressing 170 171 ~C-S-h~ :: list major mode bindings 172 173 ~C-S-h C-h t~ :: list current top-level bindings 174 175 ~C-h~ :: general entry to Emacs' help system 176 177 e.g. ~C-h k~ :: asks for another key sequence and displays the associated command 178 179 *Try this now!* 180 181 *Question*: /What command does ~C-S-h~ map to?/ 182 183 Press ~C-S-h C-h t~ and then 2–3 times ~C-h n~ to find out 184 185 Then abort (/Which key sequence aborts?/) 186 187 * Open a file 188 189 ~C-x C-f~ :: Finds a file. Use also to create a *new* one 190 191 *Try this now!* 192 193 Open the file =tutorial.org= 194 195 Press ~C-x~ and use ~C-h n~ to find how to save a buffer 196 197 * Moving around 198 199 ~C-f~, ~C-b~ :: go *forward* or *backward* (from one character to the next) 200 201 ~M-f~, ~M-b~ :: go *forward* or *backward* (from one word to the next) 202 203 ~C-n~, ~C-p~ :: go to the *next* or *previous* line 204 205 ~C-a~, ~C-e~ :: go to the *start* (Anfang) or *end* of the line 206 207 208 ~C-v~, ~M-v~ :: scroll a whole page *down* or *up* 209 210 ~M-<~, ~M->~ :: go to *start* or *end* of the whole buffer 211 212 * Undo 213 214 To reverse a change you've made in the buffer, type 215 216 ~C-/~, ~C-x u~, ~C-_~ :: to undo the last change, the one before etc. 217 218 ~C-?~, ~C-M-_~ :: to redo a change you've just undone 219 220 *Try this now!* 221 222 Type some text 223 Move the cursor the cursor in-between edits 224 Remove some text 225 Play with undo/redo 226 227 228 *Observation* 229 Some key bindings are *mnemonic*, some *graphic*, some *spatial* and some based on *convention* 230 231 Often key bindings are *contextual* 232 233 * Delete 234 235 Instead of ~<deletechar>~ (~Delete~ on your keyboard) use ~C-d~ to delete a character 236 237 It is easier to reach, especially when moving the cursor with ~C-f~ etc. 238 239 (You are holding ~Ctrl~ already) 240 241 Use ~DEL~ (~Backspace~ on keyboard) to delete a character to the left 242 243 *Try this now!* 244 245 * Marking a region 246 247 ~C-SPC~ :: mark a region for copying (or some other action) 248 249 ~C-x h~ :: mark the whole buffer 250 251 *Try this now!* 252 253 Minibuffer displays =Mark set= 254 255 Move the cursor 256 257 Watch a highlighted region running from your cursor to the marked position 258 259 Abort by pressing ~...~? 260 261 * Cut / Copy / Paste 262 263 With an active region, type one of: 264 265 ~C-w~ :: cut ("kill") the region 266 267 ~M-w~ :: copy the region 268 269 270 ~C-y~ :: paste ("yank") the just copied/cut region 271 272 ~M-y~ :: select a previously copied/cut region for pasting 273 274 *Try this now!* 275 276 * Window management 277 278 ~C-x 1~ :: show only the current window 279 280 ~C-x 2~ :: split window horizontally (→ 2 flatter windows) 281 282 ~C-x 3~ :: split window vertically (→ 2 narrower windows) 283 284 ~C-x 0~ :: close the current window 285 286 ~C-x o~ :: (meaning "other") go to the next window 287 288 *Try this now!* 289 290 When you forget, type ~C-x~ and wait and inspect the list of commands 291 292 Notice that all windows show the same buffer! 293 294 * Buffer management 295 296 ~C-x C-<left>~, ~C-x C-<right>~ :: cycle to next / previous buffer 297 298 ~C-x b~ :: select a different buffer in the current window 299 300 ~C-g~ :: abort selection. Return to old buffer 301 302 ~C-n~, ~C-p~ :: select and preview different buffer 303 304 *Try this now!* 305 306 *Notice* 307 You are presented with a selection 308 309 The selected candidate is shown immediately as a preview 310 311 * A little cheatsheet 312 313 Use your knowledge to open the file =cheatsheet.org= in a buffer to the right of =tutorial.org=! 314 315 *Try this now!* 316 #+attr_org: :width 50% :align center 317 [[file:img/0_cheatsheet.png]] 318 319 *Hint* 320 Run command =fit-window-to-buffer= while the =cheatsheet.org= buffer is active to narrow it. 321 322 323 * Searching text 324 325 ~M-s M-s~ :: start search. Prompts for search string 326 327 While search is running: 328 ~M-s~ :: select a previous search term 329 330 ~C-n~, ~C-p~ :: select and preview a search result 331 332 ~RET~ :: go to search result 333 334 ~C-g~ :: abort and return cursor where search started 335 336 *Try this now!* 337 338 Start search and press the keys ~T r y~ 339 340 Find the location that is different and correct the mistake! 341 342 * Note taking 343 344 Take notes in =tutorial.org= directly! 345 346 While I am speaking! 347 348 This helps you getting used to Emacs for your exercises 349 350 * Org mode markup 351 352 Mark up text with simple characters 353 354 **bold** 355 ~~code~~ 356 ==typewriter== :: (verbatim) 357 358 //italic// 359 __underlined__ :: ~_~ (underlines) before and after 360 ++strikethrough++ :: ~+~ before and after 361 362 '~*~ ' :: a heading. Start line with ~*~ followed by space 363 364 *Hint* 365 If you forget, move cursor over marked-up text and its markers will appear 366 367 * Lists 368 369 Start a list with any of: 370 371 '~-~ ' or '~+~ ' or '~<n>.~ ' or '~<n>)~ ' (e.g. ~1.~ or ~1)~) 372 373 With cursor on list item: 374 ~S-<left>~, ~S-<right>~ :: cycle through list types 375 ~M-RET~ :: open a new item of the same type 376 *Notice*: Cursor at start of item, new item opened above. Use ~C-a~ to get there! 377 ~C-c C-c~ :: fix numbering 378 ~M-<up>~, ~M-<down>~ :: move sub-list up or down 379 ~M-S-<left>~, ~M-S-<right>~ :: change level of sub-list 380 381 *Try this now!* 382 383 1. item 1 384 2. item 2 385 + sub-item 1 386 + sub-item 2 387 3. item 3 388 389 * Todo lists 390 391 '~- [ ]~ ' :: add square brackets to make item a todo 392 393 With cursor on item: 394 ~C-c C-c~ :: toggle completion (and fix numbering) 395 396 *Try this now!* 397 398 1. [ ] pasta 399 2. [ ] veggies 400 - [ ] tomatoes 401 - [ ] garlic 402 3. [ ] cheese 403 404 *Notice* 405 Item =veggies= becomes partially/fully done when toggling its sub-items 406 407 * Code snippets / Examples 408 409 Start line with '~:~ ' (colon and ~SPC~) to turn the line into ~code~ 410 411 : This is all code. 412 : Has to be repeated for each line. 413 414 *Notice* 415 Examples are treated specially regarding line wrapping etc. 416 417 It makes sense to distinguish examples from prose text 418 419 * Hyperlinks 1 420 421 Enclose any text, like <<<anchor>>>, with triple angular brackets and press ~C-c C-c~ 422 423 Now, any mention of the word anchor turns it into a hyperlink 424 425 ~C-c C-o~ :: with the cursor on the link to jump to the definition 426 ~C-c &~ :: to return to the origin (watch the minibuffer!) 427 428 *Try this now!* 429 430 431 *Hint* 432 Use this to mark the definition of an important concept 433 434 * Hyperlinks 2 435 436 Put an <<anchr>> in the text 437 438 ~C-c C-l~ :: create a link with =anchr= as the link text at the =Insert Link:= prompt 439 440 With a discription, the [[anchr][this text]] is displayed instead of the [[anchr]] 441 442 *Try this now!* 443 444 ~C-c C-l~ :: with cursor over a link to edit 445 446 * What did you learn? 447 448 Emacs works in *conversation* with you 449 450 You *ask* something of it by issuing a command 451 452 It *responds* to you with messages or *requests* more input 453 454 You can *execute* commands or *read* about them via its comprehensive help system 455 456 457 * Congratulations 458 459 You are already learning *two* new computer languages! 460 461 1) Emacs command language 462 463 2) Org markup language 464 465 You should work through the [[elisp:help-with-tutorial][Emacs tutorial]] after today's session 466 467 Run command =org-open-at-point= with the cursor on the link to start the tutorial