sort rows in 'VI' editor

喜夏-厌秋 提交于 2019-12-04 03:04:17
bjhaid

This answer is coming 2 years late, but might still be relevant, in visual mode select the block you want to sort and run:

:!sort

Try:

:%!sort

It will sort according the whole line alphabetically. If you want to sort numerically (i.e. the number in the first column can have different widt), then try:

:%!sort -n

Don't worry about the =, it will not modify any line, it will just change their order.

You can do the following to see the sorted output:

:!sort %

Explanation:

  • : : to enter ex mode.
  • ! : allows you to run a shell command.
  • % : the name of the file currently open.

To sort the file by changing it you can redirect its output to a temp file and then copy its content back to the original file:

:!(sort %>/tmp/tmp;cp -f /tmp/tmp %)

I'm not sure exactly when in the last eight years vi built this in, but you can now run:

:sort n

to sort numerical entries instead of using :! to run the sort command. See :help sort

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!