Sort is a sorting plugin for Neovim that provides a simple command to mimic :sort
and supports both line-wise and delimiter sorting. This plugin intelligently selects a sorting strategy by using a configurable priority list, minimizing manual input and covering most sorting cases with just the :Sort
command on a range.
:sort
command where possible.-- 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 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
Sort comes with the following defaults:
{
-- List of delimiters, in descending order of priority, to automatically
-- sort on.
delimiters = {
',',
'|',
';',
':',
's', -- Space
't' -- Tab
}
}
https://user-images.githubusercontent.com/2284724/145567686-3b52978c-58fe-4f32-ad27-c2b1060870ba.mp4
Sorting with the Sort plugin is made easy through the provided :Sort
command. The plugin utilizes two different strategies depending on the visual selection:
Multiple lines
When selecting multiple lines, all arguments provided to :Sort
are fed to the built-in :sort
command, thereby mirroring all of the features provided by the built-in sort. See :help :sort
for usage and options.
Single line
:[range]Sort[!] [delimiter][b][i][n][o][u][x]
[!]
to reverse the sort order.[delimiter]
to manually set the delimiter instead of iterating over config.delimiters
and sorting by the highest priority delimiter. Valid delimiters include:%p
lua pattern character class.s
: Spacet
: Tab[b]
to sort based on the first binary number in the word.[i]
to ignore the case when sorting.[n]
to sort based on the first decimal number in the word.[o]
to sort based on the first octal number in the word.[u]
to only keep the first instance of words within the selection. Leading and trailing whitespace are not considered when testing for uniqueness.[x]
to sort based on the first hexadecimal number in the word. A leading 0x
or 0X
is ignored.By default, Sort does not set any keybindings. Here's an example of how you can set a keybinding to trigger :Sort
. In this example, the keybinding go
is used, but you can change it to whatever you prefer:
" 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>
All contributions to Sort are greatly appreciated, whether it's a bug fix or a feature request. If you would like to contribute, please don't hesitate to reach out via the issue tracker.
Before making a pull request, please consider the following:
:sort
: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).