Repeating characters in VIM insert mode

81,355

Solution 1

Slightly different version of Eelvex's solution:

function! Repeat()
    let times = input("Count: ")
    let char  = input("Char: ")
    exe ":normal a" . repeat(char, times)
endfunction
imap <C-u> <C-o>:call Repeat()<cr>

Solution 2

If you are OK with leaving INSERT mode only once (at the end), this sequence works:

Ctrl+o 80i- Esc

  • Ctrl+o is used to issue normal commands without leaving INSERT mode,
  • 80 the repetition,
  • i to insert,
  • - the character you want to insert,
  • Esc to leave INSERT mode.

Another one without EVER leaving INSERT mode:

Ctrl+o :norm 8ia Return

Solution 3

Escnic Esc Esc.

E.g. Esc4iJEsc Esc will output JJJJ.

Solution 4

Through single repeat:

Insert mode
-
Esc
80.

Will output: 80 -

---------------------------------------------------------------------------------

More details about single repeat: :help .

Solution 5

<ESC> 
<the number of times you want to repeat>
i 
<the char you want to repeat> 
<ESC>
for example: <ESC>12ia<ESC> will insert 12 a's.
Share:
81,355

Related videos on Youtube

Cthutu
Author by

Cthutu

I've worked in the video game industry for nearly 20 years and bring experience from many platforms and programming languages.

Updated on January 16, 2021

Comments

  • Cthutu
    Cthutu over 2 years

    Is there a way of repeating a character while in Vim's insert mode? For example, say I would like to insert 80 dashes, in something like emacs I would type:

    Ctrl+U   8 0 -

    The only way I know how to do it in VIM is to exit normal mode for the repeat argument, then go back into insert mode to type the dash, then exit to insert the actual dashes, AND then go back into insert mode to carry on typing. The sequence is a really long:

    Esc 8 0 a - Esc a

    It would be nice not to switch in and out of modes.

  • Cthutu
    Cthutu over 12 years
    Unfortunately that doesn't work on my vim unless you escape back to normal mode.
  • Cthutu
    Cthutu over 12 years
    It didn't work for me but thanks for the answer. My knowledge of the vim programming language is not good enough to figure out why.
  • Drasill
    Drasill over 12 years
    But this is the best answer in the "vim way".
  • twblamer
    twblamer over 11 years
    Your answer and the OP's answer are the only ones that work for me.
  • Severo Raz
    Severo Raz about 10 years
    I always find myself going back to insert mode when I press the i, so then I will only insert CHARACTER. How do I work this?
  • Stabledog
    Stabledog about 9 years
    "<C-o> is used to issue normal commands without leaving INSERT mode" wins my can't-believe-I-never-knew-about-this award!
  • Kelvin
    Kelvin about 8 years
    I like this answer but on MacVim, I get an annoying beep when pressing the first Esc, as though warning me I did something invalid. Oh well I can get used to it.
  • Shaun Luttin
    Shaun Luttin about 8 years
    <C-o> is shorthand for Ctrl + o.
  • Kenny Evitt
    Kenny Evitt over 7 years
    This is the exact sequence of commands the OP wanted to avoid.
  • Mark Reed
    Mark Reed over 7 years
    There's also the :normal command which lets you issue them from the command prompt. Occasionally quite useful.
  • Michael Leonard
    Michael Leonard over 7 years
    This may not be the answer to the question that was asked, but its the vim way to do it and it's the answer to the question I was looking for.
  • rdbisme
    rdbisme about 7 years
    @SeveroRaz did you managed how to solve that? I'm also into that. When I press i I enter the insert mode.
  • Severo Raz
    Severo Raz about 7 years
    @SolidSnake yeah, it doesn't matter that you go to insert mode again. When you press the i you go into insert mode, then you type in the character you want and press Esc again; then the string appears.
  • mkobit
    mkobit over 6 years
    This looks like an exact duplicate of Martin Beckett's answer
  • KFL
    KFL over 4 years
    For me only one ESC at the end is enough. I'm inserting 80 - to form a split line. So I do: <ESC>80i-<ESC>
  • 71GA
    71GA about 2 years
    This is trully vim language... This is why it is the best answer.

Related