Vim Tips Wiki
m (Use script template)
(Added a human readable description to a long series of keystrokes)
Tags: Visual edit apiedit
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{TipImported
 
{{TipImported
 
|id=588
 
|id=588
|previous=587
+
|previous=586
 
|next=589
 
|next=589
|created=October 16, 2003
+
|created=2003
 
|complexity=basic
 
|complexity=basic
 
|author=Charles E. Campbell
 
|author=Charles E. Campbell
 
|version=6.0
 
|version=6.0
 
|rating=212/66
 
|rating=212/66
  +
|category1=
  +
|category2=
 
}}
 
}}
To sort lines based on a visually-selected column:
+
To sort lines based on a visually-selected column, use [http://www.drchip.org/astronaut/vim/index.html#VISSORT Visual Block Sorting]. It uses Piet Delport's Vim-based binary insertion sort and some Vim glue to provide visual-block sorts!
http://mysite.verizon.net/astronaut/vim/index.html#VISSORT
 
   
 
To enable it, put vissort.vim into your .vim/plugin directory.
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 perform sorting with Vissort, based on a visual-block selection (Ctrl-v):
To enable it, put <bisort.vim> into your <.vim/plugin> directory.
 
 
:'<,'>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 perform sorting with Vissort, based on a visual-block selection (ctrl-v):
 
:'&lt;,'&gt;Vissort
 
   
  +
To sort a visually-selected block (without changing text outside the block), use [http://www.drchip.org/astronaut/vim/index.html#VIS Visual Block Commands].
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.
 
   
 
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 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 &lt;vis.vim&gt; plugin provides a command which allows other commands to be applied only to the visually selected block.
 
   
 
vis.vim can also be downloaded at {{script|id=1195}}.
 
vis.vim can also be downloaded at {{script|id=1195}}.
   
To then perform sorting of just a visual-block (ctrl-v):
+
To then perform sorting of just a visual-block (Ctrl-v):
:'&lt;,'&gt;B !sort
+
:'<,'>B !sort
   
 
Or, using Piet Delport's binary insertion sort:
 
Or, using Piet Delport's binary insertion sort:
:'&lt;,'&gt;B Bisort
+
:'<,'>B Bisort
   
Of course, you can use vim's internal sort if you're using vim 7.0 or later, too:
+
Of course, you can use Vim's internal sort if you're using Vim 7.0 or later, too:
:'&lt;,'&gt;B sort
+
:'<,'>B sort
   
 
Examples: Original, visual-block select the central column
 
Examples: Original, visual-block select the central column
 
 
<pre>
 
<pre>
 
one two three
 
one two three
Line 47: Line 44:
   
 
<pre>
 
<pre>
:'&lt;,'&gt;Vissort
+
:'<,'>Vissort
 
seven eight nine
 
seven eight nine
 
ten eleven twelve
 
ten eleven twelve
Line 55: Line 52:
   
 
<pre>
 
<pre>
:'&lt;,'&gt;B !sort
+
:'<,'>B !sort
 
one eight three
 
one eight three
 
four eleven six
 
four eleven six
Line 63: Line 60:
   
 
<pre>
 
<pre>
:'&lt;,'&gt;B Bisort
+
:'<,'>B Bisort
 
one eight three
 
one eight three
 
four eleven six
 
four eleven six
Line 77: Line 74:
   
 
----
 
----
 
The simple sample in the tip can be solved by native :sort command, no plugin required.
 
the simple sample in the tip can be solved by native :sort command, no plugin required.
 
 
:sort /\s\+/
 
: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
+
and for the visual blocking condition, you need no plugin. If your Visuall Block start at visual column 7, without selecting it, just execute
 
:sort /\%7v/
 
:sort /\%7v/
I have few plugins in my computer, for vim is good enough!
+
I have few plugins in my computer, for Vim is good enough!
   
 
Generally, visual block sorting can be done as following.
 
Generally, visual block sorting can be done as following.
Line 89: Line 85:
 
:'<,'>sort /\ze\%V/
 
:'<,'>sort /\ze\%V/
   
If you want to keep the text outside of visual block unchanged, just type
+
If you want to keep the text outside of visual block unchanged, you need select or go Go to the last Visual selection and Yank it to a register. Then Undo the the sort made. Then just paste the unsorted data over the sorted data. Or just type:
 
gvyugvp
 
gvyugvp

Latest revision as of 18:52, 23 December 2016

Tip 588 Printable Monobook Previous Next

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


To sort lines based on a visually-selected column, use 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 vissort.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 (without changing text outside the block), use Visual Block Commands.

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.

vis.vim can also be downloaded at script#1195.

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 no plugin. If your Visuall Block start at visual column 7, without selecting it, just execute

:sort /\%7v/

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

Generally, visual block sorting can be done as following.

After selecting visual block, execute following command to sort on the selected text in the selected range:

:'<,'>sort /\ze\%V/

If you want to keep the text outside of visual block unchanged, you need select or go Go to the last Visual selection and Yank it to a register. Then Undo the the sort made. Then just paste the unsorted data over the sorted data. Or just type:

gvyugvp