开发者

How to sort using statement of C# code in vim?

开发者 https://www.devze.com 2022-12-13 11:10 出处:网络
Recently I edit C# code in vim. And the build system has StyleCop enabled so that all using statement should be in alphabetical order.

Recently I edit C# code in vim. And the build system has StyleCop enabled so that all using statement should be in alphabetical order.

So, I tried to select below lines of code in visual mode, then type ":sort".

using System.Security.Permissions;
using System.Runtime.Serialization;
using System.Security;
using System.ServiceModel;

The result is:

using System.Runtime.Serialization;
using System.Security.Permission开发者_高级运维s;
using System.Security;
using System.ServiceModel;

It doesn't pass StyleCop checking because "System.Security" is not ahead of "System.Security.Permissions". The ASCII value of ";" is larger than ASCII value of ".".

The preferred result is:

using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
using System.ServiceModel;

How to achieve it?


:h :sort is your friend:

:[range]sort r /[^;]*/

If along the way you wish to remove duplicates, add the uniq flag:

:[range]sort ur /[^;]*/

(This won't do any good if you have different comments after the ';' though)


:1,4s/;$//
:sort
:1,4s/$/;/

(where 1,4 are lines with using statements)


Not using CodeRush or ReSharper is stealing from your employer

<ducks for downvotes>

(Yes, I know that requires VS (and AFAIK VS10 has this OOTB))


On my linux box with a local other than C (tested fr_FR, fr_FR.UTF-8, en_US, en_GB), the sort command sorts as you expect. You could very well pipe to the sort command :

:1,4!sort

If you are on windows, I suppose you can install unix tools (like SFU) that could do the job since vim's sort command doesn't seem to handle locale.

0

精彩评论

暂无评论...
验证码 换一张
取 消