Skip to content

earshinov/vscode-sort-lines-by-selection

Repository files navigation

Sort Lines by Selection

This extension allows you to sort lines based on the text selected in each line. It is a port of the Sort Lines By Selection plugin for Sublime Text 3.

Demo

The functionality is available via the Command Palette: Ctrl+Shift+P > Sort Lines by Selection.

When there is only one selected region, the extension will sort the entire lines that the selection spans, similarly to the built-in "Sort Lines Ascending" and "Sort Lines Descending" commands.

Natural Sort

By default the extension compares selected strings lexicographically, which means that 10 will come before 9. If you want numbers embedded in selected strings to be sorted as numbers, use Ctrl+Shift+P > Sort Lines by Selection (Natural Sort) command.

There is no all-in-one solution for which parts of a particular string should be treated as symbols and which as numbers. So the extension does not try to be super-smart and only supports the following:

  • Whole numbers
  • Fractional numbers with the decimal part separated with . (period)
  • Negative numbers preceded with -

The following is not supported:

  • Hexadecimal numbers
  • Numbers written in scientific format (1e6)

Configuring Keybindings

The commands come without default keybindinds, but you can easily add them. Run Ctrl+Shift+P > Preferences: Open Keyboard Shortcuts (JSON) and paste something like this:

{
  "key": "f10",
  "command": "sort-lines-by-selection.sortLinesBySelection"
}

The command takes optional arguments listed in the section below.

To use natural sort, specify the natural_sort argument:

{
  "key": "f10",
  "command": "sort-lines-by-selection.sortLinesBySelection",
  "natural_sort": true
}

You can change the f10 key to anything you want. Also, keep in mind that all keybindings in the file must be separated with commas.

Optional Command Arguments

case_sensitive (default: false)

Sorting is case-insensitive by default. To override, specify "case_sensitive": true:

{
  "key": "f10",
  "command": "sort-lines-by-selection.sortLinesBySelection",
  "case_sensitive": true
}

natural_sort (default: false)

Specify "natural_sort": true to use natural sort:

{
  "key": "f10",
  "command": "sort-lines-by-selection.sortLinesBySelection",
  "natural_sort": true
}

morph

A custom "morph" function can be applied to the selected strings before sorting. Please see the description of the Sublime Text plugin for more details. One difference is that for this extension the morph function has to be written in JavaScript rather than in Python.

By way of example, here is a command that sorts lines by their selected strings reversed:

{
  "key": "shift+f10",
  "command": "sort-lines-by-selection.sortLinesBySelection",
  "args": {
    "morph": "s.split('').reverse().join()"
  }
}

Enjoy!

Releases

No releases published

Packages

No packages published