Supercharge Your Workflow With Yasnippet

I’ve long been an enthusiastic Yasnippet user and have written about it a few times. Its canonical use case is in programming to fill in primitives such as the for loop in C or a class definition in Python but I rarely use it that way. I mostly use it with Org files to do things like create the header for blog posts, add boilerplate, do simple expansions like latex → LaTeX, insert daily checklists into my journal, and add code blocks to my blog posts.

These are all pretty simple operations that don’t begin to use all that Yasnippet has to offer. Jack of Some has an excellent video on how to superchage Emacs with Yasnippet. He does, of course, cover simple text substitutions but he also shows how to use its more advanced features.

One particularly nice example is his expansion of a Python class definition. As he adds arguments to the definition, the snippet automatically adds the code to initialize the instance with the arguments’ values. More generally, he shows how you can embed Elisp into the snippet template to do non-trivial things like insert the name of the current file into the text.

One thing I didn’t know how to do was to add “global snippets”. Usually snippets are only active for a particular type of file but it’s possible to have snippets that are always available. Take a look at the video to see how to do this.

The video is two years old but I just came across it. It’s definitely worth a few minutes of your time. It’s a few seconds over 12 minutes long so it should be easy to fit into your schedule.

Posted in General | Tagged | Leave a comment

A Better Dired Listing

Sooner or later almost every Emacs user discovers Dired and becomes a dedicated—or even fanatical—user. It’s basically a file manager but embedded within Emacs. As I’ve mentioned before, I tend to be an old-timey sort of guy so my natural inclination is to do my file manipulations on the command line just as I have for years but even I am a Dired convert and do essentially all my file work with it.

Nicolas Martyanoff is pretty much the same but he has a problem with the default listing. His complaint is that there’s a lot of unnecessary information, often formatted in an inconvenient way. That would be a minor annoyance except that it often causes the (arguably) most important datum, the file name, to wrap and be difficult to read.

Fortunately, Dired is reasonably configurable by setting various variables. Martyanoff took advantage of this to produce a nicer Dired listing. The dates are all in ISO format and file sizes are given in bytes rather than the generally useless number of file blocks. The resulting listing is more pleasant and easier to read as you can see from Martyanoff’s post.

Dired, of course, gets its information from ls so getting a better date and size format just means changing the arguments to ls, which are specified in one of the above mentioned variables. Sadly, the required changes are specific to the GNU version of ls so you’ll have to use it.

Martyanoff doesn’t mention if this effects Wdired but there’s no reason it should. That would be a showstopper for many of us. Other than that, this seems like a nice change that might be worth implementing. There is just a bit of Elisp involved so it’s easy to try it out and experiment with it.

Posted in General | Tagged | Leave a comment

Sussman Explains the End of SICP

If you’ve spent any time at all on Irreal, you know that I consider Structure and Interpretation of Computer Programs (SICP) one of the best—arguably the best—books on computer science. It’s influence was tremendous and served as the backbone of the introductory programming course at MIT for a couple of decades.

Then, seemingly suddenly, MIT abandoned SICP and started using Python to do things like control robots in their introductory course. Most of you oldtimers know that I have very strong feelings about this. Just ask Grant.

Still, this change was driven by Sussman and Abelson, the authors of SICP and the primary teachers of the course that was built on it, and we have to consider that maybe they knew what they were doing. Here’s Sussman, in a fairly recent video, explaining the decision to retire SICP. If you care about SICP at all, you should definitely watch it; It’s only 7 minutes long.

Sussman builds a great case for the power of the SICP approach but I’m less convinced by his rational for what replaced it. He describes the poking at “batteries included” libraries to figure out what they do as a sort of science. To me, it seems more like casting magic spells. Younger engineers using those libraries—almost always without understanding how they work—are just casting spells without any understanding of the magic behind them. I think a lot is lost with this approach but folks smarter than me—like Sussman—disagree. Still, we’re all entitled to our opinions and this is mine.

Posted in General | Tagged , | Leave a comment

Literature Review With Org-ref

If you’re a researcher or even a college student writing research papers, one of the arduous tasks waiting for you is the literature review. You probably start with a single paper and want to find related papers that address the same problem. In the past, this was a hard problem but the digitization of papers has made it easier.

