In an Emacs Lisp function, I would like to insert some text at point or - if the region is active - overwrite it with such text. How to do it?

1 Answer 1


For Emacs setting:

(delete-selection-mode t)  ; delete seleted text when typing

For lisp code, add following line before insert:

(if (region-active-p) (delete-region (region-beginning) (region-end)))
  • Thank you, but I'm trying to do that from Lisp code.
    – Eleno
    May 30, 2018 at 4:29
  • Sorry, I didn't read your question carefully. I updated my answer, hope it helps.
    – AhLeung
    May 30, 2018 at 5:36

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.