Wikia

Vim Tips Wiki

Changes: Sort lines

Edit

Back to page

(Sort subsections independently, in this example sort numbers between "start" and "end" markers)
(example for delimiter sort was wrong, move to comments and explain)
 
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>
 
<pre>:g/start/+1,/end/-1 sort n</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 73: Line 72:
   
 
----
 
----
  +
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>

Latest revision as of 19:48, January 2, 2014

Tip 1166 Printable Monobook Previous Next

created 2006 · complexity basic · author Robert Stovall · version 7.0


Vim has a very powerful built-in sort utility, or it can interface with an external one. In order to keep only unique lines in Vim, you would:

:{range}sort u

Yes, it's that simple.

You could create a range in advance, such as 'a,. (from mark 'a' to the current line) or you could create one on-the-fly using visual selection by pressing ':' in visual mode, after selecting the text you wish to sort, to get a range of '<,'> on the command line.

If you like using an external sort utility instead, you can do it just as easily. For example, Unix sort, removing duplicate lines:

:{range}!sort -u

Many other systems also have an external sort utility, but the options and capabilities will differ. It is probably better to use the built-in Vim sort unless you are looking for a specific feature of the external sort (or using an old Vim without the :sort command).

ExamplesEdit

Sort in reverseEdit

:%sort!

Sort, removing duplicate linesEdit

:%sort u

Sort using the external Unix sort utility, respecting month-name orderEdit

:%!sort -M

("respecting month-name order" means January < February < ... < December)

Numeric sortEdit

:sort n

(this way, 100 doesn't precede 20 in the sort)

Sort subsections independently, in this example sort numbers between "start" and "end" markersEdit

:g/start/+1,/end/-1 sort n

See alsoEdit

ReferencesEdit

CommentsEdit

 TO DO 

  • Probably need some general :sort command info.
  • Give examples of numeric sort and using regex sort.
  • Clean up my "see also" list. It's useful now for a comprehensive list of related tips, some of which need work. At least should add a note on point of tip.
  • If we're going to mention an external sort tool, we may as well include the following with a brief explanation. Vim could do this, but only with a complex regex. Or perhaps better, mention it in VimTip374 or VimTip923 in "see also". -k2 sorts on the second field (word by default).
:!sort -k2

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 :(
/|.*|/ | sort
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:

/|.*|/: jump to the next line that has two '|' characters in it, anywhere

|: command separator, this lets you start a new command on the current line

sort: do a default sort of the entire buffer

Basically this is the equivalent of typing :%sort.

Now, what you CAN do, is provide a pattern that the :sort 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:

:sort /^.*|/

Around Wikia's network

Random Wiki