在Python代码中,我经常遇到像这样的导入语句:
from foo import ppp, zzz, abc
有没有类似于:sort
对于行的Vim技巧,可以将其排序成这样:
from foo import abc, ppp, zzz
没错,有这个东西:
%s/import\s*\zs.*/\=join(sort(split(submatch(0), '\s*,\s*')),', ')
关键要素包括:
回答评论,如果你想对视觉选择应用替换,则变成:
'<,'>s/\%V.*\%V\@!/\=join(sort(split(submatch(0), '\s*,\s*')), ', ')
新的关键元素这一次是:
顺便一提,我们还可以交互地使用s
和i_CTRL-R_=
,或将其放入映射(此处触发为µ
):
:xnoremap µ s<c-r>=join(sort(split(@", '\s*,\s*')), ', ')<cr><esc>
或者,您可以执行以下步骤:
Move the words you want to sort to the next line:
from foo import ppp, zzz, abc
Add a comma at the end of the words list:
from foo import ppp, zzz, abc,
Select the word list for example with Shift-v. Now hit : and then enter !xargs -n1 | sort | xargs
. It should look like this:
:'<,'>!xargs -n1 | sort | xargs
Hit Enter.
from foo import abc, ppp, zzz,
Now remove the trailing comma and merge the word list back to the original line (for example with Shift-j).
from foo import abc, ppp, zzz
以下是可能对您有用的Vim插件:
relationships = {
'project', 'freelancer', 'task', 'managers', 'team'
}
usage: paste [-s] [-d delimiters] file ...
。为了确保它在标准输入上正常工作,需要在末尾加上一个 -
,例如 !tr ',' '\n' | sort -f | paste -sd ','
。 - oschrenk