How do I sort lines alphabetically in Vim?
Answer
:sort
Explanation
The :sort command sorts lines in the current buffer or a specified range alphabetically. It is a built-in alternative to the external sort command.
How it works
:sortsorts all lines in the buffer:'<,'>sortsorts only the selected lines:sort!sorts in reverse order:sort usorts and removes duplicate lines
Example
charlie
alpha
bravo
After :sort:
alpha
bravo
charlie
Tips
:sort nsorts numerically instead of alphabetically:sort isorts case-insensitively:sort /pattern/sorts by the text after the pattern match:sort uremoves duplicate lines while sorting:%sortis equivalent to:sort(whole file)