Symex (pron. "sym-ex", pl. symexes): A Lisp symbolic expression, which describes a computation to be performed.
An evil way to edit Lisp symbolic expressions ("symexes") as trees in Emacs.
Symex mode (pronounced sym-ex, as in symbolic expression) is a vim-inspired way of editing Lisp code as trees. Entering symex mode allows you to reason about your code in terms of its structure, similar to other tools like paredit and lispy. But while those packages provide a curated number of useful tree operations, symex mode treats the tree structure explicitly so that arbitrary tree navigations and operations can be described using an expressive DSL, and invoked conveniently via a vim-style modal interface. As a consequence of this:
- Symex provides many novel features, such as "leap branch," "climb/descend branch," "goto highest/lowest," "skip forward/backward", recursive indent, recursive evaluate, among many others
- Implementing new structure-related features in general is easy .
- Keybindings are short and memorable
At the moment, symex mode uses
lispy, and evil-cleverparens to provide much of its low level functionality. In the future, this layer of primitives may be replaced with a layer that explicitly uses the abstract syntax tree, for still greater precision.
|||As long as, from a theoretical perspective, the intended traversal can be accomplished using a finite automaton. More complex traversals can be implemented (such as "leap branch"), but not as easily. Symex may be made Turing-complete at some point in the future, if there is interest in a feature that cannot be implemented in the DSL in its current form.|
- Install the package the usual way via MELPA (e.g.
- Then, assuming you're using use-package to manage your configuration, add the following config to your
init.d(note these instructions have changed as of version 0.9):
(use-package symex :config (symex-initialize) (global-set-key (kbd "s-;") 'symex-mode-interface)) ; or whatever keybinding you like
This provides a keybinding to load the symex editing interface, and also enables the symex minor mode in all recognized lisp modes (the minor mode ensures that manual edits respect the tree structure, e.g. keeps parens balanced like paredit).
The best way to learn how to use Symex is to read the Animated Guide. Besides animations, it also contains lots of helpful field notes. Go check it out!
Symex provides both an evil state as well as a hydra-based modal interface. Which one should you use?
TL;DR: Use the evil option -- this is the default, and it is available to both evil and vanilla emacs users. If you're learning and would like some hand-holding as you familiarize yourself with the keybindings, use the hydra option, but it is likely that hydra will eventually be deprecated here.
The evil option is less obtrusive and allows you to, for instance, execute
M-x commands without leaving symex mode. It should feel very similar to using Normal state, and doesn't interfere with normal Emacs usage including any custom keybindings you may be using.
The hydra operates almost identically to the evil state, but it provides a comprehensive menu that can be toggled on and off, and can therefore help you learn the keybindings as you go along. On the other hand, the drawback is that the hydra will exit if you do something not specifically connected to symex mode -- for instance, if you run an
M-x command, or do a text search, or save the buffer, or run a custom command of some kind. You could customize the hydra so that it is more persistent (e.g. "pink" or "amaranth" hydra) but doing so could cause it to interfere with normal Emacs functions, as hydra keybindings take precedence over everything else.
In short, evil provides a more seamless experience, but hydra may be a good option while you are learning to use symex.
Depending on your choice, put one of these in the
:custom section (not the
:config section) of your
The table below lists the key bindings in Symex mode for Evil Symex users. You don't need this with the hydra frontend since you can lookup the keybindings at any time by pulling up the hydra menu (default binding:
H-m). Also for the evil frontend, while you don't have the menu, you can always use Emacs's
C-h k to learn what a key does, as another way of learning the bindings.
||backwards, down, up, forwards|
||linewise down, up||don't rely too heavily on these -- e.g. "leap branch" is often better|
||traverse forwards, backwards|
||traverse forwards, backwards more||quicker ways to get around|
||skip forwards, backwards||quick ways to move forwards and backwards -- traverse without entering nested expressions|
||leap backwards, forwards||"leap" to adjacent branches in the current tree, preserving position on branch|
||soar backwards, forwards||leap, but crossing trees if necessary|
||climb, descend||a quick way to go up and down a tree|
||go to first symex at this level|
||go to last symex at this level|
||go to lowest, highest symex in the tree|
||insert at beginning, append at end|
||insert before, append after|
||open line below, above|
||create symex with indicated delimiter|
||wrap symex with indicated delimiter|
||cycle quoting, cycle unquoting, add quoting level, remove quoting level|
||wrap with parens and insert, wrap and append|
||delete, delete backwards, delete remaining|
||change, change remaining|
||yank (copy), yank remaining, paste after, paste before|
||change "surrounding" delimiter|
||move/shift symex backwards, forwards|
||move/shift symex backwards, forwards as far as possible on line or column||Remember that usually the Meta prefix
||emit backwards, forwards|
||capture backwards, forwards|
||swallow head, swallow tail|
||splice||clip the delimiters, joining the symex to the containing expression|
||insert newline before, append newline after|
||join with preceding line, join with next line|
||collapse to a single line|
||unfurl across multiple lines|
||collapse remaining, unfurl remaining|
||tidy||indent and remove extraneous whitespace|
||tidy remaining||tidy remaining symexes at this level|
||tidy recursively||tidies while traversing the symex from the highest branch to the root, for cases where a simple tidy isn't adequate|
||comment out, comment out remaining|
||evaluate, evaluate remaining, evaluate definition, evaluate recursively, pretty evaluate, evaluate as "thunk"||
||eval-expression||evaluate an arbitrary expression in the minibuffer|
||switch to a scratch buffer|
||display the messages buffer alongside|
||go to REPL|
||run/eval the buffer|
||evaluate, and insert result|
||describe / lookup documentation|
||enter insertion state|
Entering the symex modal interface (via e.g.
s-;) using the hydra option shows you a comprehensive menu of all possible actions, by default. This is helpful initially, but over time you may prefer to dismiss the menu and bring it up only on demand, in order to conserve screen real estate. To do this, either run
symex-toggle-menu via the menu entry point (
H-m) while in symex mode, or add this to your
init.d (e.g. in the
:config section of the
The default keybindings in symex mode treat increasingly nested code as being "higher" and elements closer to the root as "lower." Think going "up" to the nest and "down" to the root. But symex allows you to modify these or any other keybindings to whatever you may find most natural.
If you're using evil, put something resembling this in your configuration before the call to
(setq symex--user-evil-keyspec '(("j" . symex-go-up) ("k" . symex-go-down) ("C-j" . symex-climb-branch) ("C-k" . symex-descend-branch) ("M-j" . symex-goto-highest) ("M-k" . symex-goto-lowest)))
If you're using hydra, put something resembling this in your configuration after the call to
(defhydra+ hydra-symex (:columns 4 :post (symex-exit-mode) :after-exit (symex--signal-exit)) "Symex mode" ("j" symex-go-up "up") ("k" symex-go-down "down") ("C-j" symex-climb-branch "climb branch") ("C-k" symex-descend-branch "descend branch") ("M-j" symex-goto-highest "go to highest") ("M-k" symex-goto-lowest "go to lowest"))
When going up and down, the choice of initial position on the branch is arbitrary. By default, symex the squirrel remembers where it was on each branch as it goes up and down the tree, so you return to your last position when going up and down. If you'd like to move to the first or last position, you can use (for instance)
$ at each level, as usual, or traverse the tree using
b instead. If, on the other hand, you'd like to start always at the first position when going up (as it was in older versions of Symex), disable the branch memory feature by adding this to the
:custom section (not the
:config section) of your
C-, cycle through standard quoting and unquoting prefixes (
,@, respectively) recognizable to all Lisps. But some Lisps, such as Racket, provide additional quoting styles that you may want to add here. You could also technically add any prefixes here that you may find yourself using often, and they don't have to have anything to do with quoting. To add custom prefixes, add something like this to the
:custom section (not the
:config section) of your
(symex-quote-prefix-list (list "'" "`" "#'" "#`")) (symex-unquote-prefix-list (list "," ",@" "#,@"))
For evil users, when you "escape" from Insert state, you may prefer to enter Symex state rather than Normal state while in Lisp buffers. You could write one-off keybindings to do this, but if you'd like a more structured alternative, use Rigpa.
When you define macros in symex mode (e.g. via
q for evil users), make sure that the commands you use are those that have the same effect in every situation. For instance, the "up" and "down" motions (default:
j) could vary based on "branch memory" - up may sometimes move you to the first position on the higher level, but at other times it may move you to the third position, if that happens to be your most recent position. Using up and down in your macro would mean that it could have different results in each tree depending on your activities in the tree, unless you remember to reset the frame of reference by using something like
$. Instead, it may be more natural to use the "flow" traversal commands (default:
b), repeating them or prefixing them with count arguments if necessary, to move around in a fully deterministic way. This will ensure that your macros behave the same way in every case.
In writing Lisp code, parentheses are among the most commonly typed characters, and yet, these require us to leave home position dramatically to type! I recommend a keybinding resembling the following to make it more efficient. Of course this applies only in Insert state (for Evil users) or in vanilla Emacs state, as you can insert and modulate delimiters in other ways while in Symex state:
(define-key symex-mode-map (kbd "C-w") (lambda () (interactive) (execute-kbd-macro (kbd "("))))
You could think of "w" as "wrap" in this context, as in, "to wrap with parentheses," and it matches a similar binding in symex state (i.e.
w to wrap an expression and enter insert state). For the closing parenthesis, you could just use Emacs's
C-f to move forward a character -- since symex (via paredit) ensures that parentheses are balanced, you rarely need to actually type a closing delimiter. The binding
C-w would be fine for Evil users, but vanilla Emacs users may need to find something else here. Of course it goes without saying that the Control key should be conveniently accessible without having to leave home position. I have Control under my right thumb, and Escape in place of Caps Lock.
Learn more about the implementation and see some usage examples in the video overview (given at an Emacs SF meetup in 2019):
A little while ago I was discussing Lisp syntax with @apromessi:
Me: "...And so we have these sex-puhs..."
A: "Excuse me?"
Me: "Oh, I mean ess expressions! It stands for symbolic expression."
A: "Why not just call it sym-ex?"
Lisp has inherited a few oddball names from its deep prehistory, including the infamous
cdr for the
first and the
rest of the elements in a list. But S-expression / sex-puh / symbolic expression are all somewhat of a mouthful too. Here are a few reasons why we might want to consider using "sym-ex" instead:
"Symbolic expression": 6 syllables, long in written form too
"S-expression": 4 syllables, I find this name confusing at least partially because it is a single-letter acronym which is unusual. In addition, it is long in written form.
"Sexpuh" / "sex-p" / "sexpr": 2 syllables, short in written form. But I mean, these are terrible.
"s-ex": Speaks for itself.
"Symex": 2 syllables, short in written form, has normal linguistic analogues like "complex/complexes," and it's fun to say! Symex also sounds like Ibex, and that's obviously a plus.
This work is "part of the world." You are free to do whatever you like with it and it isn't owned by anybody, not even the creators. Attribution would be appreciated and would help, but it is not strictly necessary nor required. If you'd like to learn more about this way of doing things and how it could lead to a peaceful, efficient, and creative world, and how you can help, visit drym.org.