23

I'm learning the basics of navigating/editing in Emacs and I'm curious how one could accomplish the following task:

  • Repeat the string 'bla ' n times in normal text editing mode.

Let's say I want to repeat it five times to generate 'bla bla bla bla bla '. I tried...

C-u 5 bla

...but the command executes after the 'b' is entered, and I only get 'bbbbb'.

I'm sure there's some basic command that can help me here...would somebody be kind enough to enlighten me :)?

sammy34
  • 5,312
  • 5
  • 29
  • 42

2 Answers2

25

One way is via a keyboard macro:

C-x (bla C-x)C-u4C-xe

You can also just insert the repeat count before the macro termination:

C-x (bla C-u5C-x)

Keith Flower
  • 4,032
  • 25
  • 16
  • 3
    Keyboard macros...awesome! Something tells me I'm going to love Emacs :). – sammy34 Jul 27 '13 at 06:34
  • 6
    Just for the record, the official way to use macros is `F3` to start recording and `F4` to finish and `F4` to execute. The `C-x` version is deprecated and could change. – Malabarba Jul 27 '13 at 08:57
  • 1
    @Malabarba That's news to me. I can more easily touch type `C-x (`, etc. Some flame war history here: http://xahlee.info/UnixResource_dir/writ/emacs_keybinding.html – remcycles May 04 '18 at 18:51
16

You can do it with keyboard macros: f3blaM-5 f4.

  1. f3 means "start recording"
  2. then you insert bla
  3. M-5 means "5 times"
  4. f4 means finish

Alternate to M-5 f4 is just f4 a bunch of times.

abo-abo
  • 20,038
  • 3
  • 50
  • 71