I am trying to write a :sort
wrapper that sorts from current column.
command! -bang -range=% -nargs=* Sortc exe '<line1>,<line2>sort<bang> <args> /\%' .. virtcol('.') .. 'v/'
E.g.
hello world
foo bar
apple google
When the cursor is on [w]orld
, :Sortc
will sort into
foo bar
apple google
hello world
It works in all cases except when in visual mode. E.g. From original text, I select all lines in virtual line mode and type :'<,'>Sortc
, it ignores the column and sorts from beginning. i.e. virtcol('.')
, returns 0.
Is there anyway to fix this such that it works in visual mode too?
Workaround:
After it incorrectly sorts, run the same command again placing in the correct column, without the visual mode (i.e. @:
), then it works.
Using visual block selection works.
:command! -range=% -nargs=1 Sortc :<line1>,<line2> !sort -k<q-args>,<q-args>