1

I would like to map <Tab> in select mode to indent my selection.

So I have this:

$ vim -u /dev/null
:set nocompatible
:behave mswin
:xnoremap <Tab>   >gv
:xnoremap <S-Tab> <gv
:snoremap <Tab> <C-g>>gvV<C-g>
:snoremap <S-Tab> <C-g><gvV<C-g>

The idea is to switch to S-LINE or V-LINE when identing. The problem with the above mapping is the V will toggle between VISUAL -> V-LINE -> NORMAL.

How can I correct these mappings?

2

In visual mode, you only need one > to indent. >> is the normal mode command. Also, V turns a non-linewise selection into a linewise one, but if the selection already is linewise, visual mode will end. Better drop that. These changes result in:

snoremap <Tab> <Esc>gv>gv<C-g>

Actually, you can further simplify; there's no need to leave select mode and then restore the selection in visual mode via <Esc>gv; just use <C-g> to switch from select to visual mode:

snoremap <Tab> <C-g>>gv<C-g>
  • Thanks for your answer, You actually pointed out my issue the >>. I have completed my question and your answer is no more valid. I hope you don't mind. – nowox Sep 6 '16 at 9:58
  • Ohhhh :( Actually this is a lot more complex that I thought... Going on linewise mode is complicated because if you select one line with <S-Down>, when you go on linewise mode, you actually select two lines instead of one... – nowox Sep 6 '16 at 10:01
  • Why would you explicitly switch to linewise mode? Indenting via > is linewise, anyway, regardless of the selection type. – Ingo Karkat Sep 6 '16 at 11:01
  • On other visual editors (Sublime/Notepad++/VisualStudio/...) when you select a part of several lines and you indent it, the selection mutates to linewise selection. It makes sense because you cannot really indent a part of the selection, but the whole lines. This is just for visual comfort – nowox Sep 6 '16 at 11:46
  • 2
    Ah, I see. Well, there is a way, but you'd either have to write a function, or use a :map <expr>, and check the output of visualmode() if it is already linewise (i.e. returns V), and only if not force linewise with gvV. – Ingo Karkat Sep 6 '16 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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