|
|
| Line 19: |
Line 19: |
| |
Yes, it's that simple. |
|
Yes, it's that simple. |
| |
|
|
|
| − |
You could create a range in advance, such as <tt>'a,.</tt> (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 <tt>'<,'></tt> on the command line. |
+ |
You could create a range in advance, such as <code>'a,.</code> (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 <code>'<,'></code> 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: |
|
If you like using an external sort utility instead, you can do it just as easily. For example, Unix sort, removing duplicate lines: |
| Line 27: |
Line 27: |
| |
</pre> |
|
</pre> |
| |
|
|
|
| − |
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 <tt>:sort</tt> command). |
+ |
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 <code>:sort</code> command). |
| |
|
|
|
| |
==Examples== |
|
==Examples== |
| − |
=== 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> |
| |
|
|
|
| Line 59: |
Line 59: |
| |
|
|
|
| |
{{Todo}} |
|
{{Todo}} |
| − |
*Probably need some general <tt>:sort</tt> command info. |
+ |
*Probably need some general <code>:sort</code> command info. |
| |
*Give examples of numeric sort and using regex sort. |
|
*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. |
|
*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". <tt>-k2</tt> sorts on the second field (word by default). |
+ |
*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". <code>-k2</code> sorts on the second field (word by default). |
| |
<pre> |
|
<pre> |
| |
:!sort -k2 |
|
:!sort -k2 |
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).
Examples
Sort in reverse
:%sort!
Sort, removing duplicate lines
:%sort u
Sort using the external Unix sort utility, respecting month-name order
:%!sort -M
("respecting month-name order" means January < February < ... < December)
Numeric sort
:sort n
(this way, 100 doesn't precede 20 in the sort)
Sort subsections independently, in this example sort numbers between "start" and "end" markers
:g/start/+1,/end/-1 sort n
See also
References
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