5

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.

9
  • Firstly you do it (N-1) times. Then you do it one more time.
    – phils
    Commented May 17, 2020 at 3:51
  • 2
    Serious comment: I have no idea what your question is.
    – phils
    Commented May 17, 2020 at 3:52
  • My bad, let me try again: basically, you know how you can input/insert a character once in "put your language here" right? what i want to do here, is basically that, but more than once...as in, i can insert/input X character with a given number of time to input it. Commented May 17, 2020 at 3:58
  • 2
    Like (insert (make-string 10 ?X)) ? Or (dotimes (_ 10) (insert "X")) ?
    – phils
    Commented May 17, 2020 at 4:19
  • 1
    emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented May 17, 2020 at 7:05

2 Answers 2

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

4
  • help for dotimes notes its use is deprecated. Is there a more modern substitute?
    – Jeff Trull
    Commented Aug 22, 2021 at 20:11
  • 1
    It says that using the RESULT argument is deprecated, not that dotimes is deprecated.
    – phils
    Commented Aug 22, 2021 at 22:08
  • Oh, so it does! That's good I guess.
    – Jeff Trull
    Commented Aug 23, 2021 at 2: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
    Commented Aug 23, 2021 at 2:48
6

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.

0

Your Answer

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

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