Emacs Redux

Return to the Essence of Text Editing

Clear Comint Buffers

| Comments

comint provides Emacs infrastructure for building command interpreters. It’s backing a lot of popular shell/REPL modes - like eshell, inferior-lisp, inf-ruby, inf-clojure, etc.

comint provides a myriad of built-in commands, but somewhat surprisingly it doesn’t feature a command to clear the contents of a comint buffer. Let’s write one such command ourselves!

While there are several way to tackle it, I feel this is the simplest (and the one that makes the best use of comint’s existing functionality):

1
2
3
4
5
6
7
(defun comint-clear-buffer ()
  (interactive)
  (let ((comint-buffer-maximum-size 0))
    (comint-truncate-buffer)))

;; let's bind the new command to a keycombo
(define-key comint-mode-map "\C-c\M-o" #'comint-clear-buffer)

Simple and elegant, right? One day it might even end up being part of comint itself.

Customizing the Fringes

| Comments

On graphical displays, each Emacs window normally has narrow fringes (gutters/margins) on the left and right edges. The fringes are used to display symbols that provide information about the text in the window. You can type M-x fringe-mode to disable the fringes, or modify their width. This command affects fringes in all frames; to modify fringes on the selected frame only, use M-x set-fringe-style. You can make your changes to the fringes permanent by customizing the variable fringe-mode.

Out-of-the-box the most common use of the fringes is to indicate a continuation line. When one line of text is split into multiple screen lines, the left fringe shows a curving arrow for each screen line except the first, indicating that “this is not the real beginning”. The right fringe shows a curving arrow for each screen line except the last, indicating that “this is not the real end”. If the line’s direction is right-to-left, the meanings of the curving arrows in the fringes are swapped.

Third-party modes like flycheck and diff-hl also make use of the fringe to display valuable information there (e.g. lint and VC information).

By default both fringes have width 8 pixels, but we can easily adjust this:

1
2
3
4
5
6
7
8
;; make both fringes 4 pixels wide
(fringe-mode 4)

;; make the left fringe 4 pixels wide and the right disappear
(fringe-mode '(4 . 0))

;; restore the default sizes
(fringe-mode nil)

As mentioned before, you can also invoke this command interactively and determine the optimal fringe size for you, before making it permanent in your config. The options presented by the fring-mode command are defined in the fringe-styles list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(defconst fringe-styles
  '(("default" . nil)
    ("no-fringes" . 0)
    ("right-only" . (0 . nil))
    ("left-only" . (nil . 0))
    ("half-width" . (4 . 4))
    ("minimal" . (1 . 1)))
  "Alist mapping fringe mode names to fringe widths.
Each list element has the form (NAME . WIDTH), where NAME is a
mnemonic fringe mode name and WIDTH is one of the following:
- nil, which means the default width (8 pixels).
- a cons cell (LEFT . RIGHT), where LEFT and RIGHT are
  respectively the left and right fringe widths in pixels, or
  nil (meaning the default width).
- a single integer, which specifies the pixel widths of both
fringes.")

Be careful when playing with the fringe size. Certain info doesn’t look very good when the fringe is too small (e.g. less than 4 pixels).

Lossage

| Comments

Are you sure you’re using Emacs in an efficient way? Perhaps you’re overusing commands like C-f and C-b to navigate around when you can be using more efficient alternatives like M-f and M-b (or a third-party package like ace-jump-mode). Gaining such insight is somewhat hard, as we generally don’t pay attention to our sub-optimal usage patterns. If we could only see what were the last few hundred keystrokes we entered in our current Emacs session… Turns out we can do this with the aptly-named command view-lossage (C-h l) (lossage means malfunction). It displays the last 300 keystrokes and its output looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
n e r a l l y SPC d o n ' t SPC n o t i c e SPC o u
r SPC s u b o p t i m a l SPC s u a g <backspace> <backspace>
<backspace> <backspace> u s a g e SPC p a t e <backspace>
t e r n s . SPC I f SPC w e SPC c o u l d SPC o n l
y <return> s e e SPC w h a t SPC w e r e SPC t h e
SPC l a s t SPC f <backspace> h e w SPC <backspace>
<backspace> <backspace> <backspace> f e w SPC h u n
d r e d SPC k e y s t r o k e s SPC w e SPC i <backspace>
e n t e r e d SPC i n SPC o u r SPC c u r r e n t SPC
E m a c s SPC s e s s i o n . . . SPC T u r n s SPC
o u t SPC <backspace> SPC w e SPC c a n SPC d o SPC
t h i s <company-dummy-event> . <backspace> SPC w i
t h SPC t h e SPC m a c <backspace> g i c a l <backspace>
<backspace> <backspace> <backspace> <backspace> <backspace>
<backspace> <backspace> SPC c o m m a n d <return>
` v i e w - l o s s a g e ` SPC ( b o u n <backspace>
<backspace> <backspace> <backspace> ` C - h SPC l `
) . C-p C-p M-q C-n C-n C-n C-e C-x b l o s s <return>
C-x b l o s C-g C-h l

Inspecting it can be super enlightening! I do this all the time.

Uptime

| Comments

Many Emacs users keep a single Emacs instance running for quite a while. I’ve had instances running for over a month. How do I know this? Unsurprisingly, there’s a built-in command for that:

1
M-x emacs-uptime

Now it’s time to start bragging to your vim-using friends!

Converting Between Symbols and Strings

| Comments

Sometimes you might need to convert a symbol to string (or vice versa) in Emacs Lisp. You start looking for functions like symbol-to-string and string-to-symbol but, alas, they do not seem exist. Do not despair! Such functions do actually exist, although their names are likely to surprise you:

1
2
3
4
(symbol-name 'some-symbol)
; => "some-symbol"
(intern "some-symbol")
; => some-symbol

Quickly Open an Info Manual

| Comments

Every Emacs user knows that Emacs ships with plenty of built-in documentation in the GNU info format (they don’t call it a self-documenting editor for no reason). Most Emacs users know how to access that built-in documentation with C-h i (M-x info) and some Emacs users even know that the Emacs manual can be opened directly with C-h r (M-x info-emacs-manual).

If you know the name of the manual you’re looking for, however, there’s a nice little-known alternative to using C-h i - the info-display-manual command. When you run it you’ll be prompted in the minibuffer for the name of the manual you’d like to view (manual name completion is available).

To give you a more concrete example of the command’s advantage over info let’s try to open the Emacs Lisp manual with both commands. With info you’ll have to type the following:

1
M-x info RET m elisp RET

And the alternative would be:

1
M-x info-emacs-manual RET elisp RET

If you like the command I’d suggest binding it to some keybinding:

1
(define-key 'help-command (kbd "C-i") 'info-display-manual)

Emacs Lisp Style Guide

| Comments

I’ve started working on a community Emacs Lisp style guide. My goal is to create a comprehensive document similar to the Clojure and Ruby community style guides.

Such a document would be a great starting point for many newcomers to the Emacs community and should increase the overall quality of the code produced by it. There’s much to be documented about code layout, preferred syntactic conventions, major and minor mode definitions, etc. and I obviously can’t know the best practices about everything so I’ll need a lot of help.

Of course, we already have the official coding guidelines, but they are a bit light on examples, rationale, etc. Hopefully the community style guide will manage to fill any gaps in them and in good time portions of it will find their place in the official documentation.

I’m inviting all members of our community interested in promoting good Emacs Lisp practices to join the development of this document!

A Peek at Emacs 24.4: Superword-mode

| Comments

In a previous post I wrote about camel-case aware editing with subword-mode. Emacs 24.4 adds a complementary minor mode called superword-mode, which also alters the behavior of word-based commands when enabled.

Normally Emacs would consider underscores and dashes word separators (snake_case and lisp-case anyone?). This affects all word commands - forward-word, backward-word, kill-word, etc. Let’s see a couple of examples (| denotes the cursor position):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
;; word with dash
|some-word

;; press M-f (forward-word) once
some|-word

;; press M-f again
some-word|

;; press M-b (backward-word) once
some-|word

;; word with underscore
|some_word

;; press M-f once
some|_word

;; press M-f again
some_word|

;; press M-b once
some_|word

;; word in camelCase (assuming subword-mode is not enabled)
|someWord

;; press M-f once
someWord|

;; word in camelCase (assuming subword-mode is enabled)
|someWord

;; press M-f once
some|Word

Personally I find the default behavior combined with subword-mode great. I do a lot of Ruby and Lisp programming and it also makes a lot of sense to me to be able to navigate the portions of a complex word, but I guess not everyone feels this way. Enter superword-mode - when it’s enabled all “complex/compound” words are treated as a single word:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
;; word with dash
|some-word

;; press M-f once
some-word|

;; word with underscore
|some_word

;; press M-f once
some_word|

;; word in camelCase
|someWord

;; press M-f once
someWord|

Note that you cannot have subword-mode and superword-mode enabled at the same time. Turning one of them on will disable the other.

Personally, I don’t see much value in superword-mode as a mode that’s enabled all the time, but I can imagine some useful scenarios in which I’d enable it briefly to do some focused editing.