Emacs: what is the shortcut key to clear buffer?

Emacs

Emacs Problem Overview


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

Emacs Solutions


Solution 1 - Emacs

How about using:

M-x erase-buffer

Which you could bind to whatever you want.

Solution 2 - Emacs

C-x h + del key clears the buffer

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

Solution 3 - Emacs

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.

Solution 4 - Emacs

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

Solution 5 - Emacs

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

Solution 6 - Emacs

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

M-< M-> C-w

Solution 7 - Emacs

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRonView Question on Stackoverflow
Solution 1 - EmacsTrey JacksonView Answer on Stackoverflow
Solution 2 - Emacsjith912View Answer on Stackoverflow
Solution 3 - EmacsTylerView Answer on Stackoverflow
Solution 4 - EmacstjbView Answer on Stackoverflow
Solution 5 - EmacsFrank da CruzView Answer on Stackoverflow
Solution 6 - EmacscadrianView Answer on Stackoverflow
Solution 7 - EmacsMattenView Answer on Stackoverflow