65

Like, Control-A (select all) followed by delete?

nbro
  • 15,395
  • 32
  • 113
  • 196
Ron
  • 7,588
  • 11
  • 38
  • 42
  • A related (common) problem: to clear the mini-buffer using the `esc` key, set this in your init: `(global-set-key (kbd "") 'keyboard-escape-quit)` – silian-rail Nov 30 '20 at 07:07

7 Answers7

87

How about using:

M-x erase-buffer

Which you could bind to whatever you want.

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • 3
    I have this in my .emacs: `(global-set-key (kbd "C-x ") 'erase-buffer)` – Pål GD Nov 01 '15 at 15:49
  • 2
    For new users, when `M-x erase buffer` is invoked for the first time you will receive a prompt explaining that it has been disabled but you can enable it . If unsure about exactly what this command does, I recommend running it with the `SPACE` option the first time to see what happens. – Nathan Feb 04 '16 at 18:07
45

C-x h + del key clears the buffer

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

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
jith912
  • 884
  • 1
  • 9
  • 10
  • 4
    This (or `M-x erase-buffer`) does not add the contents of the buffer to the kill ring, but `C-x h C-w` does, and `C-x h C-w` also works when transient mark mode is not enabled. – nisetama Jun 18 '16 at 06:42
33

Select all in Emacs is:

C-x h

(technically, that's mark-whole-buffer) and kill-region (to kill the marked region, which is now the entire buffer) is:

C-w

If you want to delete the region without copying it to the kill-ring, you can use

M-x delete-region

instead. If you do this alot, you'll want to bind delete-region to a key/key combo.

Tyler
  • 9,872
  • 2
  • 33
  • 57
  • 8
    The difference between this solution and M-x erase-buffer, is that this solution copies the deleted text into the kill-ring. That can be very annoying if one wants to delete a large file as copying a ton of text to the kill ring can be slow. This behavior is even more annoying if one is a user of browse-kill-ring.el – Noah Sussman Nov 28 '12 at 14:59
6

These macros build on the answers given above. To start using them paste them into your .emacs then restart emacs or (while in the .emacs buffer) type M-x eval-buffer.

(defun clear-buffer ()
  "clear whole buffer add contents to the kill ring"
  (interactive)
  (kill-region (point-min) (point-max))
  )

(defun clear-buffer-permenantly ()
  "clear whole buffer, contents is not added to the kill ring"
  (interactive)
  (delete-region (point-min) (point-max))
  )
tjb
  • 11,480
  • 9
  • 70
  • 91
5

Old folks might like to call it hk rather than clear-buffer-permanently, and assign it to the nostalgic key sequence like so:

(define-key esc-map "\^[hk" 'hk)

That's the pre-Gnu TECO EMACS command to clear the buffer (ESC ESC wHole Kill).

Frank da Cruz
  • 51
  • 1
  • 2
4

Go to the begin of the buffer, go to the end (both set the mark), then cut:

M-< M-> C-w
cadrian
  • 7,332
  • 2
  • 33
  • 42
2

There is no shortcut, but you can define one... Follow this link to get a macro for clearing a buffer.

Matten
  • 17,365
  • 2
  • 42
  • 64