top | item 31998244

Emacs’s Builtin Elisp Cheat Sheet

169 points| seansh | 3 years ago |masteringemacs.org

67 comments

order
[+] rollcat|3 years ago|reply
I have a love-hate relationship with Emacs.

The defaults are absolutely awful. The editor is pretty unusable without at least a hundred lines of ELisp: basic stuff like CUA, setting up fonts, disabling useless UI clutter, delete-selection-mode, rebinding a couple keys, setting up packages, explaining where to find coreutils on BSD systems, etc. And then you get to packages.

If you want to do anything remotely useful, you're gonna have to start installing random stuff from GNU ELPA, MELPA, etc. There's no package pinning or SemVer so you get what you get, packages sometimes break in mysterious ways. So you write more code to work around various broken stuff in packages.

I've gone through several iterations of throwing away my entire init.el and starting from scratch with just the bare minimum needed to get work done. At the current iteration, ELisp is still ~33% of all source code volume in my dotfiles repo[0]. I haven't pushed my latest effort to unbreak "jump to definition" (because apparently dumb-jump is deprecated??? and I need to use xref??? but it doesn't work???) so that'll likely go up.

I never wanted to write enough ELisp to justify calling it a program, but Emacs made me.

But then there is magit[1], which is so damn good, it makes every other VCS interface look like a sad joke. Which is problematic, because configuring Emacs itself is a bit of a sad joke. I want to just write code, not write code to make just writing code less of a pain. :(

[0]: https://github.com/rollcat/dotfiles

[1]: https://magit.vc

[+] iLemming|3 years ago|reply
The story is as old as Emacs itself. Many people (if not most) start using Emacs by treating it as "an editor". They think they have to configure it by tweaking settings and changing various options.

But Emacs is nothing but clay, an assortment of Lego pieces, building blocks, programmers' equivalent of IKEA furniture, DYI toolkit, etc.

It's not an editor, it's material for you to build your own editor.

> I never wanted to write enough ELisp

That's the mistake Emacs newcomers often make. If you truly want to master Emacs, there's no way around it - you have to accept Emacs Lisp. With all its flaws and incredible power.

If you hate Lisp because it's not like languages you learned and used before; it doesn't look like Python, Javascript or Ruby, then maybe you're missing something.

One needs to set aside their prejudice about parenthesis, learn structural editing commands, understand REPL-driven workflow, and then, once they are over that initial barrier, they may realize that all those [more popular] languages borrowed heavily from Lisp, and Lisp is an extremely nice programming language.

True craftsmen very often make their own tools. I am of course biased here, but in all honesty, every single professional I know, whom I can count as an Emacs power-user or even a master; Every single one of them is truly an incredible hacker.

ps.: "you" used here in the general sense, not targeted to the OC personally.

[+] TacticalCoder|3 years ago|reply
> The defaults are absolutely awful.

I find them awful too but many people are fine with these defaults.

> There's no package pinning or SemVer so you get what you get, packages sometimes break in mysterious ways. So you write more code to work around various broken stuff in packages.

There's however a way around it which you may like, seen your complaint... What I do, and it's not unheard of but definitely not the most common when it comes to Emacs, is that I load specific versions of packages. And I commit my entire Emacs setup in Git.

So when I upgrade packages (or install new ones) if anything goes wrong I can just checkout/restore a known fully working state.

There are trade offs when versioning your entire "emacs.d" directory but honestly I don't see many downsides. And I share that emacs.d between several user accounts / machines and any change I made are one "git pull" away.

When I start Emacs I don't check for packages / refresh package contents or anything like that: I only do that when I actually plan to upgrade packages (which I do maybe once every six months or so).

> I want to just write code, not write code to make just writing code less of a pain.

I'd say that's kinda a big selling point of Emacs though: you can write elisp code to make anything you do (not just writing code) less of a pain.

[+] richiebful1|3 years ago|reply
I've found using Doom Emacs to be a lot easier than handrolling an init file.

I understand the gripes though. Emacs really shines when you have a workflow that is poorly supported by mainstream tooling and you want to write your own. I don't know of any other editor that allows you to add keyboard shortcuts/commands/functionality that easily.

[+] beebmam|3 years ago|reply
I've been using emacs for org-mode for notes for a decade with emacs defaults. Works wonderfully and is absurdly simple to use
[+] abrax3141|3 years ago|reply
Exactly the difference between emacs and most of the software world is that for most of the software world your post would end at “The defaults are absolutely awful. The editor is pretty unusable.”
[+] msla|3 years ago|reply
I absolutely love the default Emacs keystrokes.
[+] jasonm23|3 years ago|reply
Doom emacs is excellent at getting you past that nonsense. Built around straight.el so semver, Sha, tag, branch pinning is fully covered.

If vim bindings aren't your bag that's not a problem either.

Worth a look.

(Source : Some old geezer who's been hand coding an emacs config since the 90s, Doom has allowed me to throw a lot of it away.)

[+] robonerd|3 years ago|reply
> basic stuff like CUA

I don't care for the defaults either, but I also don't care for CUA, so if you had your way the defaults would be just as objectionable to me as they are now. Since you can't make everybody happy, I think the best way forward is to continue the status quo, so at least you won't be forcing configuration on users who were previously fine with the default.

[+] pech0rin|3 years ago|reply
Mastering emacs is an amazing resource and one I have used countless times over the years. I have used emacs since my time in college (~14 years ago) and I still discover new things about it -- a lot from "mastering emacs". Anyway, thanks for all the great content!
[+] mickeyp|3 years ago|reply
Thanks! I'm very happy to hear that.
[+] abrax3141|3 years ago|reply
I have been using plain out-of-the-box emacs for literally 40+ years. (I wrote the initial version of one of the commands that’s now built-in.) I do probably a third of my data cleaning work in keyboard macros. I am so used to and efficient with plain emacs that most customization just seems like unnecessary decoration — with the exception of major language modes, because language specific fwd/bkwd (s)expr is so useful.
[+] thorw73m|3 years ago|reply
Emacs in itself is worth studying to become better developer. The best minds in the computer industry have worked on this editor's design. For example I used to think single namespace in elisp is bad. But after used it for a while it feels having single namespace is probably good for cognitive ease. In contrast C++ offers namespace but when "using namespace" is used at the top of the file, the rest of the file contains only part of the namespace. This makes it harder to grep while reading source code. Although emacs have longer symbol names its cognitively much easier.
[+] zasdffaa|3 years ago|reply
Emacs docs aren't perfect but they are a major, major strength of it and I wish so much other software would learn from it. The devs are really responsive too - good guys!
[+] thorw73m|3 years ago|reply
C-h r C-s <search_term> - This almost always helps if not google - "<search_term> emacs wiki"
[+] yc20220630103|3 years ago|reply
Personally as somebody who is NOT a software dev Emacs is my favorite tool on Linux and I usually have 1 or 2 windows open at all times. Tramp in particular is like magic, I can natively browse remote directories, copy and edit files and run a terminal remotely through eshell. This is STILL something that VSCode struggles with, the Microsoft "Remote" extension has /bin/bash hardcoded in and fails if the remote machine doesn't have it.
[+] vdm|3 years ago|reply
What machine does not have /bin/bash
[+] rtpg|3 years ago|reply
I bought Mastering emacs on a whim after reading it but am still looking for more in-depth stuff about working with Emacs Lisp in particular.

I can do basic stuff alright, being able to jump into source in particular is very helpful to at least find snippets of code doing what I want, but I don't have a good sense of how to work effectively in it.

(For example, when trying to test things out I haven't really found a way much better than typing into scratch, selecting code and running it while staring at messages....)

I would gladly pay money to watch somebody just write and debug emacs lisp code for half an hour cuz I feel wanting for usability when trying to work with this.

[+] tra3|3 years ago|reply
> (For example, when trying to test things out I haven't really found a way much better than typing into scratch, selecting code and running it while staring at messages....)

Are you talking about when you're noodling, trying to figure how things work, or actually trying to build something?

For playing around, I found that scratch works ok, but I found a better workflow.

I end up using a daily note in org-roam, with #begin_src elisp... I then tag the heading with :REFILE:ELISP: so I can always find it later. Basically evaluate everything inline within that org-babel block.

When I'm building something, or driving towards a specific goal, I use buttercup [0] to write actual unit tests. If I squint, it kinda looks like TDD.

Finally, for debugging of running elisp, take a look at edebug [1]. It's a pretty standard looking debugger (if you used something like gdb). By default emacs uses debug which is not as friendly.

[0]: https://github.com/jorgenschaefer/emacs-buttercup

[1]: https://www.gnu.org/software/emacs/manual/html_node/elisp/Ed...

[+] todd8|3 years ago|reply
There’s already been some good replies to your comments, but I want to throw in one more simple suggestion. You can learn a lot about Emacs lisp by reading and modifying other developers config files. Pick a few of the more complete well documented configurations and go through them picking out useful parts that you can assemble into your own config. You will end up with just what you want and will learn more eLisp along the way.

I’ve done this for years and found it very helpful. Also learn how to use the scratch buffer — e.g. you can write a function and try it out and see it’s output easily with [C-j], that’s bound to the eval-print-last-sexpr function.

The built in help is amazing, there are at least 50 commands that provide some form of help or documentation, I use 3 very heavily: [C-h f] that’s describe-function, [C-h v] describe-variable, and [C-h k] describe-key.

[+] kettunen|3 years ago|reply
As an Emacs user, I definitely enjoy always finding new things about it. Thanks to these kinds of discoveries I've been able to make my .emacs.d smaller and smaller in tiny increments due to being able to replace various packages downloaded from ELPA/MELPA with built-in ones.
[+] submeta|3 years ago|reply
Excellent! This is a wonderful function I did not know about. - I learn a lot about Emacs from Mickey Petersen, his book "Mastering Emacs" (highly recommended), and his articles on r/emacs. Thank you again for this article.
[+] tut-urut-utut|3 years ago|reply
Emacs is an amazing editor and never ceases to surprise in a positive way.

The only complaint I have about Emacs is its look and feel and a lack of a usable Sidebar. If they could "steal" that from Sublime Text it would be perfect.

[+] arkanane|3 years ago|reply
I think that the look and feel is what I like more about Emacs. Having such a complete environment full screen without much space waster makes me much more focused
[+] mickeyp|3 years ago|reply
Do you mean like `M-x speedbar'? Lots of third-party customizations for it, too.