How to have a selection grow/shrink as we tabulate, regardless of cursor position ? The selection can span more than one line. Kind of like this:

enter image description here

For the tabbing I've got this part so far:

xnoremap <TAB> >gv
xnoremap <S-TAB> <gv

Just need the logic of the growing/shrinking selection. Anyone have some suggestions ?

2 Answers 2


This works if you're in Visual Line mode, so one option (the one I'd recommend) is to always convert to Visual mode (from character or block visual mode) if needed, before the operation.

With characterwise visual mode, the > command affects the line in a way that the original bounds of the selection are lost (in that they're no longer tied to the characters that were now shifted), so restoring the selection makes less sense in that case. (You could try to compensate for that by calculating the adjusted column after the shift, but that's way too complex of an approach.)

Converting to Visual Line mode first:

xnoremap <expr> <Tab> (mode() ==# 'V' ? '' : 'V') . '>gv'
xnoremap <expr> <S-Tab> (mode() ==# 'V' ? '' : 'V') . '<gv'

If you want the mappings to support both Visual and Select mode, then all you need is to use vnoremap instead. This works because when a vnoremap mapping is executed in Select mode, the selection is first converted to Visual mode and the mapping is executed in Visual mode. The Visual selection at the end is automatically converted back to Select mode at the end. So you do get the implicit conversion to Select Line mode when using a different kind of selection, but the mappings do work.

vnoremap <expr> <Tab> (mode() ==# 'V' ? '' : 'V') . '>gv'
vnoremap <expr> <S-Tab> (mode() ==# 'V' ? '' : 'V') . '<gv'
  • @Akharrou Using vnoremap here works for both Visual and Select mode. I updated the answer to cover that case too.
    – filbranden
    Nov 23, 2020 at 15:50

If it’s always a whole line, just replace gv with V

We tried

xnoremap <expr> <Tab> line("'<") isnot# line("'>") ? '>gvVgv' : '>V'

to handle multi-line selections and single-line selections, but there are some issues (see comments)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.