A Quick Introduction to Awk

These days, my main hammer is Emacs and most tasks can be made to look like nails. Throughout my career, though, one of my go to tools was awk. It’s perfect for processing text files with its implied do loop that processes each line, its builtin pattern matching, and its wide selection of functions to work on text elements.

The definitive documentation is The AWK Programming Language book but that’s hard to find now and expensive when you can. Fortunately, awk is fairly easy to understand and the GNU Awk User’s Guide provides detailed documentation.

Yang Zhou has a very nice post that serves as a quick introduction to awk by demonstrating the common awk program patterns. It doesn’t begin to cover the richness of the language but it’s excellent for getting you started. If you’re not already familiar with awk, take a couple of minutes to read Zhou’s post. It’s a nice peak at a very useful scripting language and may inspire you to learn more about it. You might find it a powerful tool that can do all sorts of text jobs easily. And, of course, it’s available everywhere. You can even get the source for the original awk that should compile in most C environments.

Posted in General | Tagged | Leave a comment

Git Delta with Magit

I rarely use any of the diff utilities these days and when I do it’s mostly Ediff to resolve merge conflicts. That probably means I’m working too hard but I’ve always been that way: I just never think of using diff unless I’m really in trouble.

Most other folks are more reasonable and use some sort of diff regularly. A common use case is checking the differences between two versions of a file stored in a Git repository. Magit, of course, has that built in—several options, in fact—but Kaushal Modi really likes the syntax highlighted diff he gets with the command line tool delta.

Of course, Modi wants to stay in Emacs and use Magit. Fortunately, Dan Davison, the author of delta has a package, magit-delta, that allows it to work with Magit. Installation is pretty easy. Just install delta from your favorite package manager (it’s called git-delta in most package managers) and then install magit-delta by adding it to your init.el.

There is, apparently, an issue with displaying line numbers with git-delta (an issue has been raised) but it’s easy to work around that. Take a look at Modi’s post to see screen shots of both the normal delta and the magit-delta output. If you like what you see, just install delta and magit-delta and give them a try. It’s easy to turn it off if you decide you don’t like it.

UPDATE [2022-07-10 Sun 13:09]: Added link to Modi’s post.

Posted in General | Tagged | Leave a comment

DWIM Shell Commands

Álvaro Ramírez is relentless in chasing down ways to eliminate friction from his workflow. One day it occurred to him that invoking shell commands was an area with plenty of opportunities for workflow optimization. A particular problem was that many of the commands he used frequently, such as ffmpeg and convert have a lot of arguments and flags that are hard to remember.

Ramírez’s solution was a write tiny bits of Elisp that would invoke the desired commands without having to remember all the details. To make this work, he first defined a general template framework that allowed specifying what the command should look like with parameters for the file path and parts of the file name.

Because it’s DWIM, the framework does the right thing depending on the context. If you’re in a Dired buffer, the command will operate on all the marked files. If you’re in a file buffer, the command will operate on the associated file. Similarly, how the output of the command is shown depends on the context in which it was invoked. Take a look at Ramírez’s post for the details.

You can check out the framework code, dwim-shell-command.el, to see how things work but the examples in the post are really all you need. Once the framework is in place, adding new DWIM commands is simple. The post has several examples.

If you’re the type of Emacs user who’s always looking for ways to sand down the rough spots in your workflow, take a look at Ramírez’s post. As Ramírez says, if your needs are different, it’s easy to adjust things to suit your requirements.

Posted in General | Tagged | Leave a comment

Emacs As A Terminal Multiplexer

Torstein Krause Johansen runs Emacs in terminl mode on Linux. He likes to have several terminal sessions and switch between them. Previously, he used tmux to multiplex those terminal sessions onto a single screen but then tmux started crashing on him and he didn’t have the time or inclination to track down the problem.

Instead, he realized that he could do everything he wanted from within Emacs. That had the advantage of using the same key bindings that he was already using in Emacs and, less importantly, he didn’t have to worry about keeping the Emacs and tmux color schemes in sync. To accomplish that, he use vterm, multi-vterm, the built-in Emacs shell command, and a bit of custom Elisp, that he calls spawn-shell that allows him to create shells in named buffers the way he would in tmux.

Johansen has a video that explains how he does all this using just the packages and code above. It’s a really nice set up for those who like to run Emacs in terminal mode, of course, but even if you’re running Emacs in GUI mode, you can use the same strategy to provide multiple named shells all running from within Emacs. That’s pretty much what I do and it works well for me.

The video is short, only 5 and a quarter minutes so you can watch it pretty much any time you like. The slides are available but probably not too useful unless you want to copy the spawn-shell code. Definitely worth a few minutes of your time.

Posted in General | Tagged | Leave a comment

Additional Emphasis Markers in Org Mode

I really love the Org-mode markup language. It allows me to compose just about any document that I would actually want to write. That means that I can typeset bold, italics, underline, monofont, and even strikethrough. But some people have more imagination or are just weirder and want more ways of emphasizing text.

