4

Just curious to know how to do this, since i already know in other languages (bash, etc).

I know how to insert a character in elisp, just not sure on how to do it when it's done nth time (in succession) in elisp.

Here an example:

Basically, inserting (in file or buffer) the character X, 10 times.

XXXXXXXXXX something like that.

NickD
  • 27,023
  • 3
  • 23
  • 42
Nordine Lotfi
  • 345
  • 2
  • 13

2 Answers2

8

There are many ways to write loops/iterative/repetitive behaviour in elisp.

C-hig (elisp)Iteration has the basic options, including dotimes, which is the canonical way to repeat something N times. E.g.:

(dotimes (_ 10) (insert "X"))

For the specific example of repeating a character N times, you might alternatively use make-string.

(insert (make-string 10 ?X))

For more sophisticated looping options, I suggest starting at C-hig (cl)Iteration

phils
  • 48,657
  • 3
  • 76
  • 115
  • help for `dotimes` notes _its use is deprecated_. Is there a more modern substitute? – Jeff Trull Aug 22 '21 at 20:11
  • 1
    It says that using the `RESULT` argument is deprecated, not that `dotimes` is deprecated. – phils Aug 22 '21 at 22:08
  • Oh, so it does! That's good I guess. – Jeff Trull Aug 23 '21 at 02:23
  • 1
    It looks like the only reason they've marked it deprecated is that the macro expansion for handling RESULT has to bind VAR, but VAR might not be used in RESULT, which then produces a byte-compilation warning under lexical binding about VAR not being used. I feel like this problem could be fixed. In any case, I don't imagine that functionality is *going away* any time soon, so I wouldn't be too concerned about using it it you need it. – phils Aug 23 '21 at 02:48
5

If you want to insert the character interactively, do C-u 10 X. This will give you XXXXXXXXXX.

This repeats the self-insert-command (here, for "X"), 10 times. See the manual node on repeating.

Dan
  • 32,584
  • 6
  • 98
  • 168