John Kitchin has made it easier yet. I’ve written before about his excellent org-ref, a package that helps organize and automate references. The org-ref package has always supported literature review but recently Kitchin has added some new features that take advantage of OpenAlex to find

  • Similar papers
  • Citations of the paper
  • Papers listed in the references

It’s an excellent addition that makes literature review a little less onerous. The reason org-ref is so useful is that Kitchin wrote it for his own use as a researcher and it therefore meets real needs as opposed to theoretical needs that someone not involved with research might imagine. If you haven’t already watched Kitchin’s video on org-ref, be sure to take a look. If you’re a researcher it will be enough to make you take up Emacs. It’s a truly amazing piece of software.

I’ve long been a fan of Kitchin’s work and this latest addition to org-ref is a good reason why.

Posted in General | Tagged | Leave a comment

Code vs. Verbatim in Org Mode

If you do even a little writing in Org mode, you’ve almost certainly needed to occasionally typeset a word or sequence of words in a typewriter font. This, at least for me, comes up in two ways:

  1. I want to typeset something that would normally be part of a code buffer
  2. I want to typeset something like a file name or extension

To a first approximation, the distinction doesn’t really matter because they both end up as a typewriter font. But the two categories are different and Org distinguishes between them. For code items, you’re suppose to use a tilde:

~(setq variable value)~

For the other category—unhelpfully labeled “verbatim”—you’re supposed to use =:

On startup, Emacs reads the configuration file =init.el=.

It’s a great distinction but one that I, and probably many other people, ignore. I always use = for the simple reason that it’s easier to type than ~. After all, they’re typeset the same way so why bother trying figure out the correct one to use when = is easier anyway?

Still, in the back of my mind there’s a little voice that keeps saying, “What if it’s like Y2K? What if one day there’s an exporter that typesets them differently?” It seems like an unlikely, theoretical problem but then so did Y2k. Early programmers waved away the problem with the same sort of rationalizations that I’m using to ignore the difference between = and ~.

Some folks are more forward looking. Grant Rettke, for example, has a system that he adheres to and has even documented. His post, Choosing Between Code and Verbatim Markup In Org Mode, lays out his system and specifies which terms get the verbatim markup and which get the code markup. If you want to do the right thing, his post is a good place to start. Or you could be like me and suffer the consequences when the hammer falls.

Posted in General | Tagged , | Leave a comment

Org Plot Made Easy

As long term Irreal readers know, I was trained as a mathematician and later became a computer software engineer so you’d think I’d be pretty good at generating plots: Nope. I’m terrible at it. My go to tool is Gnuplot, which is powerful and flexible but hard to master for those of us who don’t use it regularly.

I’ve tried Org Plot as an intermediary but I couldn’t get decent results even though Org Plot is just a front end for Gnuplot. Charles Choi to the rescue. He has an excellent post on how to use Org Plot to make nice looking graphs.

His secret is leveraging YASnippet to take care of all the boilerplate that Gnuplot requires. That’s the real problem with Gnuplot. Setting it up requires a lot of very unintuitive specifications in an obscure configuration language. Choi’s solution is to make a YASnippet template for each type of graph so that it’s easy to insert it when needed.

Choi’s solution doesn’t stop there though. He also has a context-sensitive menu that’s activated when you click on an Org table. From there, you can pick the type of graph you want to plot and the appropriate boilerplate will be inserted. Another menu option will run Org plot and generate the graph. See his post for the details including the YASnippets and the Elisp code for the menu.

This is a nice solution for those of us who only occasionally generate graphs but still want them to look nice. And, of course, it’s yet another example of how Emacs lets you have it your way.

Posted in General | Tagged , | Leave a comment

Emacs: Everything’s a Buffer

It’s a commonplace that one of the features of Emacs is that everything’s a buffer and that this is a feature. Marcin Borkowski (mbork) has a short post that demonstrates the power that this brings.

The TL;DR is that he had a long file name comprised of several words one of which was hard to spell correctly. He wanted to check if he’d spelled it correctly and, if not, correct the spelling. Think for a moment how you’d solve that problem.

If you’re not an Emacser, your solution probably involves somehow isolating the file name, extracting the word in question, running it through a spell checker, and finally renaming the file either manually or through a file manager.

Borkowski’s solution was much simpler: he leveraged the fact that like everything else in Emacs, the Dired display is actually a buffer and therefore subject to the usual Emacs editing manipulations. He simply turned on Dired buffer editing, ran spell check on the word in question, and saved the results. All without leaving the comfort of Emacs. Take a look at Borkowski’s post for the details.

