Getting into the world of emacs

Someone should mention on the org-roam page that to install org-roam after requiring the melpa-stable package with this:

(require 'package)
(add-to-list 'package-archives
             '("melpa-stable" . "") t)

we need to run M-x package-refresh-contents or Emacs will keep getting lost when asked to install org-roam.


These are some commands Emacs thinks you should know right from the get-go. I’m saving them here for my later reference and for the benefit of anyone who might want to be a daredevil and skip reading the manual.

The trouble with such lists of shortcuts is that they are a dime a dozen and make little difference. I wrote these as notes while learning the commands, so these were originally meaningful to me and served as an effective learning tool. After the fact, this is simply a list that appears to encourage memorisation—which I do not myself encourage. Therefore, instead of as a tool for memorising, I see this list as serving two purposes: first, as shortcuts supported with my notes that helped me make better sense of things; and second, as a quick reference or reminder of something one would inevitably forget over time.

  • C–h to ask for help followed by a query; C–h ? to see all possible ways Emacs can offer help at that moment—both F1 and M–x help achieve the same result e.g. Try C–h c to get an option to type any command and be told what it does, so C–h c C–p will tell you what C–p does as a command name; similarly C–h k C–p gives a detailed description; but perhaps the most important is the apropos command or C–h a which lets you type in a keyword and search it in the docs amonst command descriptions
  • C–x d gets you into dired (the file system) which lets you do things like : e: to encrypt a file, : d to decrypt it, c to copy, d to mark for deletion with x and r to rename (which is essentially moving the file)
  • C–x C–s to save the current buffer to file, and C–x s to save all open, unsaved buffers one-by-one (useful for buffers in the background)1
  • C–b,f,n,p for moving backwards, forwards, down (next line) and up (previous line) which is really no different from using the arrow keys
  • C–v and M–v to page down and up respectively
  • C–SPC will start text selection and any action will stop it including the same keystrokes
  • M–t to transpose words (I wish we could do this backwards too)
  • C–x C–l to lowercase the selection (and C–u to uppercase)
  • C–u is for numeric commands e.g. C–u 8 C–n will move down 9 lines and C–u 7 DEL will delete seven characters—this is described as an “argument”, so if you are asked to “type in C–x f with an argument of 20” it means you type C–u 20 C–x f (which sets a 20-character margin for auto-fill-mode—see quoted paragraph further down this page).
  • C–/ is for undo, as is C–x u, and (weirdly) C–M–Shift–underscore is redo, which seems to really be an intentional command
  • C–x 1 will return to the window that existed before a splitpane appeared
  • C or M will delete by character/line or word/sentence respectively when used as just DEL (previous character), M–DEL (previous word), –d (next character or word), –k (kill2 cursor to end of line or sentence)
  • C or M will move by character/line or word/sentence respectively when used with –f (character/word forwards) or –b (character/word backwards), and –a (line/sentence backwards) of –e (line/sentence forwards)
  • C–w kills text selected (using C–SPC)
  • C–y is to “yank” a piece of text2, which is a great way to say “paste”—and it even means what we normally call pasting
  • M–y is a game-changer: it runs through a history of kills e.g. C–w or C–k a few times, followed by any work, followed by a yank using C–y will paste the most recent killed text but hitting M–y soon after replaces the yanked text with the text killed before it and hitting M–y again replaces it with the text killed even before that and so on3
  • C–x C–b shows all active buffers and C–x b allows you to specify a buffer to switch to
  • C–x k of course kills a buffer
  • M–x replace-string for find and replace (use C–g to quit halfway as usual)
  • M–x recover-this-file to restore a backup file if there is a system crash
  • C–x o switches to the “other” window when two windows are open in a single frame (to open a new frame use C–x 5 2 as described below and use C–x 5 o)
  • C–M–v to scroll the “other” window downards without switching to it; this is useful when the “other” window is purely for reference while actual work is being done on one window and the reference window needs to be scrolled—also use C–M–S–v to scroll the “other” window upwards
  • C–x kills windows but needs a specifier e.g. C–x 0 to close the current window and C–x 1 to keep the current window open and close the “other” window(s)4
  • C–x 5 2 opens a new frame (equivalent to a GUI window) and C–x 5 0 and C–x 5 1 both kill frames (current and other) same as C–x 0 and C–x 1 kill windows (see above)4
  • ESC ESC ESC is an all-purpose get out command; while C–g does this, quitting most things as you would like, when you are in recursive editing mode, C–g gets interpreted as a regular command and ESC ESC ESC alone will get you out

Emacs with org-agenda has proven to be of great use to me because I can note tasks, reading list additions etc. in the middle of meetings or other work (especially research since I am already in Emacs for that).

  • C–c C–t to pick task status
  • <yyyy-mm-dd Day repeat> for repeating tasks where the repeat can take three possible values: +1w for repeat in a week at any cost, or ++1w for repeat in a week at any cost in the future (the previous one repeats even if the ‘next’ week is still in the past), and finally .+1w for repeat one week after but this time relative to the date the task is marked as done rather than from the deadline.

A few other interesting points include the -:**- which means a buffer has changes not yet saved to a file. Once saved the asterisks vanish. There is an auto-fill mode (M–x auto-fill-mode) described as follows:

One minor mode which is very useful, especially for editing
human-language text, is Auto Fill mode. When this mode is on, Emacs
breaks the line in between words automatically whenever you insert
text and make a line that is too wide.

I am not currently too sure how useful that will be because it might insert linebreaks arbitrarily5, which can cause issues when I use pandoc at some point in the future. Some of my orgmode and markdown files end up as PDFs or—when there is a gun to my head—a Word file. I suspect auto-fill-mode will be a bit of an pickle then.

In auto-fill-mode, or most others with hard-wrapping, hit M–q to re-fill lines. This is especially useful when lines have been hard-wrapped and subsequently edited. It re-calculates the line and fills in extra whitespace.

Searching in Emacs is incremental: C–s starts a forwards search (in the forwards or downwards direction), C–s tabs through results, DEL tabs backwards through results until it reaches the first highlighted result at which point it starts deleting the search string by letter. Return terminates search same as C–g and most other C and M keystrokes. The same thing is true for C–r which does a search in the reverse direction. Searching with RegExp is possible with C–M–s and C–M–r for searching forwards and backwards respectively.

Tab completion like on a regular terminal works in the Emacs mini buffer (the tiny sliver at the bottom of a frame where commands get typed and shown). Finally, a quick note on terminology: a macOS window is an Emacs frame, and each split within a frame is what Emacs calls a “window” instead.

Here’s to holdovers.

  1. Similar but not quite, C–x C–c closes Emacs and offers to save unsaved buffers before closing down fully. 

  2. Kill seems to be the Emacs equivalent of cutting—it allows subsequent pasting of killed text. See also “yanking”. But there is a pretty great difference: multiple sequential kills will yank all kills at once but (a) this has to be strictly sequential as even C–f disrupts it, and (b) this does not seem to work with kills made using C–w even when C–k follows soon after. The real game-changer for this is M–y (see above).  

  3. Since M–y goes backwards along the “kill ring”, hitting M– – M–y will move it forward (M– – is the negative command that causes a change in direction). 

  4. A nice way to remember the usage of 0 and 1 here is to think of flipping a switch: C–x 0 will therefore turn off the window you are in and C–x 1 will keep it on (turning everything else off). For frames the same logic holds good with C–x 5 0 and C–x 5 1.  

  5. Not exactly arbitrary because the docs say “the margin is usually set at 70 characters, but you can change it with the C-x f command.” But this would look arbitrary when moved as-is into a word processor or some other such app. 

This is a note, a brief thought or reflection recorded for being meaningful or for sharing things of interest. Longer writings are in the essays section.