Emacs: Single Key to Delete Whole Line
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-line
【Ctrl+Shift+Backspace ⌫】.
You can give it the same key as kill-line
, to replace it. 〔see Emacs Keys: Syntax〕
You can also set the Cut command (kill-ring-save
【Alt+w】) to cut the whole line when there is no selection. See: Emacs: How to Copy/Cut Current Line.
Thanks to [Mark Hepburn https://plus.google.com/110262280296887306226/posts] (), and [Dave Pawson https://plus.google.com/105317362555281065841/posts] ().
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.
- 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 newline char is included.
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)
2014-02-18 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 is 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:
- We don't even need any key spot, we just use the key for cut. This saves us key spots for other frequently used commands.
- Bonus: it makes the cut command smart, save us keystroks.
- Delete whole lines is more frequently needed than from cusor point to end. (and, it should include line ending)
- In fact, you don't need
kill-line
anymore, because it can be done by pressing Enter then kill whole line. A very easy key operation. It's more optimal to save that keyspot for other command.
for how to set this yourself, see Emacs: Copy 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:
- The key for cut command must be a key pressed by your pointing finger or middle finder. Or, bind it to a function key. Do not use Ctrl+x or Alt+x, because they are done by your weak 4th finger.
- Use a keyboard with mechanical key switch. (and stay away from Cherry MX Black.)
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 Key System, Part 1).)
2014-03-18 there's a minor mode [2014-03-18 whole-line-or-region ] ( https://github.com/purcell/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.
Emacs Modernization
- Emacs Modernization: Simple Changes Emacs Should Adopt
- Why Emacs Keys are Painful
- Emacs: Problems of the Scratch Buffer
- Emacs Modernization: Meta Key Notation
- Emacs Menu Usability Problem
- Emacs Mode Line Problem
- Emacs cua-mode Problems
- Emacs: Inconsistency of Search Features
- Problems of grep in Emacs
- Emacs: Usability Problems of Mode Documentation
- Problems of Emacs Manual
- Emacs Manual Sucks by Examples
- Emacs: kill-buffer Induces Buffer Accumulation
- Emacs Spell Checker Problems
- Emacs: Form Feed ^L Problem
- Emacs: Single Key to Delete Whole Line
- Emacs HTML Mode Sucks
- Emacs Does Not Support Viewing Images Files In Windows
- Emacs Should Adopt HTML as Texinfo Replacement
- Emacs Should Support HTML Mail
- Problems of Emacs's “man” Command
- Emacs Lisp Mode Syntax Coloring Problem
- Emacs AutoHotkey Mode Problems
- Elisp: Syntax Table Sucks
- Emacs: Make elisp-index-search use Current Symbol
- Emacs GNU Texinfo Problems; Invalid HTML
- A Record of Frustration in IT Industry; Disappearing FSF URLs, 2006
- Emacs Manual Node Persistency Issues
- Emacs: dired-do-query-replace-regex Replace ALL (fixed)
- Problems of Emacs Supporting Obsolete Systems
- Elisp: Function to Copy/Delete a Dir Recursively (fixed)
- Thoughts on Common Lisp Scheme Lisp Based Emacs
- Text Editors Popularity and Market Research
- Text Editor's Cursor Movement Behavior (emacs, vi, Notepad++)
- Emacs: Usability Problems of Letter-Case Changing Commands
- Emacs Select Word Command Problem
- Emacs: Search Current Word 🚀
- Emacs fill-paragraph Problem