I commonly find myself inserting a tab before some text. This is easy in insert mode, I'll just type a tab, but I would also like to be able to do it in visual mode. The issue is, if I press > in visual mode, it will indent the whole line. Instead I only want to insert a tab at the start of the visual selection and then re-select that text. Here is what I have so far:

vnoremap <leader>] <Esc>`<i<tab><Esc>gv

What this tries to do is:

  • <Esc> Escape the visual selection
  • `< Go to the start of the previous visual selection and enter insert mode
  • <tab> Add a tab
  • <Esc> Escape insert mode
  • gv And again go into visual mode and reselect the visual selection

The issue is, it seems that gv is no longer valid after entering in the tab. So what would be the best way to do this?

  • You'd have to extend the selection to on the right by your :h tabstop setting, I suppose... but would not work if changing to another buffer. But it depends on what you exactly want: keep the exact same selection size? Select the new tab? Please explain more precisely what behaviour you'd expect and what's happening instead.
    – Biggybi
    Commented Jun 18, 2020 at 23:56
  • @Biggybi to select the previous selection -- that is, if I didn't add in the tab, whatever text was there before. For example, if This is <v>the text<v> and I add a tab at the start of the visual selection, it would look like this: This is <v>the text<v>
    – David542
    Commented Jun 19, 2020 at 0:16

1 Answer 1


The command to select as many characters as were selected before (assuming it's set selection=inclusive) is 1v. So it becomes:

xnoremap <leader>] c<tab><c-c>p`[1v

c --- delete selection into @- and start insert mode

<tab> --- insert literal tab

<c-c> --- stop insert mode and get back to normal

p --- put old selected text back

'[ --- move cursor back to the start of "put"

1v --- reselect the same number of characters

  • wow that is so cool and creative, thank you! A couple questions: is <c-c> and <esc> in this case identical, or why do you use <c-c> ? Could you please explain he 1v a bit more? I was testing it out a bit in vim but couldn't really understand what it does or where to find it in help? For me it was always selecting 6 characters, regardless of what I selected last in visual mode.
    – David542
    Commented Jun 19, 2020 at 5:49
  • @David542 (1) No real difference here; (2) Simply read :h v
    – Matt
    Commented Jun 19, 2020 at 5:53
  • 1
    @David542 personally i would have used escape
    – D. Ben Knoble
    Commented Jun 19, 2020 at 12:01
  • Thanks Matt for this useful tip. I often need to do the same operation on a block of lines. For example to insert a name or label at the start of each line. but I still don't know how to do it efficiently.
    – user25109
    Commented Aug 4, 2020 at 15:00
  • 1
    @elmclose :h blockwise-operators
    – Matt
    Commented Aug 4, 2020 at 15:48

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.