26

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

0

3 Answers 3

31

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

2
  • 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, 2015 at 21:13
  • 1
    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, 2015 at 21:54
9

You can use some setf magic.

(setf (buffer-string) "")

-1

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).

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

Your Answer

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

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