在排序VIM字(未行) [英] Sorting words (not lines) in VIM
问题描述
内置的VIM :排序
命令各种各样的文本行。我想在一个单一的一行字排序,例如变换行
The built-in VIM :sort
command sorts lines of text. I want to sort words in a single line, e.g. transform the line
b a d c e f
到
a b c d e f
目前我选择的线路,然后用 TR:!''的'\\ n'做到这一点|排序| TR的'\\ n'''
,但我敢肯定有一个更好,更简单,更快捷的方法。有没有?
Currently I accomplish this by selecting the line and then using :!tr ' ' '\n' | sort | tr '\n' ' '
, but I'm sure there's a better, simpler, quicker way. Is there?
请注意,我使用bash所以如果有这样做的它也是罚款更短,更优雅的bash命令。
Note that I use bash so if there's a shorter and more elegant bash command for doing this it's also fine.
编辑:我的用例是,我有这样一行 SOME_VARIABLE =一二三四等
和我想要的在该变量的话进行排序,即我想有 SOME_VARIABLE =等四一三二
。
My use-case is that I have a line that says SOME_VARIABLE="one two three four etc"
and I want the words in that variable to be sorted, i.e. I want to have SOME_VARIABLE="etc four one three two"
.
最终结果应该preferably可映射到一个快捷键,因为这是我找到自己需要经常
The end result should preferably be mappable to a shortcut key as this is something I find myself needing quite often.
推荐答案
从你的答案,尤其是铝的回答用伟大的想法,我终于想出了以下内容:
Using great ideas from your answers, especially Al's answer, I eventually came up with the following:
:vnoremap <F2> d:execute 'normal i' . join(sort(split(getreg('"'))), ' ')<CR>
这个映射在的<大骨节病> F2 骨节病>按钮视觉
模式下删除选定的文本,拆分,排序和加入它,然后重新插入。当选择跨多行,这将词语的所有输出一种排序和分类线,我可以快速修复使用 gqq
。
我会很高兴听到这如何可以进一步改善的建议。
I'll be glad to hear suggestions on how this can be further improved.
非常感谢,我学到了很多:)
Many thanks, I've learned a lot :)
修改:更改'&LT; C-R&gt;中
到 getreg('')
来处理与它的炭
文本。
EDIT: Changed '<C-R>"'
to getreg('"')
to handle text with the char '
in it.
这篇关于在排序VIM字(未行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!