A Nice AWK One-liner

How would you solve this problem?

Given a file of text lines, remove duplicate lines keeping the original order.

The first thing that springs to mind is using

sort | uniq

or perhaps

sort -u

but these solutions fail to maintain the original order.

Lazarus Lazaridis has a very nice solution that accomplishes the task with an AWK one-liner. I wrote about the power of AWK one-liners a couple of weeks ago so this is a nice coda. Lazaridis’ solution leverages AWK’s associative arrays so a similar solution is available in many other languages. Try, though, to implement it in, say, Python. It’s straightforward but it’s definitely not a one-liner.

Lazaridis’ post explains his solution in detail so it’s worth reading even if you aren’t an AWK user. He also explains how you can solve the problem with sort, although not in a trivial way.

Notice how the AWK solution leverages the two powerful features of AWK that I discussed in my AWK post: the implicit main loop and associative arrays. It’s amazing how many problems can be trivially solved by using those two features.

Posted in General | Tagged | Leave a comment

An Emacs Journey

Someone tweeted a link to a 6-month-old post by Murat Demirbas, a computer scientist at SUNY Buffalo. The post, My Emacs Journey, describes Demirbas’ use of Emacs and his embracing of Org mode. He’s been using Emacs for over 20 years and says that he has reached the stage where his Emacs knowledge is mostly held in his muscle memory. He says that if you ask him what the key sequence for some command is, he can’t tell you until he types it and observes what he typed. I experience something similar: if you give me a key sequence and ask me what it does, I’ll have to stop and think and possibly won’t know even though I use it all the time without conscious effort. As Demirbas says, the knowledge is held in my muscle memory.

He writes at length about how Org mode changed his workflow. Like me, he uses it for writing, task management, and even to organize his thinking. I liked how he uses Org for his writing. It reminded me of a technique I’d forgotten about. When he’s writing a paper and defines a term, he turns it into a radio-target so that every time he uses it in the sequel, the new use gets turned into a link back to the definition. That’s a natural thing to do but I always forget about it.

I spent a long time reading his post because he had several interesting links to other posts or information. Take a look at his post to see what I mean.

Posted in General | Tagged , | Leave a comment

Adding Your YouTube Subscriptions to Elfeed

If you’ve been around Irreal for a while, you know that I’m a very happy user of elfeed for my RSS needs. It’s easy, fast, and, of course, I can read my feeds from within Emacs. Just the other day, I was wishing that there were some way of adding my YouTube subscriptions to elfeed. As things stand now, I have to check each of the channels everyday. Yes, I know that Google will send me a notification of new videos but I’ve stopped using Gmail for the obvious reasons and only check it once a month. I should probably just forward it to one of my active accounts.

Over at Codingquark, Dhavan Vaidya shows how to add those YouTube subscriptions to elfeed and download the videos. I briefly thought about doing this but I didn’t know that you could export your subscriptions from YouTube so I couldn’t figure out where to look for new videos. Vaidya has all that handled and it turns out to be very easy to set up.

All you need is youtube_dl and, of course, elfeed. Add a little bit of Elisp to your init.el, export your subscriptions from YouTube, and you’re set to go. I’m really happy about this; it’s one more function absorbed into Emacs.

Posted in General | Tagged | Leave a comment

Markdown in Org-mode

Org-mode seems to be gobbling up everything having to do with text generation. Here’s the latest example:

That’s pretty neat. Given that Org markup is superior to Markdown—sorry Markdown partisans, it is—it’s a bit hard to envision a non-contrived use case for the functionality but they surely exist. One example might be if you want to integrate some existing Markdown into an Org document. Doubtless folks with better imaginations can think of others.

It’s no wonder that Org envy is spreading to users of those other editors. There are already several ports to some of those editors as well as a standalone version. Of course, Org documents are plain text so you can trivially use any editor to write them but it’s Org’s integration with Emacs that provides the wonderful display, text wrangling capabilities, and, of course, (executable) Babel code blocks.

Posted in General | Tagged , | Leave a comment

A Development Environment

If you’re a software engineer, one of the most important aspects of your working life is your development environment. You’ve got to be comfortable with your editor, your debuggers, your OS, your desktop, and the other important pieces of your software stack.

Evgeny Budilovsky over at Meta-x86 has an interesting post describing his environment. His post focuses on the three main legs of that environment:

  1. His OS;
  2. His Desktop;
  3. His editor.

Budilovsky uses Nixos because he likes the way it lets him manage his configuration and software installation. Take a look at his post for more on that.

For his desktop, he uses Xmonad, which I’ve written about before. If you haven’t already, watch the demonstration video linked from that post to see how powerful Xmonad can be. Budilovsky says he likes it because it’s configurable with Haskell in a way that reminded him of Emacs.

Emacs is, of course, his editor. He likes the way it’s configurable and can be personalized. Between Emacs and Xmonad he can largely avoid using the mouse. Not everyone likes that idea, I suppose, but I consider it a real win.

If you’re living in the Linux world, be sure to take a look at Budilovsky’s post. His setup seems very nice and may appeal to you.

Posted in General | Tagged | Leave a comment

Refactoring in Spacemacs

Jack of Some is back with another video. This one is about using Spacemacs to refactor code. For someone who’s content with vanilla Emacs and has no plans to move to one of the other configurations, I’ve been writing a lot about Spacemacs lately. This is justified with today’s video because it taught me several things I didn’t know about iedit that apply no matter what flavor of Emacs you prefer.

