19

If I want to delete everything in a buffer with elisp, how do I do that?

22

Have you tried erase-buffer?

erase-buffer is an interactive built-in function in `C source code'.

(erase-buffer)


Delete the entire contents of the current buffer.
Any narrowing restriction in effect (see `narrow-to-region') is removed,
so the buffer is truly empty after this.

How to find such a function? M-x apropos buffer erase

  • 1
    There are a few exceptions, e.g., overlay objects are not deleted, and there may be some other stuff I am not familiar with that continues to be present after erase-buffer is called. – lawlist Oct 19 '15 at 21:13
  • Yes, the question is not very precise wrt "everything" and "in" the buffer. If you want to get rid of everything associated with the buffer, then kill-buffer is your friend. – Drew Oct 19 '15 at 21:54
7

You can use some setf magic.

(setf (buffer-string) "")

0

There's also this answer:

C-x h + del key clears the buffer

Note: This requires transient-mark-mode to be enabled (which it is by default).

  • But this is a key sequence, whereas the OP asked for an Elisp answer. – Stefan Jul 19 '17 at 3:08
  • Oops, posted too quickly. – Joshua Goldberg Jul 19 '17 at 13:46

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.