Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Kill Entire Line with Prefix Argument

I won’t repeat myself on the usefulness of prefix arguments, though it would hardly be an overstatement. Killing 7 lines of text in two keystrokes is a bliss most people will never know.

Today's post was prompted by this question on Emacs Stack Exchange. Itsjeyd has grown tired of using 3 keystrokes to kill a line (C-a C-k C-k) and asks for an alternative. The straightforward answer is to use kill-whole-line instead, but then you either need another keybind, C-S-backspace, or you need to lose the regular kill-line functionality.

The solution I've found for myself is to use prefix arguments.
You see, killing half a line is a useful feature, but slaughtering three and a half lines make very little sense. So it stands to reason to have kill-line meticulously murder everything in its sight when given a prefix argument.

(defmacro bol-with-prefix (function)
  "Define a new function which calls FUNCTION.
Except it moves to beginning of line before calling FUNCTION when
called with a prefix argument. The FUNCTION still receives the
prefix argument."
  (let ((name (intern (format "endless/%s-BOL" function))))
    `(progn
       (defun ,name (p)
         ,(format 
           "Call `%s', but move to BOL when called with a prefix argument."
           function)
         (interactive "P")
         (when p
           (forward-line 0))
         (call-interactively ',function))
       ',name)))

And we bind them, of course.

(global-set-key [remap paredit-kill] (bol-with-prefix paredit-kill))
(global-set-key [remap org-kill-line] (bol-with-prefix org-kill-line))
(global-set-key [remap kill-line] (bol-with-prefix kill-line))

With this little macro, C-k still kills from point, but C-3 C-k swallows three whole lines. As a bonus, we get the kill-whole-line behavior by doing C-1 C-k.

Are there any other functions which might benefit from this macro?

Tags: keybind, init.el, emacs,

comments powered by Disqus