Vim Tips Wiki
Advertisement
Tip 588 Printable Monobook Previous Next

created October 16, 2003 · complexity basic · author Charles E. Campbell · version 6.0


To sort lines based on a visually-selected column: http://mysite.verizon.net/astronaut/vim/index.html#VISSORT

Look under "Visual Block Sorting". It uses Piet Delport's vim-based binary insertion sort and some vim-glue to provide visual-block sorts!

To enable it, put <bisort.vim> into your <.vim/plugin> directory.

To perform sorting with Vissort, based on a visual-block selection (ctrl-v):

:'<,'>Vissort

If you're using vim 7.0 or later, vissort.vim will use vim's internal sort command rather than the binary insertion sort script.

To sort a visually-selected block (and leave the text outside the block in place): http://mysite.verizon.net/astronaut/vim/index.html#VimFuncs

Look under "Visual Block Commands". The <vis.vim> plugin provides a command which allows other commands to be applied only to the visually selected block.

To then perform sorting of just a visual-block (ctrl-v):

:'<,'>B !sort

Or, using Piet Delport's binary insertion sort:

:'<,'>B Bisort

Of course, you can use vim's internal sort if you're using vim 7.0 or later, too:

:'<,'>B sort

Examples: Original, visual-block select the central column

one two three
four five six
seven eight nine
ten eleven twelve
:'<,'>Vissort
seven eight nine
ten eleven twelve
four five six
one two three
:'<,'>B !sort
one eight three
four eleven six
seven five nine
ten two twelve
:'<,'>B Bisort
one eight three
four eleven six
seven five nine
ten two twelve

Comments

vissort has been updated to version 3: it now has BlockSort() which permits one to sort blocks of text based on tags contained within them.


For Windows users only: Visual blocking is done with Ctrl-q. Do not use the arrow key to make the block, use only h-j-k-l.


the simple sample in the tip can be solved by native :sort command, no plugin required.

:sort /\s\+/

and for the visual blocking condition, you need not any plugin neither. e.g. if your Visuall Block start at visual colume 7, without selecting it, just execute

:sort /\%7v/

I have few plugins in my computer, for vim is good enough!

Advertisement