Skip to content
Theme:

Sorting and filtering in Helix

I recently posted a quick tip about sorting in Helix that quickly became the second most popular subreddit post ever. This is a good indicator of two things: people found it helpful, and it should be a blog post. So here it is!

Helix comes with a built-in :sort command mentioned on the website, but its functionality isn’t well documented. It works on multiple selections and is as powerful as your multi-selection game. Another way of placing stuff in order is by piping a selection to the sort CLI program. Similarly, we can pipe a selection to whatever CLI you want. We can go crazy that way! Let’s have a look at a few examples.

Sort multiple lines (:sort command)

The simplest form of using :sort command is sorting multiple lines.

  1. % - Select everything
  2. Alt-s - Split lines into individual selections
  3. :sort - Sort command

Sort complex selection (:sort command)

Even though it looks a bit more complicated, the same method can be used on more complex selections as long as they are symmetrical.

  1. % - Select everything
  2. s user - Create multiple selection
  3. v jjgl - Expand selections
  4. :sort - Sort command

Sort multiple lines (sort CLI)

The sort takes a singular selection compared to the built-in :sort command. This produces the same output as the method in the first example.

  1. % - Select everything
  2. | sort - Pipe selection to sort CLI

Randomize order (sort CLI)

We can randomize content by piping stdin (single selection) to sort -R.

  1. % - Select everything
  2. | sort -R - Pipe selection to sort -R CLI

Sort, remove duplicates and count occurrences (uniq and sort CLI)

How about sorting the output in descending order, removing duplicates and adding a count of occurrences? No problem 😉

  1. % - Select everything
  2. | sort | uniq -c | sort -r - Pipe selection to sort | uniq -c | sort -r CLI to remove duplicates, acc counted and order lines in reverse order

I hope you see how powerful this is. Perhaps this post gave you some ideas about using these features in your projects. Feel free to share some of your favourite Helix tips and tricks with me in the comments below. Until next time, stay curious ✌️

Leave a comment

👆 you can use Markdown here

Your comment is awaiting moderation. Thanks!