Emacs: Single Key to Delete Whole Line

By Xah Lee. Date: . Last updated: .

This page tells you how to set emacs to kill (cut/delete) a whole line, and discuss some efficiency issues.

To delete a whole line, Alt+x kill-whole-lineCtrl+Shift+⌫ Backspace】.

You can give it the same key as kill-line, to replace it. 〔►see Emacs Keybinding Syntax Examples

You can also set the Cut command (kill-ring-saveAlt+w】) to cut the whole line when there's no selection. See: Emacs: How to Copy/Cut Current Line.

Thanks to Mark Hepburn (), and Dave Pawson ().

Efficiency Issues

[the following is written in as a blog on some efficiency issues. It remains here as a historical archive.]

I've been using the ErgoEmacs Keybinding for 3 years now, and have spent hundreds of hours thinking about efficiency of commands and key choices. Today, it came to my mind that a key to delete the whole line, is actually quite useful. Because, i noticed that i'm tired of having to move cursor to beginning of line first then kill. The problem is made worse by the fact that kill-line does not kill the line ending. So, to delete a line in emacs, you have to move cursor to the beginning first, then do kill-line twice, a total of 3 operations. In GNU Emacs, the key presses are【Ctrl+a Ctrl+k Ctrl+k】. (In vi, it is just a single operation, done by pressing 【d d】.)

So, personally, i made a key to kill the whole line. Like this:

(global-set-key (kbd "M-9") 'kill-whole-line)

Issues of Efficiency and Design

For a command that deletes a line, there are several issues.

The problem with including the ending newline char when deleting a line is that, when you paste the line, often you do not want the newline char as part of the paste. I have not made statistical study of this. This is just my gut feelings.

Suppose that my assumption about the ending newline issue is correct, then it could be fixed. When deleting a whole line, the kill-ring's copy do not include the line ending, except when there are multiple sequence of kill-line operations. Of course, this solution is getting complicated, breaks the simplicity of how deleted text is copied to the kill-ring. So, overall, i think this isn't a solution.

Also, now consider keyboard shortcuts. Key spots are precious. Line deletion related commands that we can consider to add a key are: {kill-line, kill-line-backward, kill-whole-line}, and also a version of each that includes the line ending char except kill-line-backward, so there are total of 5 commands. Let's say only 2 of these commands can have prime key spot, which 2 should we choose?

This requires some research. For example, by starting with myself. Pick 2 commands and try them for 2 weeks. Do this for all possible 2 choices out of the 5. Generally, this research can be done in some scientific way by statistical means, with lots of people.

In ErgoEmacs, there are 2 keys to delete line, they are simply kill-line and kill-line-backward, with key 【Alt+g】, and 【Alt+Shift+g】. The kill-line do not include line ending char.

Perhaps, a alternative choice would be kill-whole-line for 【Alt+g】, and kill-whole-line with line ending char at 【Alt+Shift+g】.

From my experience, kill-line-backward isn't frequently used, and kill-whole-line is more frequently needed than kill-line. So, i think that emacs would have been more efficient if kill-line's semantic was kill-whole-line.

Now having spend a hour writing this out, i think it's actually worth-while to try remapping kill-line to kill-whole-line to test things out. But, this would be a pain, because then i'll have to spend weeks changing my muscle memory, and if things didn't work out, i'll have to change and relearn back. And if i found kill-whole-line to be more efficient than kill-line, i probably won't change ErgoEmacs keybinding because that would break some compatibility issue with emacs. So, this would be mostly to satisfy a curiosity. For now i'll just see how my 【Alt+9】 works out.

Another related issue is that, emacs's text deleting commands automatically place deleted text into its clipboard the “kill-ring”. This is different from modern text editors, which only put things in clipboard when user explicitly calls the copy command. In the past i've wondered if this emacs way is actually more efficient. I made some study of it, reported here: The Operative Efficiency of Emacs's Deleting Text to “kill-ring”.

Conclusion: Use Cut to Delete Line(S)

addendum

I haven't been using any of the kill line commands in emacs since 2011.

the best solution, from my personal experience, is to just use the cut command (xah-cut-line-or-region, which is a modified version of GNU emacs's kill-region). When there's no text selection, it'll will do kill-whole-line, including the line ending character.

i've been doing this since 2011, for 3 years now. This is excellent solution, because:

for how to set this yourself, see Emacs: Copy/Cut Current Line If No Selection.

RSI WARNING: Don't Use X Key for Cut

If you plan to use cut for all line deletion, be sure to do one or more of the following:

Because, when i started to use cut to kill line, i started to use it extensively, even for deleting entire paragraphs, by pressing cut key repeatedly. That puts a whole lot of stress on my left 4th finger. (because i was using 【Alt+x】 on a rubber dom keyboard the Microsoft Natural Ergonomic Keyboard 4000. And, it got me into RSI. See: How the X Key Got Me RSI. (the Roadmap to Completely Replace Emacs's Key System, Part 1).)

there's a minor mode whole-line-or-region that changes any command that requires region (aka text selection) to act on current line, when there is no text selection. Thanks to Steve Purcell for the tip.

Like it? Buy Xah Emacs Tutorial. Thanks.