As Borkowski says, no other editor has this level of integration between the various subsystems. Buffers, like many things in Emacs, are easy to take for granted but they are the source of a lot of power.

Posted in General | Tagged | Leave a comment

The New Luddites: The New Frontier

The New Luddites have discovered a new frontier: Digital Minimalism. The term is actually very specific for the new Luddites. It means replacing their smartphones with a “feature phone”—a phone that does little more than make and take phone calls and maybe texts.

Over at Every there’s an article about this Quest for a Dumber Phone. Some of these people are obvious poseurs: they give up their smart phones for a while and then write books or even make YouTube videos about their experience. Others pretty clearly have addiction issues that go much deeper than their phones.

A close reading of the article makes it pretty clear that the real problem these folks are experiencing isn’t addiction to a smartphone but an inability to stop spending their lives on Twitter, Facebook, and Instagram. Rather than abandon their smartphones, they could simply delete these three apps from their phones. Even the digital minimalists admit it’s hard to get by without things like GPS to help them find their way around, Uber to order up a ride, and lots of other useful apps that no one wastes time on but that makes our lives easier.

The real reveal is at the end of the article where they admit the whole concept of Internet/smartphone addiction is controversial and without a substantive scientific foundation. A lot of it is the usual New Luddite nonsense. You could make the exact same arguments for doing away with electricity or any other modern convenience. I can, in fact, remember those same arguments being wielded against TV, rock and roll, and calculators.

I’m happy for these people leave the modern world behind and do without smartphones or anything else. I just wish they’d do it quietly and leave the rest of us alone.

Posted in General | Tagged | Leave a comment

Units in Emacs Calc

As many of you know, I’m a big fan of Emacs Calc and use it as my calculator of choice whenever I’m on my laptop. Calc has robust conversion capabilities—to convert miles to kilometers, e.g.—but it can be a bit tricky to use.

Not to worry. Nicolas Martyanoff has a very nice post that explains how to use units with Calc. You can do things like add meters to centimeters and simplify the results, convert between units, and even define custom units.

The hardest part for me is remembering the unit abbreviations. Calc, of course, will list them for you and you eventually learn the ones you use regularly. One odd thing about Calc is that the conversion between temperature units—Fahrenheit to Celsius, say—doesn’t work the way you think it would. If you try to convert 32°F to Celsius, you get 17.7777777778°C instead of the expected 0°C. The manual explains this as Calc treating the temperatures as relative temperatures so a change of 32°F corresponds to a change of 17.7777777778°C. What’s really going on, of course, is that most conversions involve a simple multiplicative constant whereas with temperatures there’s also an additive constant. Fortunately, Calc has a separate command (u t) to do temperature conversions.

If you’re a Calc user or just want a handy way of making conversions, take a look at Martyanoff’s post. It’s short and well worth a couple of minutes of your time.

Posted in General | Tagged | Leave a comment

The Emacs Help System

One of the really nice features of Emacs is its builtin help system, which often results in Emacs being described as the self documenting editor. Experienced Emacsers usually take it for granted but it’s really a wonderful thing. There’s no need for storing or losing manuals; it’s all there all the time as part of the application.

Old hands take advantage of this documentation without thinking about it but n00bs may wonder how to get started. Davide Mastromatteo has an excellent tutorial on how to access the Emacs help system and use it effectively. He starts by mentioning the builtin tutorial and recommends that everyone go through it at least once. Fortunately, Emacs will remember where you left off so you don’t to complete it in a single sitting.

Next he mentions that the Emacs manual is also built in (in info format) and easily accessed with the Ctrl+h r sequence. I find it more convenient to access the Web version of the manual but it’s nice to have it available when I don’t have Internet connectivity.

From there Mastromatteo moves on to the three help commands most of us use the most: describe-key, describe-function, and describe-variable bound to Ctrl+h k, Ctrl+h f, and Ctrl+h v respectively.

Finally, he mentions the appropos command (Ctrl+h a) to do fuzzy searches of the help commands. If you’ve been using Emacs for a while, you’ve doubtlessly internalized these commands by now. If you’re new to Emacs, Mastromatteo’s post is definitely worth your while.

Posted in General | Tagged | Leave a comment