26

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

Drew
  • 75,699
  • 9
  • 109
  • 225

3 Answers3

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

Drew
  • 75,699
  • 9
  • 109
  • 225
  • 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
  • 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 '15 at 21:54
9

You can use some setf magic.

(setf (buffer-string) "")

Jordon Biondo
  • 12,332
  • 2
  • 41
  • 62
-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).