Emacs - Skip whitespace kills

553 Views Asked by At

I'm trying to make the kill ring essentially ignore whitespace only entries (tabs, newlines, just spaces, etC), I'm fairly new to elisp and I'm pretty sure the way to do is by doing defadvice but I have a few questions.

  • Would it better to stop whitespace entries from ever getting into the kill ring in the first place, or to skip them on yank? I assumed the latter.

In which case, I'm completely lost on which function I should advise, its between current-kill, yank, and insert-for-yank - but am not entirely sure which I should manipulate to not yank whitespace from the kill ring.

Thanks!

EDIT: I'm pretty sure the way to do this is to manipulate `current-kill' to keep calling itself until it reaches a non whitespace entry? (or the end of the ring, whichever comes first)

3

There are 3 best solutions below

2
Nicolas Dudebout On BEST ANSWER

From the comments it seems that you are having problems with whitespaces in your kill-ring since you kill whitespace lines. My solution would be to avoid killing whitespace lines and to use the function delete-blank-line (C-x C-o) instead. This reduces a group a blank lines (including whitespaces and tabs) to a single blank line.

0
HappyFace On

The following advice does not let whitespace be added to the kill ring in the first place:

(defun night/h-kill-skip-whitespace (orig-fn string &optional rest)
  (let* (
         (string-raw (substring-no-properties string))
         (space-p (not (string-match-p "[^ \t\n\r]" string-raw))))

    (cond
     ((not space-p)
      (apply orig-fn string rest))
     (t
      (message "skipped whitespace kill")
     ))))

(advice-add 'kill-new :around #'night/h-kill-skip-whitespace)
0
Nicholas Hubbard On

I wrote a package clean-kill-ring.el that provides functionality for controlling what is allowed in the kill ring.

By default enabling clean-kill-ring-mode prevents blank strings from entering the kill ring, but further customization is possible as well.