Ever since abo-abo turned me on to it, I’ve been a heavy user of iedit. I almost always prefer it to query-replace or query-replace-regexp. It turns out, though, that it’s much more flexible than I imagined and Jack demonstrates that flexibility in his video. If you’re not a Spacemacs user, the keysequences are different, of course, but bringing up the doc-string will show you their vanilla Emacs counterparts.

As an example, when I was writing the post on Org gems, I decided to change ‘6’ to “six” in the text so I fired up iedit. Unfortunately, the ‘6’ in the list got changed too. It was easy to fix it up, of course, but I thought it was too bad there wasn’t a way of turning iedit off for some occurrences of the target symbol. After watching Jack’s video, I realized that there was a way of doing it and that it was all documented in the docstring.

If you’re an iedit user and are not aware of all of it’s functionality, be sure to watch the video and visit the documentation.

Posted in General | Tagged | Leave a comment

More Org Gems: Tables

Just after I published yesterday’s post on Org-mode Gems, I discovered that Cheong Yiufung has another post up with some more Org gems. This time he considers Org Table Gems. In this post he demonstrates three things about working with Org tables that you might not know:

  1. Operating on table regions.
  2. Converting a (semi-formatted) region to a table.
  3. Special markings to name rows, columns, and fields and to enable automatic recalculation.

As before, he has animations showing the gems in action. This time, however, he used mp4 instead of gif for the animation. That has certain advantages, I guess, but, for me at least, comes with a problem. I couldn’t figure out how to freeze the animation without having the video control bar cover the minibuffer. That’s a problem because I want to follow what he’s typing and sometimes it’s necessary to freeze the video to read a longer command.

That’s a small nit, of course, and Yiufung more than makes up for it with a little extra at the end. He shows how he used Dired to bulk convert his animations from webm format—which doesn’t work well with the Safari browser—to mp4. It’s a nice workflow and worth studying because the same ideas are portable to other bulk conversions or, really, any command line action on multiple files.

Posted in General | Tagged , | Leave a comment

Org-mode Gems

Every Org-mode user is aware that Org is a huge package stuffed with all sorts of useful functionality. For most of us, a lot of that functionality remains unknown and hidden. Most often the only way to find out about it is to read the Org-mode manual but that can be an undertaking.

Luckily, Cheong Yiufung has done the reading for us and made a very nice post that reveals some of the gems he found. The post mentions six pieces of functionality and illustrates them with animated GIFS. The six items are:

  1. Avoid inadvertently editing in a folded area.
  2. Show headings only.
  3. Clone trees with a time shift.
  4. Hide empty lines between headings in collapsed view.
  5. Turn lists into subtrees.
  6. Demote sequence for list bullets.

He also mentions a couple of miscellaneous items.

I’ve been a heavy Org user for many years but all six of those items were new to me. With Org, as with Emacs, there’s always something else to learn. Perhaps I should just RTFM from cover-to-cover but I’ve never had much luck in reading whole manuals. Fortunately in this case, Yiufung has done that for me. If you’re an Org user, you really should spend a few minutes reading Yiufung’s post. It’s a great post and definitely worth your time.

Posted in General | Tagged , | Leave a comment

Another Org-mode with Spacemacs Tutorial

Nathan Lovato has a tutorial on using Org-mode with Spacemacs. It’s longer and more comprehensive than Jack of Some’s introductory video on Org with Spacemacs, but as with that video, Lovato’s looks at Org from the Spacemacs user’s point of view. If you’re not a Spacemacs user, you probably won’t find the tutorial too helpful. If you are a Spacemacs user or are thinking of becoming one you might find it useful.

After a short introduction, he covers creating and manipulating nodes, widening/narrowing, lists, and checkmarks. Then he moves on to using Org for task management, including TODO keywords and time tracking.

Next, he takes up using sparse trees, column view, tags, and sorting. He approaches these from the point of view of searching and organizing your data. I, at least, usually forget about sparse trees and column view but that’s a mistake because they can be very useful for accessing your data. Lately, I’ve been making a point of using it just to burn it into my muscle memory.

Finally, he shows how he exports his Org data to his (Android) smart phone using Orgzly. Of course, if you’re living on the Apple plantation, Orgzly won’t work for you but there are other options such as beorg.

The best way to think of this tutorial is not as a how-to but as a demonstration of some of the things Org can do. The video moves quickly but Lovato provides a list of topics and their timestamp so you can easily revisit any topic you want to see again.

Posted in General | Tagged , | Leave a comment

Alzheimer’s and Gaming

Years (and years) ago, the usual crazies were running around making dire predictions about Rock and Roll and how it was going to destroy young people. It seems pretty ludicrous now but these people were serious and generally made pests (and fools) of themselves for many years.

Fast forward half a lifetime and another bunch of crazies were (or, perhaps, are) running around screaming that video games are going to be the death of our youth. Perfectly useless wastes of time. Maybe they should be outlawed. And, well, you know…

Now in an especially delicious piece of irony, CNN is reporting that researchers have been using a video game to diagnose early stage Alzheimer’s disease. The diagnoses, it turns out, are more accurate than the standard medical tests. You can follow the link for the details but the TL;DR is that an early sign of Alzheimer’s is a subtle deficiency in spatial awareness and navigation skills. The game, which involves navigating through a maze, can detect these deficits years before the memory problems that we associate with Alzheimer’s appear.

The CNN article has a link to a paper describing the research where you can read the abstract. Sadly, the paper itself is behind a paywall. This is a fine opportunity for a rant about Open Science—especially science that we’ve already paid for—but I’ll forbear.

Posted in General | Tagged , | Leave a comment