How to sort using c # code in vim?

I recently edit C # code in vim. And the build system has StyleCop enabled, so everything using the operator should be in alphabetical order.

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

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

Result:

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

It does not pass the StyleCop check because "System.Security" is not ahead of "System.Security.Permissions." ASCII value ";" greater than the ASCII value of ".".

Preferred Result:

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

How to do it?

+5
source share
4 answers

:h :sort - your friend:

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

, u niq:

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

( , ";" )

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

( 1,4 - )

+1

CodeRush ReSharper -

<ducks for downvotes>

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

0
source

In my Linux box with a local non-C local (checked by fr_FR, fr_FR.UTF-8, en_US, en_GB), the sort command is sorted as you expect. You can very well execute the sort command:

:1,4!sort

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

0
source

All Articles