1

What is the best way to indent multiple lines in insert mode?

Here is what I have currently:

vmap <Tab> >
vmap <S-Tab> <

The problem is that it doesn't properly handle selection area: the selection will shrink on each Tab press (Shift-Tab to unindent doesn't have any problems).

Then I tried:

vmap <Tab> ><C-O>gv

But it doesn't work for some reason. (Why?)

(To answer your questions: yes, I need to implement indenting in insert mode. I understand, it's against Vim way, but I'm trying to configure Vim for non-IT person, and therefore it will be used with -y flag, i.e. any modes except "insert" will not be available).

  • It's not entirely clear to me what you're asking. vmap is for visual mode not insert mode. And could you clarify what you mean by the selection will shrink on each Tab press? – DJMcMayhem Sep 10 '18 at 20:12
  • @DJMcMayhem Actually I'm asking about Easy Vim (i.e. Vim launched with -y flag). In this case Vim is locked in insert mode, but obviously it automatically switches from it when you select something. Don't know how I could show you the shrinking. It just shrinks! :) Select multiple lines, press Tab and you will see that the last line selected only partially. – john c. j. Sep 10 '18 at 20:27
  • Ahh, I see the behavior of visual mode is different with -y so I couldn't recreate it. Working on an answer now... – DJMcMayhem Sep 10 '18 at 21:20
2

Think of it like this.

aaaa
bbbb
cccc

If you select these 3 lines, then each line has 4 characters selected. Now, you indent them and use gv to reselect the whole thing. Here is the new text:

<tab>aaaa
<tab>bbbb
<tab>cccc

Which selection does vim see you used to have? 3 lines, each with 4 characters (including the tab). So vim selects that, and you lose the last "c" on the third line.

That should be what's causing this. Here's how to fix it.

Try this:

vmap <tab> >gvV

gv tells vim to reselect that last visual selection, but causes this "shrinking" behavior. Using V while in visual mode will switch you over to linewise visual mode, where entire lines are selected rather than individual characters.

Note that this will slightly change your selection if you have partial lines selected, but since indenting is really a linewise operation in concept, I don't think that's necessarily a bad thing. If you'd really like to keep the exact same characters selected and not go to linewise mode, this should work:

vmap <tab> >gvolloll
  • Thank you very much! Of course, I upvoted it yesterday, but haven't time to test, that's why I accepted it a bit late. The second snippet (gvolloll) doesn't work good for me (it shrinks selection from both first and last lines), but the first one (gvV) works just fine :) – john c. j. Sep 12 '18 at 0:56
2

You want to use:

vmap <tab> >gv

In this not-quite-Vim mode, a vmap'ed key briefly puts you in normal mode before returning you to insert mode. The <c-o> part of your map was performing the normal mode version of that command (jump list navigation).


I understand, it's against Vim way, but I'm trying to configure Vim for non-IT person, and therefore it will be used with -y flag, i.e. any modes except "insert" will not be available.

I totally sympathize with you if you're trying to make it possible for a non-technical person to edit text in a terminal. In this regard, all I can offer you is years of my own experience with this particular scenario: just tell them to use nano. If you want them to learn Vim, let them learn Vim. Don't hinder them from the very beginning by making it "not Vim". At least with nano, things are actually simple, not an illusion of simplicity.

Vim is a modal editor by design and it's very difficult to get it to act like a reliable modeless editor. The -y option seems like a nice compromise for people who aren't used to Vim, but I can't imagine it being a good way to teach the benefits of modal editing.


To clarify for anyone that's confused:

Vim's -y flag will cause Vim to always be in insert mode like a modeless editor, which is why "insert mode" is mentioned. Mouse selection or shift+arrows puts the buffer in visual mode. Pressing keys that aren't in a vmap in visual mode clears the selected text, replaces it with the keys that were pressed, and returns to insert mode.

  • Thanks, upvoted yesterday :) Yes, probably I should consider using Nano in such cases :) – john c. j. Sep 12 '18 at 1:00

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.