Emacs Shortcut to Delete Whole Line

Perm url with updates: http://xahlee.org/emacs/emacs_delete_whole_line.html

Emacs Shortcut to Delete Whole Line

Xah Lee, 2010-04-28

I've been using the ErgoEmacs Keybinding for 3 years, and have spent hundreds of hours thinking about efficiency of commands and shortcut choices.

Today, it came to my mind that a shortcut 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 C-a, then C-k, C-k. (In vi, it is just a single operation, done by pressing “dd”.)

Define a Shortcut to Delete Whole Line

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

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

This is actually part of a personal set:

(defun xah-define-alt-num-hotkeys ()
  "define some Alt+num shortcut keys."
  (interactive)
  (global-set-key (kbd "M-3") 'insert-xah-url)
  (global-set-key (kbd "M-7") 'copy-file-path)
  (global-set-key (kbd "M-6") 'run-current-file)
  (global-set-key (kbd "M-9") 'kill-whole-line)
  (global-set-key (kbd "M-^") 'run-current-java-file)
  )

Note that “M-8” is used by default in ErgoEmacs for extend-selection.

Note that kill-whole-line command exist by default in emacs, you don't need to code it.

Make kill-line Delete Line Ending Char

Also, If you want kill-line to kill including the line ending char (from the cursor position), all you have to do is set a variable named “kill-whole-line” to true, like this:

(setq kill-whole-line t)

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 sharing the same symbol. Common Lisp is also this way.

Issues of Efficiency and Design

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

  • Delete from cursor position to end of line.
  • Delete from cursor position to beginning of line.
  • Delete the whole line the cursor is on.
  • Whether the ending line return char is included.

The problem with including the line ending when deleting a line is that, when you paste the line, often you do not want the line ending 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 char 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. Shortcut spots are precious. Line deletion related commands that we can consider to add a shortcut 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 shortcut spot, which 2 should we choose?

This requires some research. For example, by starting with myself, trying each 2 choices, or by testing by many other people, and over all by statistical data.

In ErgoEmacs, there are 2 shortcuts 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.

PS i've spent perhaps over a thousand hours over past 20 years fuzzing about keyboarding. Efficiency is my obsession. See: All About Keyboards, Keyboard Layouts, Shortcuts, Macros.

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs