Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alphabetize lines of a given range in vim

Tags:

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 
like image 934
EvergreenTree Avatar asked Feb 06 '15 00:02

EvergreenTree


People also ask

How do I sort alphabetically in Vim?

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.

How do I rearrange lines in Vim?

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.

How do I sort a specific column in vim?

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.


1 Answers

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

like image 194
EvergreenTree Avatar answered Oct 04 '22 17:10

EvergreenTree