Sorting a selection

Today at work a colleague suggested ordering a hash of key / value pairs alphabetically to provide a bit of order. I agreed and then set about figuring out how to do it in Vim. This was the starting hash:

{
  "foo" => 2,
  "bar" => 3,
  "baz" => 4,
}

Expecting some complex solution involving some form of regex searching, Vim surprised me with its built in sort functionality. All I did was highlight the keys within the object, and then hit :sort u:

{
  "bar" => 3,
  "baz" => 4,
  "foo" => 2,
}

The u means duplicate lines will be removed. :help sorting has a great run down of all the options.

The Vim wiki has some useful other things it can do, too.

comments powered by Disqus