Natural Sorting in Neovim
To achieve natural numeric sorting (1
, 2
, 3
, 10
) in Neovim, you can use a custom sorting command rather than the default :sort u
, as it performs a lexicographical sort by default.
Select the lines you want to sort in visual mode (v
or V
):
:'<,'>!sort -n
!sort -n
pipes the selected lines to the Unixsort
command with numeric sorting (-n
flag).
Method 2: Add a Key Mapping
If you often need this, you can add a custom key mapping to your Neovim configuration file (~/.config/nvim/init.lua
or lua/custom/mappings.lua
for NVChad users).
Add this snippet:
vim.keymap.set('v', '<leader>sn', ":'<,'>!sort -n<CR>", { noremap = true, silent = true })
- Usage: In visual mode, press
<leader>sn
to sort selected lines numerically.
Published on 08 Jan 2025
all tags
100daysofcode activerecord android annoyances api apt arch array artix atom az3w backend bash blog browser bug callback career ci-cd cli cloud code coding config configuration cp crud cryptography css csv database db design devops django docker email erp feelsgood filter fugitive gif gist git gnome gnome pomodoro grep hebrew http ide isbn-fetcher iso javascript job search js kanban kindle koans learning linkedin linux logger manjaro map markdown microservices mobi mtp neovim nodejs nvchad packages panda pastbin patch portfolio post postgres pytest python rails reduce refactoring reflections rest routes rspec ruby salesforce script scripting security sed shell sql string_replacement study tdd terminal testing tmux ttd version_control vim vim sort walkthrough webdev workflow