Sublime Forum

Sort options are too limited -- alternatives?

#1

As a *nix user I’ve used the command line sort tool often. One of its most useful options is -k, key, using which one can specify a position within the row as the key on which the sort is based, e.g., cat | sort -k 5 will output the contents of with its lines sorted by whatever is in the fifth position of every line. For columnar, csv, tab-delimited files this is brilliant, especially as it is not length or delimiter dependent. The sort options of SublimeText and all plugins I can find do not replicate this function.

Two alternatives come to mind:

  1. A new plugin that does this.
  2. A way to run that sort command on an open file (or selected text) within SublimeText.

I can perhaps create a plugin but such is many days (weeks?) away.

My query here is if there is a way of doing 2? At least one editor (e.g., medit) does provide the ability to create a command (macro?) that takes as its input the current file (or buffer), pass it to a command line tool, and then capture the tool’s output to a new (or the current) buffer. Can SublimeText do this? If so, please point me to the documentation that explains how.

Much thanks and regards.

0 Likes

#2

build systems will allow you to execute arbitrary system/shell commands and output the results in a new buffer (panel):
http://docs.sublimetext.info/en/latest/reference/build_systems/configuration.htm

you would need to save the file for this to work though, as build systems pass the file path as an argument to the command you specify in the build system, instead of piping the contents of the buffer.

1 Like

#3

You could test ShellCommand. Open the command palette ctrl+shift+p, type Shell Command, and write sort -k 5 works for me.

2 Likes

#4

Thanks kingkeith for the pointer to shellcommand. Works like a charm for *nix based operating systems. Less so for Windows. Took a bit of futzing but I discovered that I can type in the complete path to the cygwin sort.exe command and it will work.

0 Likes

#5

@gstalnaker Have you added the cygwin bin folder to the PATH? I am using Windows and it works without any problem.

0 Likes

#6

There is also a package specifically for csv files that allows sorting by a specific column.

0 Likes

#7

Thanks for all the replies. I’d shied away from modifying the PATH to put cygwin executables in the path before WinOS executables. That seemed to me unwise. I did investigate the cvs plugin and have used it, but it does require comma-separated syntax that the *nix sort command does not require. FYI - I also investigated using powershell, but its sort-like command has horrible syntax, etc. So much easier to type /sort.exe -k # :slight_smile:

About Build - My brain simply didn’t register that build must, by default, have access to system executables to “run” them against code. I was locked into macro/script and looked right past it. Thanks for the reminder that sometimes it’s just terminology.

0 Likes