Repeating characters in VIM insert mode
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,
80the repetition,ito 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.
Related videos on Youtube
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, 2021Comments
-
Cthutu over 2 yearsIs 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 over 12 yearsUnfortunately that doesn't work on my vim unless you escape back to normal mode. -
Cthutu over 12 yearsIt 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 over 12 yearsBut this is the best answer in the "vim way". -
twblamer over 11 yearsYour answer and the OP's answer are the only ones that work for me. -
Severo Raz about 10 yearsI 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 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 about 8 yearsI 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 about 8 years<C-o>is shorthand forCtrl+o. -
Kenny Evitt over 7 yearsThis is the exact sequence of commands the OP wanted to avoid. -
Mark Reed over 7 yearsThere's also the:normalcommand which lets you issue them from the command prompt. Occasionally quite useful. -
Michael Leonard over 7 yearsThis 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 about 7 years@SeveroRaz did you managed how to solve that? I'm also into that. When I pressiI enter the insert mode. -
Severo Raz about 7 years@SolidSnake yeah, it doesn't matter that you go to insert mode again. When you press theiyou go into insert mode, then you type in the character you want and pressEscagain; then the string appears. -
mkobit over 6 yearsThis looks like an exact duplicate of Martin Beckett's answer -
KFL over 4 yearsFor 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 about 2 yearsThis is trullyvimlanguage... This is why it is the best answer.