Vim Tips Wiki
Edit Page
We recommend that you log in before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your Watchlist. Creating an account is quick and free.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 32: Line 32:
 
===Sort in reverse===
 
===Sort in reverse===
 
<pre>:%sort!</pre>
 
<pre>:%sort!</pre>
 
 
===Sort, removing duplicate lines===
 
===Sort, removing duplicate lines===
 
<pre>:%sort u</pre>
 
<pre>:%sort u</pre>
 
 
===Sort using the external Unix sort utility, respecting month-name order===
 
===Sort using the external Unix sort utility, respecting month-name order===
 
<pre>:%!sort -M</pre>
 
<pre>:%!sort -M</pre>
 
("respecting month-name order" means January < February < ... < December)
 
("respecting month-name order" means January < February < ... < December)
 
 
===Numeric sort===
 
===Numeric sort===
 
<pre>:sort n</pre>
 
<pre>:sort n</pre>
 
(this way, 100 doesn't precede 20 in the sort)
 
(this way, 100 doesn't precede 20 in the sort)
 
 
===Sort subsections independently, in this example sort numbers between "start" and "end" markers===
 
===Sort subsections independently, in this example sort numbers between "start" and "end" markers===
 
<pre>:g/start/+1,/end/-1 sort n</pre>It is very important that the strings "start" and "end" ONLY appear as markers!
 
<pre>:g/start/+1,/end/-1 sort n</pre>It is very important that the strings "start" and "end" ONLY appear as markers!
Line 89: Line 85:
 
 
 
You can also sort on text that *matches* the regex by including the 'r' flag, for example: <pre>:{range}sort /\/[A-z]/ r</pre>
 
You can also sort on text that *matches* the regex by including the 'r' flag, for example: <pre>:{range}sort /\/[A-z]/ r</pre>
  +
 
<pre>delimit the column using some char here I have | symbol as delimiter, once did with that you can use below command to sort specific column use -n if u want to sort numeric and its working on some version of vi and not on ubuntu vi :(
  +
 
/|.*|/ | sort
 
// used to match a patern |.*| used to match words delimited between || and | as piping commend and sort to sort </pre>
   
 
==See also==
 
==See also==
Line 115: Line 116:
   
 
----
 
----
This misguided snippet was added recently:
 
 
:delimit the column using some char here I have | symbol as delimiter, once did with that you can use below command to sort specific column use -n if u want to sort numeric and its working on some version of vi and not on ubuntu vi :(
 
 
<pre>/|.*|/ | sort</pre>
 
 
:used to match a patern |.*| used to match words delimited between || and | as piping commend and sort to sort
 
 
This is wrong and should never work. Here's what it is actually doing:
 
 
<code>/|.*|/</code>: jump to the next line that has two '|' characters in it, anywhere
 
 
<code>|</code>: command separator, this lets you start a new command on the current line
 
 
<code>sort</code>: do a default sort of the entire buffer
 
 
Basically this is the equivalent of typing <code>:%sort</code>.
 
 
Now, what you CAN do, is provide a pattern that the <code>:sort</code> command will skip over and ignore at the start of every line while sorting. For example, to sort based only on text after the last '|' character on the line (what I think was intended by the example), you'd do this:
 
 
<pre>
 
:sort /^.*|/
 
</pre>
 
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel Editing help (opens in new window)