Files
thpeetz-notes/Quellen/IT/Sort lines.md
T

144 lines
9.2 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: Sort lines
source: https://vim.fandom.com/wiki/Sort_lines
---
## [Sort lines in Vim](https://vim.fandom.com/wiki/Sort_lines)
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).
## <a id="Examples"></a>Examples\[[edit](https://vim.fandom.com/wiki/Sort_lines?veaction=edit&section=1 "Edit section: Examples") | [edit source](https://vim.fandom.com/wiki/Sort_lines?section=1&veaction=editsource "Edit section: Examples")\]
### <a id="Sort_in_reverse"></a>Sort in reverse\[[edit](https://vim.fandom.com/wiki/Sort_lines?veaction=edit&section=2 "Edit section: Sort in reverse") | [edit source](https://vim.fandom.com/wiki/Sort_lines?section=2&veaction=editsource "Edit section: Sort in reverse")\]
:%sort!
### <a id="Sort.2C_removing_duplicate_lines"></a>Sort, removing duplicate lines\[[edit](https://vim.fandom.com/wiki/Sort_lines?veaction=edit&section=3 "Edit section: Sort, removing duplicate lines") | [edit source](https://vim.fandom.com/wiki/Sort_lines?section=3&veaction=editsource "Edit section: Sort, removing duplicate lines")\]
:%sort u
### <a id="Sort_using_the_external_Unix_sort_utility.2C_respecting_month-name_order"></a>Sort using the external Unix sort utility, respecting month-name order\[[edit](https://vim.fandom.com/wiki/Sort_lines?veaction=edit&section=4 "Edit section: Sort using the external Unix sort utility, respecting month-name order") | [edit source](https://vim.fandom.com/wiki/Sort_lines?section=4&veaction=editsource "Edit section: Sort using the external Unix sort utility, respecting month-name order")\]
:%!sort -M
("respecting month-name order" means January < February < ... < December)
### <a id="Numeric_sort"></a>Numeric sort\[[edit](https://vim.fandom.com/wiki/Sort_lines?veaction=edit&section=5 "Edit section: Numeric sort") | [edit source](https://vim.fandom.com/wiki/Sort_lines?section=5&veaction=editsource "Edit section: Numeric sort")\]
:sort n
(this way, 100 doesn't precede 20 in the sort)
### <a id="Sort_subsections_independently.2C_in_this_example_sort_numbers_between_.22start.22_and_.22end.22_markers"></a>Sort subsections independently, in this example sort numbers between "start" and "end" markers\[[edit](https://vim.fandom.com/wiki/Sort_lines?veaction=edit&section=6 "Edit section: Sort subsections independently, in this example sort numbers between "start" and "end" markers") | [edit source](https://vim.fandom.com/wiki/Sort_lines?section=6&veaction=editsource "Edit section: Sort subsections independently, in this example sort numbers between "start" and "end" markers")\]
:g/start/+1,/end/-1 sort n
### <a id="Sort_only_specific_lines_using_ranges"></a>Sort only specific lines using ranges\[[edit](https://vim.fandom.com/wiki/Sort_lines?veaction=edit&section=7 "Edit section: Sort only specific lines using ranges") | [edit source](https://vim.fandom.com/wiki/Sort_lines?section=7&veaction=editsource "Edit section: Sort only specific lines using ranges")\]
sort lines 296 to 349, inclusive
:296,349sort
### <a id="Sort_by_pattern"></a>Sort by pattern\[[edit](https://vim.fandom.com/wiki/Sort_lines?veaction=edit&section=8 "Edit section: Sort by pattern") | [edit source](https://vim.fandom.com/wiki/Sort_lines?section=8&veaction=editsource "Edit section: Sort by pattern")\]
When working with Javascript ES6, it may be useful to sort your imports
import './ProjectTemplateEditModal.scss';
import * as _ from "lodash";
import Moment from 'moment';
import React from 'react';
import { Button, Col, Modal, Row, Label } from 'react-bootstrap';
import { CurrencyControl } from '../../Core/Components/Controls';
import { DynamicModalMixin } from '../../Core/Components/Modals';
import { ProjectTemplateStore } from '../Stores/ProjectTemplateStore';
import { StoreBinder } from '../../Core/Utils/StoreBinder';
import { TextAreaControl } from '../../Core/Components/Controls/TextAreaControl';
import { TextBoxControl } from '../../Core/Components/Controls/TextBoxControl';
import { TooltipWrapper } from '../../Core/Components/Tooltips/TooltipWrapper';
import { withRouter } from 'react-router';
It is possible to pass a regex expression to sort. Any lines that do not match the expression will be sorted normally, while lines that do match will be sorted on the text that \*follows\* the expression.
:{range}sort /\\/\[A-z\]/
This will organize your imports relative to the "package" they are related to:
import * as _ from "lodash";
import Moment from 'moment';
import React from 'react';
import { Button, Col, Modal, Row, Label } from 'react-bootstrap';
import { withRouter } from 'react-router';
import { CurrencyControl } from '../../Core/Components/Controls';
import { TextAreaControl } from '../../Core/Components/Controls/TextAreaControl';
import { TextBoxControl } from '../../Core/Components/Controls/TextBoxControl';
import { DynamicModalMixin } from '../../Core/Components/Modals';
import { TooltipWrapper } from '../../Core/Components/Tooltips/TooltipWrapper';
import { StoreBinder } from '../../Core/Utils/StoreBinder';
import './ProjectTemplateEditModal.scss';
import { ProjectTemplateStore } from '../Stores/ProjectTemplateStore';
You can also sort on text that \*matches\* the regex by including the 'r' flag, for example:
:{range}sort /\\/\[A-z\]/ r
## <a id="See_also"></a>See also\[[edit](https://vim.fandom.com/wiki/Sort_lines?veaction=edit&section=9 "Edit section: See also") | [edit source](https://vim.fandom.com/wiki/Sort_lines?section=9&veaction=editsource "Edit section: See also")\]
- [374 Use filter commands to process text](https://vim.fandom.com/wiki/VimTip374 "VimTip374")
- [588 How to sort using visual blocks](https://vim.fandom.com/wiki/VimTip588 "VimTip588")
- [758 Search and sort by selection](https://vim.fandom.com/wiki/VimTip758 "VimTip758")
- [800 Sorting lines in a file based on the number of words in each line](https://vim.fandom.com/wiki/VimTip800 "VimTip800")
- [923 Sort lines by a specified word number](https://vim.fandom.com/wiki/VimTip923 "VimTip923")
- [667 Working with CSV files](https://vim.fandom.com/wiki/VimTip667 "VimTip667") sort by CSV field
- [128 Use Unix command-line tools in Windows](https://vim.fandom.com/wiki/VimTip128 "VimTip128") links to download GNU sort for Windows
- [648 Uniq - Removing duplicate lines](https://vim.fandom.com/wiki/VimTip648 "VimTip648") techniques to remove duplicate lines
## <a id="References"></a>References\[[edit](https://vim.fandom.com/wiki/Sort_lines?veaction=edit&section=10 "Edit section: References") | [edit source](https://vim.fandom.com/wiki/Sort_lines?section=10&veaction=editsource "Edit section: References")\]
- [:help :sort](http://vimdoc.sourceforge.net/cgi-bin/help?tag=%3Asort)
## \[[edit](https://vim.fandom.com/wiki/Sort_lines?veaction=edit&section=11 "Edit section: Comments") | [edit source](https://vim.fandom.com/wiki/Sort_lines?section=11&veaction=editsource "Edit section: Comments")\]
*** 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](https://vim.fandom.com/wiki/VimTip374 "VimTip374") or [VimTip923](https://vim.fandom.com/wiki/VimTip923 "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 /^.*|/