I have several lines that I want to alphabetize. For example, say I have a bunch of vim set
commands in a file:
set nowrap set number set expandtab set hlsearch set list
How would I alphabetize these 5 lines? The output would look like this:
set expandtab set hlsearch set list set nowrap set number
Sorting text in Vim is easy! Select the text, then press : , type sort , then hit enter! It'll sort the whole document by default, but you can enter a range too.
Mappings to move lines In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up.
column happens to be the first column with a decimal number, note that you could use simply :sort n , as the help says: "With [n] ... sorting is done on the first decimal number in the line..." This doesn't apply to your case, but might to somebody elses.
The vim :sort
command takes in a command line range, and allows you to use a regex to select what is sorted. You can also use the external sort command the same way, using :{range}!sort
In my case, :1,5sort
does what I want. More help on the :sort
command is available in this vim help topic:
:help :sort
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With