Emacs: Single Key to Delete Whole Line

Advertise Here

, ,

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

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 “dd”.)

Define a Key to Delete Whole Line

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

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

(i choose 【Alt+9】 because most other 【Alt+‹number›】 spots are already used for my personal keys.)

The kill-whole-line command is part of emacs, you don't need to define it.

Even better is to set the Cut command do cut the whole line when there's no selection, so you don't need to waste a key spot for a command. See: Emacs: How to Copy/Cut Current Line.

Make kill-line Delete Line Ending Character

Also, If you want kill-line to kill including the ending newline character, all you have to do is set a variable named kill-whole-line to true, like this:

(setq kill-whole-line t) ; make kill-line include the ending newline char

Note: the variable kill-whole-line doesn't have anything to do with the function of the same name. Yes, in Emacs Lisp, you can have a function and variable both having the same name. Common Lisp is also this way.

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 consistency 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”.

blog comments powered by Disqus