0

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.

  • Firstly you do it (N-1) times. Then you do it one more time. – phils May 17 at 3:51
  • 2
    Serious comment: I have no idea what your question is. – phils May 17 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. – Nordine Lotfi May 17 at 3:58
  • 1
    Like (insert (make-string 10 ?X)) ? Or (dotimes (_ 10) (insert "X")) ? – phils May 17 at 4:19
  • 1
3

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

| improve this answer | |
3

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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