What additional ways would you want? The Emacs Notes blog has an answer. I can’t imagine myself ever wanting to typeset something like that example but as the post makes clear, the method is pretty general and allows you to specify emphasis markers that will use whatever font you like, in whatever color, weight, and size you desire.

The TL;DR is that you just need to load org-extra-emphasis.el and configure what you want the extra markers to produce. The blog post covers this in detail so take a look if you’re interested in this capability.

As I said, it’s highly unlikely I’d ever need this facility but it serves as a wonderful example of the extreme extensibility—or perhaps meta-extensibility in this case—of Emacs. In any event, if you need a bit more markup for Org mode, take a look at this post to see how to get it.

Posted in Programming | Tagged , | Leave a comment

A Regular Expression Conundrum

Here’s a little quiz to test your regex-fu. Suppose you want to match any of the strings

  • ab,cd
  • ab-cd
  • ab.cd

Someone proposes the regex ab[,-.]cd but you object that that’s incorrect because the - will be treated as specifying the range of characters between , and .. Oddly, the regex does match all three strings. Why is that?

See Paul Boyd’s post for the answer.

UPDATE [2022-07-05 Tue 14:04]:

I should have said that the regex matches all three strings and only those strings.

Posted in General | Tagged | Leave a comment

Neckties (again)

A few Fridays ago, the Irreal Red Meat Friday featured a Lifehacker article on why you should throw your neckties away. At the time, I mentioned that although most Irreal readers would be on-board with the idea of doing away with neckties, most of the commenters were offended with the idea although they could adduce no reason to support their position.

Antonio García Martínez reminds us of an earlier time when it was very unusual to see a man in pubic without a hat and that doesn’t mean a baseball cap. Men wore Stetsons, Fedoras, Derbys, or similar headgear. It was, in short, exactly the same situation we now find ourselves in with ties. Then John Kennedy chose not to wear a hat during his inauguration and everything changed.

As Martínez’s referenced tweet shows, not a single leader at the G7 wore a tie in the group photo. Martínez hints that this could be the bellwether for the end of neckties. One can only hope.

Posted in General | Tagged | Leave a comment

Getting Things Done With Emacs and Org-mode

Nicolas Rougier, whom I’ve written about before in regard to his beautiful Org documents, had a long post a couple of years ago that’s well worth a look and some serious studying. At the time, Rougier was just learning about Org-mode so the post is a step-by-step account of how he implemented a GTD process with Emacs and Org-mode.

Rougier is a busy guy. In addition to supervising five PhD students, being the editor of two journals, being active in several open source projects, he is also handling the usual academic researcher duties of negotiating the grant process, writing up his research, and organizing meetings.

He was committing the cardinal sin of using his email inbox as a TODO list but when he moved to Mu4e he rethought that and developed an Org-based process that leveraged the Org capture facility to be able to quickly capture random thoughts, ideas, or emails and put them into a holding file. Later, according to GTD orthodoxy, he moved those items to their appropriate project file and arranged for them to appear in his Org agenda in a convenient way.

His post is long and involved but worth studying. He’s got lots of good ideas and has thought through how to solve many of the problems anyone trying to implement a GTD process will face. If you’re looking for a way to get organized, Rougier’s post is definitely worth your attention.

Posted in General | Tagged , | Leave a comment

Emacs 28.2 Pretest

Stefan Kangas writes that the first pretest for Emacs 28.2 is out. If you’re conservative about the Emacs version that you use but don’t mind a little bit of adventure, this is a great way to experience the next great thing and help the developers test the next stable release.

It won’t be a news flash to Irreal readers but this announcement once again puts the lie to the notion that Emacs is moribund. Emacs development is not only robust but continues to introduce ground breaking improvements that—eventually—get copied by the other editors to much fanfare.

As always, thanks to all the developers and contributors who made this release possible.

Posted in General | Tagged | Leave a comment

Handling Org Tables in Elisp

Although it’s easy to forget, Org tables have another life besides their obvious textual representation. It’s obvious when you think about it because you can operate on those tables either through the built-in Org table operators or directly with Elisp. But what, exactly, is the data structure that Elisp operating on a table sees?

The secret to dealing with Org tables is understanding that they’re held as a list of lists. Each of the inner lists represents a row with its elements being the column values. The outer list is the list of rows. That can make the code dealing with a table a little fiddly.

Ben Smith has a nice post that illustrates the best way I know of dealing with the data in each row in turn. The TL;DR is that you use on of the map functions (-map in Smith’s case) to look at each row and then use cl-destructuring-bind to name each column’s data. After that you can operate of each column’s data as needed.

Most of the time, the built-in Org table operators give you all the functionality you need but sometimes it’s necessary to write some Elisp to get the job done. Best’s post shows that this needn’t be hard. The code snippet that Best shows is short and easy to understand so there’s no reason not to take a look if you ever have to deal with Org tables using Elisp.

Posted in General | Tagged , | Leave a comment