Skip to content
Jonathan Tsai edited this page Oct 18, 2019 · 6 revisions

Getting Started with Emacs: A Guide

Author

Jonathan Tsai (@jontsai), with help from others (see acknowledgements).

Intro and Mythology

There used to be an old joke, "Emacs is a great operating system that also comes with a decent editor." There is some truth to this — Emacs is a master process that allows multiple sub-processes, so it’s either quite powerful, or quite bloated, depending on whichever way you prefer to look at it. Ergo, it is recommended to eventually move from using a code editor like Atom or VSCode to a text editor such as Emacs or VIM.

The best software engineers in the world are general masters of either one of Emacs or VIM. The author of this article has chosen Emacs (blue lightsaber) over VIM (red lightsaber). Both can be quite formidable. However, in all of his journeys far and wide, the author has only encountered once a VIM practitioner who rivaled his power of the force, none other than the villainous Sith Lord Johnathan Ritzi (red lightsaber). In their duel, they were head-to-head, neck-to-neck, and trading blows back-and-forth, variously saber-locking at times. The two masters of the force eventually declared the duel a draw, and have withdrawn back to their caves to hone their skill and craft in their respective lightsabers.

Below are some tips on adjusting to the transition. Remember that There is no voodoo, only puppets on a string™, and trace through files one by one, function by function, don’t leap.

How do I get started?

Print out the Emacs cheat sheet double-sided (optionally laminate it or put it in a sheet protector), and tape it next to your monitor.

Basically, you want to get to the point of being able to memorize > 50% of the shortcut keys for Emacs, and once you develop the muscle memory, you will be a wizard. It is a critical skill to be able to program with your brain, not with your eyes. Wean yourself off of needing to look at a bunch of stuff. Train your brain to think deeply on one section of code at a time.

What are some of the recommended shortcut keys to learn first in Emacs?

  • Learn these first

    • C-z - suspend any Unix program, drop to the shell; then use ag to search for what you need. Once you have found you need, fg to resume Emacs.
      • you can use man or Google/Wikipedia how jobs, fg work to learn more advanced usage, i.e. man jobs, man fg
    • C-g to cancel whatever intermediate Emacs mode you are in, in case you accidentally triggered a multiple-keystroke command mode
      • hit this a bunch of times if you somehow messed up, and got into some weird state in the mini-buffer
      • Caution: If you press it too many times at the wrong time, you may cause a core dump of Emacs. This is a more advanced scenario that we won't cover now...
  • Opening files, closing files, working with buffers

    • C-x f C-f to open another file in Emacs. Repeat as many times as you'd like. The file you already had open will still be open in the background, in another buffer. You can switch back to it via C-x b.
    • C-x b to switch buffers; you can have multiple buffers open in Emacs at a time (which means, multiple files open, and switch between files). Practice switching between open files.
    • C-x k to kill a buffer in Emacs, or close a file, then follow the prompts in the mini-buffer
    • C-x 2 to split a window horizontally. Repeat as many times as you'd like.
    • C-x 3 to split a window vertically. Repeat as many times as you'd like.
    • C-x o to move from buffer to buffer in split window mode.
    • C-x 1 to make the current buffer/split window the primary window, and close all others.
    • C-x 0 to close the current buffer/window.
  • Basic Movement and Navigation within a File

    • C-p to move to a previous line
    • C-n to move to the next line
    • C-f to move forward one character
    • C-b to move backwards one character
  • Advanced Movement and Navigation within a File

    • C-s to search (forwards through the file)... type a phrase in the mini-buffer... repeatedly hit C-s to search for the next occurrence
    • C-r to search backwards through the file. C-s to switch directions to forwards.
      • C-g to stop searching if you were in search mode
    • C-l multiple times to move the current line to the middle, top, and bottom of screen
    • C-k to "kill" a line or "cut from cursor to end of line"
    • C-y to "yank" or paste a line from buffer (goes well with C-k)
    • C-space to mark (anchor to begin "highlighting" or "selecting" a region)
      • C-g to cancel mark/selection mode
    • C-w to "cut" out a selected region to "Emacs clipboard"
      • C-y to paste a previously killed or "cut" back into the buffer wherever the cursor is currently at
  • Emacs Meta Commands

    • TBD

FAQ

I kind of miss being able to look through all the files in one directory in a code editor. Is there any advice on how to get used to the transition from a code to text editor?

To look at files in a directory, just C-z to suspend and drop to the shell, and just use good ol' ls to list the files in the directory.

Also, from within Emacs, you can also do C-x C-f <tab> <tab> <tab> ... <tab> to autocomplete and show files in directory that match the substring you already typed.

For understanding the files, we recommend mapping out the directory architecture one time by hand (app/settings.py, app/urls.py, app/models.py, app/views.py, app/templates/, static/app etc) and just internalizing the model in your head.

Acknowledgements

Thank you to Angela Chang and others for reading drafts of this article and suggesting edits.