174

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.

0

13 Answers 13

312

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

4
  • 6
    Your answer and the OP's answer are the only ones that work for me.
    – twblamer
    Sep 23, 2011 at 21:26
  • 47
    "<C-o> is used to issue normal commands without leaving INSERT mode" wins my can't-believe-I-never-knew-about-this award!
    – Stabledog
    Mar 20, 2014 at 13:30
  • 11
    <C-o> is shorthand for Ctrl + o. Apr 10, 2015 at 22:32
  • 3
    There's also the :normal command which lets you issue them from the command prompt. Occasionally quite useful.
    – Mark Reed
    Sep 3, 2015 at 19:31
195

Esc nic Esc .

E.g. Esc 4iJ Esc will output JJJJ.

10
  • 3
    Unfortunately that doesn't work on my vim unless you escape back to normal mode.
    – Cthutu
    Feb 20, 2011 at 0:56
  • 11
    But this is the best answer in the "vim way".
    – Drasill
    Feb 21, 2011 at 13:09
  • 2
    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?
    – Severo Raz
    Mar 3, 2013 at 16:50
  • 2
    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. Nov 20, 2015 at 23:18
  • 5
    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>
    – KFL
    Oct 24, 2018 at 17:02
45

Through single repeat:

  1. Press: i to enter into Insert mode
  2. Press: -
  3. Press: Esc
  4. Press: 80.

It will output: 81 -, like this:

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

More details about single repeat: :help .

6
  • Thanks but this solution requires you to go out of insert mode.
    – Cthutu
    Sep 17, 2018 at 20:45
  • 1
    This is trully vim language... This is why it is the best answer.
    – 71GA
    Mar 12, 2021 at 18:00
  • 1
    That's my winner. Also you can do . + ENTER repeatedly to add more reps
    – Hari Honor
    Jul 20, 2022 at 10:26
  • I like this, but you will end up with 81 characters in total. Oct 20, 2022 at 13:32
  • For me this was a bit confusing. I use VIM in vscode, and had to press 80, then 'i', '-' and esc last to get it to repeat like you say in this answer. Then I got 80 and not 81 chars as well. Nov 16, 2022 at 10:01
24

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>
0
24
<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.
1
  • 10
    This is the exact sequence of commands the OP wanted to avoid. Sep 3, 2015 at 19:26
13

You can also do, Escnihello there EscEsc

where, n is the number of repeats.

e.g., Esc5ihello there EscEsc

2
  • This looks like an exact duplicate of Martin Beckett's answer
    – mkobit
    Sep 18, 2016 at 17:11
  • It's not an exact duplicate. The other one didn't work for me. Here I realized I have to press Esc twice afterwards. Thanks @prashant Dec 16, 2017 at 20:30
11

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.

1
  • Not only can't I believe that no one suggested this so late, but also that your solution has so less up-votes. I was looking / hoping that something exactly like this(with Ctrl-r) would exist, and I almost closed this tab but I am so lucky that I found this. Awesome dude thank you :)
    – Pixelbog
    May 23, 2022 at 2:44
8

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>
1
  • 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.
    – Cthutu
    Feb 20, 2011 at 1:06
7

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
  • 1
    I had to use v y to enter visual mode, then yank, then 80 p to paste it 80 times. Am I missing something, or did you forget a step? Nov 10, 2016 at 16:22
  • Well spotted, I should have used 'x' to cut, then paste. Thank you I have amended my answer.
    – Inyoka
    Nov 11, 2016 at 6:56
  • 1
    nice since this is actually faster than the typical repeat method.
    – Auxiliary
    Sep 28, 2017 at 21:23
3

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

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.

2

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

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.