

Sort lines based on a column in Vim/Nvim
source link: https://jdhao.github.io/2021/12/21/sort_lines_based_on_a_column_nvim/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Sort lines based on a column in Vim/Nvim
I have a file where each line has several comma-separated items. I want to sort the lines, based on a certain column. How do we do it inside Vim/Nvim?
There are two different ways to achieve this.
Using external sort
The first is via external tool sort
and the vim filter
feature (see
:help filter
). The syntax is:
:{range}!{filter}
Lines in the given {range}
are sent to external program and are replaced with
the output of external command.
For example, to sort the following lines based on the 3rd column:
adf,adf,0.56
dkkdf,iwer,0.23
uywe,qwe,0.4
mckdf,adfj,0.35
we can use the following vim command:
:%!sort -t, -k3
" or use the following
" :1,$!sort -t, -k3
Using :sort
command
The second method is to use the vim builtin :sort
command (see :h :sort
for
the details).
:sort f /^.*,.*,/
Explanation of the command:
:sort
: you can prefix it with a range, like1,3sort
. Without a range, the whole buffer is used (i.e.,%sort
).f
: we are going to sort float numbers. We can also usex
(hex number),b
(binary number),o
(octal number) etc./^.*,.*,/
: the pattern we are going to skip during sort, i.e., content after the pattern will be sorted.
The default is to sort the lines in ascending order. To sort the lines in
descending order, add a !
after :sort
(i.e., use :sort!
).
References
Recommend
-
147
Nvim-R This is the development code of Nvim-R which improves Vim's support to edit R code. Installation If you use a plugin manager, such as vim-plug, foll...
-
448
Demo Screenshots • doc Install vim-plug
-
7
Lines in a multi-column list are not properly controlled when scrolling: Android advertisements I've populated the data into a multi column...
-
13
Sort column by string in sql server? advertisements LogID Title Message 1 Error Occured Could not find stored procedure 'RT_SE...
-
4
Sort-Based Blocking Shuffle Implementation in Flink - Part Two 26 Oct 2021 Yingjie Cao (Kevin) & Daisy Tsang Part one of this blog post explain...
-
4
I have an array stored in memory. How to sort it according to a column? advertisements I have a table stored in con...
-
7
The VIM lines and boxes plugin : vim online vim-linebox : The VIM lines and boxes plugin script karma Rating 0/0, Downloaded by 5 Comments, bugs,...
-
8
The VIM lines and boxes plugin : vim online vim-linebox : The VIM lines and boxes plugin script karma Rating 0/0, Downloaded by 6 Comments, bugs,...
-
10
How to Match Non-greedily in Nvim/Vim 2022-04-27 Nvim 119 words 1 min read 19 times read A short note on how to match
-
8
in: VimTipProposed
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK