sQVe/sort.nvim

github github
editing-support
star 74
alert-circle 1
users 3
git-branch 0
CREATED

2021-11-15

UPDATED

5 months ago


🔠 sort.nvim

Sort is a sorting plugin for Neovim which provides a simple command that mimics :sort and supports both line-wise and delimiter sorting. Sort intelligently picks a sorting strategy, by using a configurable priority list, which minimizes manual input and should cover most sorting cases by simply running :Sort on a range.

❓ Why

  • Delimiter sorting.
  • Picks sorting strategy intelligently, by using a configurable priority list.
  • Minimize manual input.
  • Efficient and lightweight.
  • Utilize and mimic builtin :sort when possible.
  • Silence the nitpicker within you.

📦 Installation

packer

-- Lua.

use({
  'sQVe/sort.nvim',

  -- Optional setup for overriding defaults.
  config = function()
    require("sort").setup({
      -- Input configuration here.
      -- Refer to the configuration section below for options.
    })
  end
})

vim-plug

" Vim Script.

Plug 'sQVe/sort.nvim'

" Optional setup for overriding defaults.
lua << EOF
  require("sort").setup({
    -- Input configuration here.
    -- Refer to the configuration section below for options.
  })
EOF

⚙ Configuration

Sort comes with the following defaults:

{
  -- List of delimiters, in descending order of priority, to automatically
  -- sort on.
  delimiters = {
    ',',
    '|',
    ';',
    ':',
    's', -- Space
    't'  -- Tab
   }
}

📗 Usage

https://user-images.githubusercontent.com/2284724/145567686-3b52978c-58fe-4f32-ad27-c2b1060870ba.mp4

Sorting with Sort is easy via the provided :Sort command. Two different strategies are utilized depending on the visual selection:

  • Multiple lines

    All arguments provided to :Sort are feed to the builtin :sort command, and thus mirroring all features provided by the builtin sort. See :help :sort for usage and options.

  • Single line

    :[range]Sort[!] [delimiter][b][i][n][o][u][x]
    
    • With [!] the sort order is reversed.

    • With [delimiter] the delimiter is manually set instead of iterating over config.delimiters and sorting by the highest priority delimiter with results. Valid delimiters are:

      • Any punctuation character (!, ?, &, ...), matching the %p lua pattern character class.
      • s: Space
      • t: Tab
    • With [b] sorting is done on the first binary number in the word.

    • With [i] case is ignored.

    • With [n] sorting is done on the first decimal number in the word.

    • With [o] sorting is done on the first octal number in the word.

    • With [u] only keep the first instance of words within selection. Note leading and trailing white space isn't considered when testing for uniqueness.

    • With [x] sorting is done on the first hexadecimal number the word. A leading 0x or 0X is ignored.

⌨️ Keybinding

Sort does not by default set any keybindings. The following example sets go, which you can change to whatever you choose, to trigger :Sort:

" Vim Script.

nnoremap <silent> go <Cmd>Sort<CR>
vnoremap <silent> go <Esc><Cmd>Sort<CR>
-- Lua.

vim.cmd([[
  nnoremap <silent> go <Cmd>Sort<CR>
  vnoremap <silent> go <Esc><Cmd>Sort<CR>
]])

A common workflow, before sorting, is to visually select inside a set of characters with vi<character>. Instead of doing that manually before running Sort you could add the keybindings like:

" Vim Script.

nnoremap <silent> go" vi"<Esc><Cmd>Sort<CR>
nnoremap <silent> go' vi'<Esc><Cmd>Sort<CR>
nnoremap <silent> go( vi(<Esc><Cmd>Sort<CR>
nnoremap <silent> go[ vi[<Esc><Cmd>Sort<CR>
nnoremap <silent> gop vip<Esc><Cmd>Sort<CR>
nnoremap <silent> go{ vi{<Esc><Cmd>Sort<CR>

🤝 Contributing

All contributions are great and highly appreciated, be it a bug, fix or feature request. Don't hesitate to reach out via the issue tracker.

Please consider the following before making a PR:

  • Install stylua to ensure proper formatting.

🏁 Roadmap

  • Extend support for delimiter sorting, mirroring :sort options:
    • b option to sort by binary (2).
    • n option to sort by decimal (10).
    • o option to sort by octal (8).
    • x option to sort by hexidecimal (16).
  • Decent test coverage.
  • Natural Sorting.
  • Opt-in motion mappings.