153
votes

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.

13

13 Answers

286
votes

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

174
votes

Escnic Esc Esc.

E.g. Esc4iJEsc Esc will output JJJJ.

33
votes

Through single repeat:

Insert mode
-
Esc
80.

Will output: 80 -

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

More details about single repeat: :help .

23
votes
<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.
22
votes

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>
9
votes

You can also do, Escnihello there EscEsc

where, n is the number of repeats.

e.g., Esc5ihello there EscEsc

7
votes

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

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.

6
votes

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.

3
votes

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.

2
votes

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.

1
votes

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)

1
votes

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.