Skip to content

Instantly share code, notes, and snippets.

@plexus
Created July 29, 2014 21:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save plexus/aa8d40b194bd551d2b84 to your computer and use it in GitHub Desktop.
Save plexus/aa8d40b194bd551d2b84 to your computer and use it in GitHub Desktop.

Emacs Lesson 1

Keybindings Cheatsheet

Getting out
C-x C-cExit emacssave-buffers-kill-terminal
C-gCancel running or partially typed commandkeyboard-quit
qClose window (in read only buffers)quit-window
Working with files
C-x C-ffind (open) filefind-file
C-x kkill (close) bufferkill-buffer
C-x C-sSavesave-buffer
C-x sSave Allsave-some-buffers
Working with windows
C-x 0Minimize windowdelete-window
C-x 1Maximize windowdelete-other-window
C-x 2Split window horizontallysplit-window-below
C-x 3Split window vertically
C-x oGo to other windowother-window
C-x bSwitch to specific bufferido-switch-buffer
C-x C-bList all bufferslist-buffers
S-<arrows>Move around multiple windows
Getting help
C-h kDescribe keydescribe-key
C-h fDescribe function/commanddescribe-function
C-h tOpen tutorialhelp-with-tutorial
C-h KShow manual for key commandInfo-goto-emacs-key-command-node
C-h mDescribe current major and minor modes
Copy paste
C-SPC“start selecting text”set-mark-command
C-w“Cut”kill-region
M-w“Copy”kill-ring-save
C-y“Paste” (yank)yank
“Paste previous ones” (cycle)yank-pop
C-k“Cut till end of line” (repeat to kill mutliple lines)kill-line
Working with Lisps
C-x C-eEvaluate previous expressioneval-last-sexp
C-M-xEvaluate outer expression
Clojure
C-c M-jStart a REPL in the current projectcider-jack-in
C-c M-nSwitch to current namespacecider-repl-set-ns
C-c M-cConnect to a REPL already running in a terminalcider-connect
Stop the REPLcider-quit

Theory

Emacs

Function, Commands, Key bindings

Emacs is essentially a virtual machine that runs Lisp code. The editor functionality is written in Lisp, and other “apps” can be created that run on the Emacs platform. (Try M-x tetris or M-x doctor). This is why Emacs is praised for its extensibility, you can program the editor itself from within the editor.

To do so you define functions. Functions that are meant to be executed by the user are called commands, they have to have (interaction) as the first expression within the function body.

You can try this in the scratch buffer that Emacs starts with.

(defun go-down-five-lines ()
       (interactive)
       (forward-line 5))

Put your cursor after the last closing parantheses and type C-x C-e. Now you’ve executed the function definition, meaning the function (command) is now defined. You can execute it with M-x go-down-five-lines.

To be more useful we can bind it to a key.

(global-set-key (kbd "<f9>") 'go-down-five-lines)

Is this case we set a global key binding, so we can use the key to invoke the command from anywhere in Emacs. Some bindings are specific to a certain context, some commands might only be active when editing Lisp files, or Ruby files, or when viewing the contents of directories.

Help

Emacs has various help systems built-in, you should be able to learn everything about Emacs from within Emacs. All these help commands are bound to key combinations starting with C-h, for example, C-h f will prompt for a function. You can find out what it does, and what key, if any, it is bound to. Other notable help commands are

C-h ?List all help commandshelp-for-help
C-h kDescribe keydescribe-key
C-h KShow manual for keyInfo-goto-emacs-key-command-node
C-h iOpen the Emacs manualinfo
C-h tOpen the (pretty old school) tutorialhelp-with-tutorial

Exercises

  • What does the command zap-to-char do?
  • What key is it bound to?
  • Try using it
  • What command is bound to the key M-SPC? (meta-space)
  • What does it do?
  • Jump to its description in the manual.
  • Try using it
  • In the cheat sheet at the top some keybindings or command names are missing, look them and fill them in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment