If you want to delete an entire block of whitespace at point, you can use hungry deletion. This deletes all the contiguous whitespace either before point or after point in a single operation. “Whitespace” here includes tabs and newlines, but not comments or preprocessor commands. Hungry deletion can markedly cut down on the number of times you have to hit deletion keys when, for example, you’ve made a mistake on the preceding line and have already pressed C-j.
Hungry deletion is a simple feature that some people find extremely useful. In fact, you might find yourself wanting it in all your editing modes!
Loosely speaking, in what follows, DEL means “the backspace key” and DELETE means “the forward delete key”. This is discussed in more detail below.
There are two different ways you can use hungry deletion:
Here you toggle Hungry Delete minor mode with M-x c-toggle-hungry-state13 (see Minor Modes.) This makes DEL and C-d do backwards and forward hungry deletion.
This command is run by default when you hit the DEL key. When
hungry delete mode is enabled, it deletes any amount of whitespace in
the backwards direction. Otherwise, or when used with a prefix
argument or in a literal (see Auto-newlines), the command just
deletes backwards in the usual way. (More precisely, it calls the
function contained in the variable
passing it the prefix argument, if any.)
Hook that gets called by
c-electric-backspace when it doesn’t
do an “electric” deletion of the preceding whitespace. The default
(see Deletion in GNU Emacs Lisp Reference Manual, the function which
deletes a single character.
This function, which is bound to C-d by default, works just like
c-electric-backspace but in the forward direction. When it
doesn’t do an “electric” deletion of the following whitespace, it
delete-char, more or less. (Strictly speaking, it
calls the function in
c-delete-function with the prefix
Hook that gets called by
c-electric-delete-forward when it
doesn’t do an “electric” deletion of the following whitespace. The
default value is
The other (newer and recommended) way to use hungry deletion is to
c-hungry-delete-forward directly through their key sequences
rather than using the minor mode toggling.
Delete any amount of whitespace in the backwards direction (regardless whether hungry-delete mode is enabled or not). This command is bound to both C-c C-DEL and C-c DEL, since the more natural one, C-c C-DEL, is sometimes difficult to type at a character terminal.
Delete any amount of whitespace in the forward direction (regardless whether hungry-delete mode is enabled or not). This command is bound to both C-c C-Delete and C-c Delete for the same reason as for DEL above.
When we talk about DEL, and Delete above, we actually do so without connecting them to the physical keys commonly known as Backspace and Delete. The default bindings to those two keys depends on the flavor of (X)Emacs you are using.
In XEmacs 20.3 and beyond, the Backspace key is bound to
c-electric-backspace and the Delete key is bound to
c-electric-delete. You control the direction it deletes in by
setting the variable
delete-key-deletes-forward, a standard
When this variable is non-
c-electric-delete will do
forward deletion with
c-electric-delete-forward, otherwise it
does backward deletion with
C-c Delete and C-c C-Delete are bound to
c-hungry-delete which is controlled in the same way by
Emacs 21 and later automatically binds Backspace and
Delete to DEL and C-d according to your environment,
and CC Mode extends those bindings to C-c C-Backspace
etc. If you need to change the bindings through
normal-erase-is-backspace-mode then CC Mode will also adapt
its extended bindings accordingly.
In earlier (X)Emacs versions, CC Mode doesn’t bind either Backspace or Delete directly. Only the key codes DEL and C-d are bound, and it’s up to the default bindings to map the physical keys to them. You might need to modify this yourself if the defaults are unsuitable.
Getting your Backspace and Delete keys properly set up can sometimes be tricky. The information in DEL Does Not Delete in GNU Emacs Manual, might be helpful if you’re having trouble with this in GNU Emacs.
Prior to CC Mode 5.31, this command
was bound to C-c C-d. C-c C-d is now the default binding
This command was formerly known as