惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
爱范儿
爱范儿
小众软件
小众软件
MyScale Blog
MyScale Blog
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
V
V2EX
量子位
云风的 BLOG
云风的 BLOG
A
About on SuperTechFans
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
C
Check Point Blog
月光博客
月光博客

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Sort lines based on a column in Vim/Nvim
2021-12-21 · via jdhao's digital space

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:

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).

Explanation of the command:

  • :sort: you can prefix it with a range, like 1,3sort. Without a range, the whole buffer is used (i.e., %sort).
  • f: we are going to sort float numbers. We can also use x (hex number), b (binary number), o (octal number) etc.
  • /\v^(.{-},){2}/: 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#