Quick Emacs/Guile primer
What’s this?
This post is a quick introduction on using Guile Scheme within Emacs, written mainly for future me. You’re welcome, future Rohan!
Why?
I tend to jump between languages a lot and my general-purpose editor of choice is Sublime Text. Lately, though, I’ve been programming in Guile Scheme for my Summer of Code project, which means I’ve been playing around in Emacs a lot. It’s fun and I really love the Emacs/Scheme workflow, but the only downside is that the shortcuts are a little different from those I’m used to in Sublime Text. Hence, this post. Although all this information is anyway available in the GNU Emacs guide and the Geiser guide, I’ve culled the unimportant stuff and condensed the useful bits into this blog post.
Quick Note
In the shortcuts that follow, C is Ctrl, M is Alt, RET is Enter and S is Shift. <mouse-11> is the left click mouse button, and <mouse-3> is the right click mouse button.
Starting out
Get Emacs 24 with apt-get install emacs24.
Then, theme it to your liking. This means creating a file named init.el and placing it in the ~/.emacs.d directory. Here are my settings, with comments explaining the configuration.
Once you’re done changing init.el, run M-x eval-buffer to tell Emacs about it - or just M-x eval-region to eval a particular sexp.
Next, install Geiser with M-x package-install RET geiser RET.
Basic Emacs stuff
C-gcancels the minibuffer command.C-x C-ssaves your work.C-x C-fvisits a file (which means it opens it if it exists and creates it if it doesn’t).C-x 0closes the current buffer.C-x 1closes everything except the current buffer.C-x-2splits the current buffer horizontally and duplicates it in the newly created window.C-x-3does the same thing as above, except vertically.M-x man RET pagename RETopens up the man pages forpagename.C-h iopens the info pages. This includes the info pages for Emacs, Geiser, Guile and many other GNU programs and software.C-h kdescribes a keybinding. For example, tryC-h k C-h k.C-x k buffername RETkills the buffer named ‘buffername’. Merely typingC-x kallows you to scroll up and down and select a buffer to be killed. Or you can type in the first few letters of the buffername and hit TAB to autocomplete.C-x b buffername RETvisits the buffer named ‘buffername’. Its behaviour is similar to that ofC-x k.M-;comments or uncomments a region, short forM-x comment-region.C-x lcounts the number of lines in the page, and displays in brackets the number of lines before and after the cursor position.
Using Geiser
Note: Geiser supports Racket and Chicken Scheme too, but those aren’t relevant to us right now.
M-x run-guilesummons a Guile REPL.C-c C-zswitches between the.scmfile currently being edited and this REPL.C-upgoes back in history at the Guile REPL.C-downgoes forward in history at the Guile REPL.
Dealing with sexps
Moving around
C-M-f(f = forward) jumps to the end of the sexp in front of the current edit point.C-M-b(b = backward) jumps to the beginning of the sexp behind the current edit point.C-M-S-fselects the sexp in front of the current edit point (Makes sense: Shift is used for selection).C-M-S-b: same thing, but for the sexp behind the current edit point.C-iintelligently indents the selected region. If no region is selected, it indents only the current line.
Guile and sexps
C-x C-eevaluates the sexp behind the current edit point.C-c C-bevaluates the entire buffer.
Commenting
- Putting “*;” before a sexp comments it out (as well as all contained sexps), while leaving the containing sexps intact.
Buffers
M-x buffer-menubrings up the list of active buffers. This is useful because sometimes Emacs decides to reuse a window carrying one of your useful buffers for something else, like showing error messages when Geiser evaluates an sexp, or showing minibuffer command completion candidates. Useupanddownto move around the buffers list, and hitRETwhen you find the buffer you were looking for.- If you just want to look at a buffer, but not interact with it, do
M-x view-buffer RETand then navigate to the buffer you want. C-x o(o = other) switches between visible buffers.C-<mouse-1>brings up a graphical buffer menu.
Viewing RFCs
- For reading RFCs, I use
irfc. Get it by following the instructions here. M-x irfc-visit RET 2131downloads RFC 2131 (if it isn’t already download) to the location specified ininit.eland opens it in the current buffer.