I'd like to insert (programmatically) some text right after the point (without moving it). I came up with this:

(save-excursion (insert "my text"))

It seems to work. Is it a good way to do what I want? E.g., are there situations where this could break? (Other than those where insert would break anyway, like read-only buffers.) Is it different from what would more experienced Elisp hackers do?

  • 5
    Yes. Not much more to say.
    – shosti
    Commented Oct 13, 2014 at 21:33
  • Thanks; I was a bit worried about e.g. some (strange?) situations when it wouldn't work etc.
    – mbork
    Commented Oct 13, 2014 at 21:36
  • 3
    You could rephrase the question in that case. But 99% of the time, save-excursion + insert is the way to go.
    – shosti
    Commented Oct 13, 2014 at 21:39
  • OK, I'll do it. BTW: what is the 1%?
    – mbork
    Commented Oct 13, 2014 at 21:43
  • 1
    Weird modes with read-only overlays and that sort of thing. Nothing to worry about really.
    – shosti
    Commented Oct 13, 2014 at 22:00

2 Answers 2


A very common example of this behavior is electric-pair-mode in the Emacs standard library. If you've never used this mode before, then (quoting from the manual):

Whenever you insert an opening delimiter, the matching closing delimiter is automatically inserted as well, leaving point between the two.

You can check out the code for electric-pair-post-self-insert-function to see how it accomplishes the insertion. Spoiler:

(defun electric-pair-post-self-insert-function ()
  [lots of cond logic to decide if it's time to insert a matching closer]
      (save-excursion (insert closer)))))))

Yes, that is the most idiomatic way of inserting text after point that I know of. save-excursion is very resilient to changes in the buffer, so it is the preferred way to do destructive editing. The only place you're likely to run into issues is where you would have issues with insert itself, such as buffers where some text is read-only.

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.