Repeating characters in VIM insert mode

Vim

Vim Problem Overview


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.

Vim Solutions


Solution 1 - Vim

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 2 - Vim

Escnic Esc Esc.

E.g. Esc4iJEsc Esc will output JJJJ.

Solution 3 - Vim

Through single repeat:

1. Press: (i) # Enter into Insert mode
2. Press: (-)
3. Press: (Esc)
4. Press: (80.)

It will output: 80 -, like this:

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

More details about single repeat: :help .

Solution 4 - Vim

<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.

Solution 5 - Vim

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 6 - Vim

You can also do, Escnihello there EscEsc

where, n is the number of repeats.

e.g., Esc5ihello there EscEsc

Solution 7 - Vim

There are many other ways but AFAIK the one you describe is the shortest one. In vim you are mostly supposed to spend your time in command mode, so that would be just 3 keystrokes + the number of repeats (80i-).

However, if you find that you very often use this repeat thing, you can make yourself a function or macro to that end; maybe something like:

:function Repeat(char)
: let counter = input("How many times?: ")
: call feedkeys("i")
: call feedkeys(repeat(a:char,counter))
:endfunction
:imap <C-U> <ESC>h"ryl :call Repeat(@r)<CR>

Solution 8 - Vim

I'm surprised no one has suggested this yet:

In Insert mode, use <C-r>=repeat('-', 80)<CR>

That is:

  • Press Ctrl-r=
  • At the resulting prompt, enter repeat('-', 80)
  • Press Enter

Works for repeating any character any number of times.

This uses more keystrokes than @romainl's answer, but does not leave Insert mode at all.

Solution 9 - Vim

You said it would be 'nice' to stay in 'Insert' mode, however in Command Mode the following method would avoid your 2nd ESC :-

While I know this post is old, it seems a shame to miss the obvious 'Cut/Copy and Paste' option...

x ...cut

80 ...number of copies

p Paste

Note : This is similar to the method suggested by Martin Beckett, however I get a delay when issuing that command, perhaps because it switches modes several times, this command executes instantly.

Solution 10 - Vim

Late answer but for what it's worth, if you wanna hand spam it, you can use the "repeat last command" command: .

i "Phrase" Esc - i to insert, enter phrase/character, esc to go normal mode

. - Spam till you are satisfied. Will repeatedly input the phrase you typed (it repeats your last command).

I find this especially useful when I don't know exactly how many repeats I want to do, but know visually how long I want it to be. Basically blast the . till my eyes are content.

Solution 11 - Vim

In addition to writing function that will repeat text multiple times, you could use <C-x><C-l>: if you already have line that contains 80 dashes, writing a few dashes at the start of new line and then pressing <C-x><C-l> will complete lines which start with these few dashes which will be likely that line with 80 dashes. I used to write horizontal lines (78 dashes) in help files in a such way.

Solution 12 - Vim

For such an easy task abbreviation should do the trick. Add the following to your .vimrc

iab <expr> -- repeat('-', 80)

and from now, when you type -- followed by a space (while you are in insert mode), the -- will be automatically converted to - 80 times.

By using the function repeat you are able to repeat the string as many time you want.

Note that you can test it before updating the .vimrc by entering in command mode then issuing the following :iab <expr> -- repeat('-', 80)

Solution 13 - Vim

I did this without exiting the INSERT mode using the below steps.

  1. Enable INSERT mode.
  2. Type one dash "-".
  3. Ctrl + O
  4. lowercase 'v' (to enter -- (insert) VISUAL -- mode)
  5. lowercase 'y' (to copy)
  6. Ctrl + O
  7. Type 80
  8. Then, followed by lowercase 'p' (for paste).

i - Ctrl+o v y Ctrl+o 80 p

This will print all the dashes horizontally in a single line.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCthutuView Question on Stackoverflow
Solution 1 - VimromainlView Answer on Stackoverflow
Solution 2 - VimMartin BeckettView Answer on Stackoverflow
Solution 3 - VimitsnikolayView Answer on Stackoverflow
Solution 4 - VimqedView Answer on Stackoverflow
Solution 5 - VimMatteo RivaView Answer on Stackoverflow
Solution 6 - VimprashantView Answer on Stackoverflow
Solution 7 - VimEelvexView Answer on Stackoverflow
Solution 8 - VimgpandersView Answer on Stackoverflow
Solution 9 - VimInyokaView Answer on Stackoverflow
Solution 10 - VimgowrathView Answer on Stackoverflow
Solution 11 - VimZyXView Answer on Stackoverflow
Solution 12 - VimSamir SadekView Answer on Stackoverflow
Solution 13 - VimhelView Answer on Stackoverflow