Keeping lists alphabetized is important for organized, maintainable code. Fortunately, Vim has a number of ways to sort lines.

First and foremost, Vim has a built-in sort function. In this example, some sloppy, inattentive chap has left the Gemfile in utter shambles. We can clean it up easily.

source 'https://rubygems.org'
ruby '2.3.3'

gem 'rails'
gem 'rspec'
gem 'capybara'
gem 'fog'
gem 'poltergeist'
gem 'haml-rails'
gem 'pismo'

With our cursor on the first gem line, V will put us in visual select mode and G will highlight to the end of the file. Then :sort will use Vim’s built-in sort capability and we’ll end up with a vastly more tolerable:

source 'https://rubygems.org'
ruby '2.3.3'

gem 'capybara'
gem 'fog'
gem 'haml-rails'
gem 'pismo'
gem 'poltergeist'
gem 'rails'
gem 'rspec'

If we wanted to sort in reverse, then :sort! will do the trick. But don’t get that confused with :!sort. The ! command on a block of text executes the proceeding command in the shell, passing the highlighted text to stdin and returning the output into Vim. That means :!sort, assuming a Unix-like operating system, will do the same as Vim’s built-in :sort: alphabetize the highlighted text from A-Z.

There are lots of variations we can do on sorts either with the Unix sort utility or Vim’s:

Sort and remove duplicate lines, the Vim way and the Unix way:

:sort u
:!sort | uniq

Sort and respect month order by month name:

:!sort -M

Sort numerically:

:sort n

For more options and ideas, check out the docs for Vim’s sort with :help sort or man sort for the Unix utility.