简体   繁体   English

如何在Vim中对UTF-8行进行排序?

[英]How to sort UTF-8 lines in Vim?

I have these lines in Vim: 我在Vim中有这些行:

a
c
b
e
é
f
g

and when I do :%sort , I get this: 当我这样做时:%sort ,我得到这个:

a
b
c
e
f
g
é

Obviously, the "é" line should not be at the end, it should be after the "e" line. 显然,“é”行不应该在最后,它应该在“e”行之后。 Is it possible to get Vim to sort these lines correctly? 是否有可能让Vim正确排序这些行? Not using the ASCCI key for the characters but the actual character. 不使用ASCCI键作为字符,而是使用实际字符。

I also tried with :!sort (to use GNU sort utiliy) but I get the same result. 我也试过:!sort (使用GNU sort utiliy)但我得到了相同的结果。

:%sort and :%!sort do not necessarily work in the same way. :%sort:%!sort不一定以相同的方式工作。 To quote :help sort : 引用:help sort

The details about sorting depend on the library function used. 有关排序的详细信息取决于所使用的库函数。 There is no guarantee that sorting is "stable" or obeys the current locale. 无法保证排序“稳定”或服从当前区域设置。 You will have to try it out. 你必须尝试一下。

On the other hand, GNU sort sorts according to the current locale. 另一方面,GNU排序根据当前语言环境排序。 To quote man sort : 引用man sort

* WARNING * The locale specified by the environment affects sort order. *警告*环境指定的区域设置会影响排序顺序。 Set LC_ALL=C to get the traditional sort order that uses native byte values. 设置LC_ALL = C以获取使用本机字节值的传统排序顺序。

On my system (Ubuntu 9.10 with fr_CA.UTF-8 temporarily set) :%sort sorts as if C or POSIX was set, while :%!sort sorts according to the French locale. 在我的系统上(临时设置为fr_CA.UTF-8的Ubuntu 9.10) :%sort排序,就像设置了CPOSIX ,而:%!sort根据法语区域设置:%!sort排序。

My guess is that you've initially tried both :%sort and :%!sort under a POSIX-like locale (which yielded the same result), and then continued your experiments with different locales using :%sort only (which always returned POSIX-like order). 我的猜测是你最初尝试了两种:%sort:%!sort在类似POSIX的语言环境下:%!sort (产生相同的结果),然后继续使用不同的语言环境进行实验:%sort only(总是返回POSIX) - 顺序)。 Can you confirm that? 你能证实吗?

I get the following using :%!sort : 我使用以下方法获得以下内容:%!sort

a
b
c
e
é
f
g

which is what is supposed to happen. 这是应该发生的事情。 It seems like your locale set up might be to blame. 看起来你的语言环境设置可能是罪魁祸首。 I'm not sure what exactly is wrong, but FWIW, I have the following environment setting: LANG=en_GB.UTF-8 我不确定到底出了什么问题,但是FWIW,我有以下环境设置: LANG=en_GB.UTF-8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM