Next: , Previous: , Up: Commands   [Contents][Index]

4.8 Hungry Deletion of Whitespace

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:

Using Hungry Delete Mode with DEL and C-d

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.

DEL (c-electric-backspace)

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-newline Insertion), the command just deletes backwards in the usual way. (More precisely, it calls the function contained in the variable c-backspace-function, passing it the prefix argument, if any.)

c-backspace-function

Hook that gets called by c-electric-backspace when it doesn’t do an “electric” deletion of the preceding whitespace. The default value is backward-delete-char-untabify (see Deletion in GNU Emacs Lisp Reference Manual, the function which deletes a single character.

C-d (c-electric-delete-forward)

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 just does delete-char, more or less. (Strictly speaking, it calls the function in c-delete-function with the prefix argument.)

c-delete-function

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 delete-char.

Using Distinct Bindings

The other (newer and recommended) way to use hungry deletion is to perform c-hungry-delete-backwards and c-hungry-delete-forward directly through their key sequences rather than using the minor mode toggling.

C-c C-DEL, or C-c DEL (c-hungry-delete-backwards)14

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.

C-c C-d, C-c C-DELETE, or C-c DELETE (c-hungry-delete-forward)

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 XEmacs variable. When this variable is non-nil, c-electric-delete will do forward deletion with c-electric-delete-forward, otherwise it does backward deletion with c-electric-backspace. Similarly, C-c Delete and C-c C-Delete are bound to c-hungry-delete which is controlled in the same way by delete-key-deletes-forward.

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.


Footnotes

(13)

Prior to CC Mode 5.31, this command was bound to C-c C-d. C-c C-d is now the default binding for c-hungry-delete-forward.

(14)

This command was formerly known as c-hungry-backspace.

Next: Subword Movement and Editing, Previous: Auto-newline Insertion, Up: Commands   [Contents][Index]