16

I often have to manually indent, but when I enter a visual mode, select my lines to indent, and press < or >, I leave visual mode whether I want to or not.

I would rather have it so I stay in visual mode, and keep my selection and cursor position intact.

How can I achieve this?

  • 1
    Is there any chance a remapping would do the trick ? Something like :vmap < <a ? – Feffe Jun 24 '16 at 14:04
19

I have this in my vimrc:

"keep visual mode after indent
vnoremap > >gv
vnoremap < <gv

Note that you could also simply use . (dot) to repeat the last indent action.

  • Almost perfect, save that my cursor does not move with the row. – Akiva Jun 24 '16 at 14:17
  • 1
    @Akiva Maybe you want to add a ^ at the end of that mapping? i.e. >gv^? – Martin Tournoij Jun 24 '16 at 14:21
  • 2
    The cursor seems to move (or not) depending on its position with respect to the indented text. It's not clear for me where do you want the cursor to stay: at the same column number, or on the same character? Also, you know that you can still move the cursor e.g. with j, k, l etc. while in visual mode? – VanLaser Jun 24 '16 at 14:32
  • 2
    Honestly, I just put <Right> And <Left> after the commands, and that worked fine. – Akiva Jun 24 '16 at 14:42
9

I know this isn't an automatic solution, but you can accomplish this by using gv after the visual indent.

This will go into visual mode with the previous selection selected, and your cursor at the last line. See :h gv for more info.

-2

I tried adding the following mapping to my vimrc :

vmap < <a
vmap > >a

It seems to have interesting results, maybe that's what you're looking for ?

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.