0

How can I configure Emacs in the Evil mode to delete an empty line (d d) without copying it but still copy it when deleting a non-empty line?

Thanks!

1

You can try this.

(evil-define-operator evil-delete-without-register-if-whitespace (beg end type reg yank-handler)
  (interactive "<R><y>")
  (let ((text (replace-regexp-in-string "\n" "" (filter-buffer-substring beg end))))
    (if (string-match-p "^\\s-*$" text)
      (evil-delete beg end type ?_)
      (evil-delete beg end type reg yank-handler))))

Followed by rebinding d with it:

(define-key evil-normal-state-map "d" #'evil-delete-without-register-if-whitespace)

I found this looking at this emacs configuration.

What you're asking for does not require knowledge of spacemacs. It involves evil, a package that is included in spacemacs.

Deleting in evil is an operator. This defines a new operator to replace the default one; the main difference being that it checks to see if what's being added to the register is whitespace. If it is it adds nothing, otherwise it adds it to the register.

To learn more about evil check out noctuid's evil guide.

Note: that this is not strictly what you asked for because you spoke of not adding empty lines to the register while this doesn't add whitespace in general to the register (even if not a full empty line). But I suspect you'd actually find this more useful.

  • Thank you for such detailed answer! That's exactly what I was looking for! Also thank you for the references and explanations, this is super helpful for a beginner! – henadzit Mar 15 '18 at 19:06
  • No problem! I'd appreciate it if you could change the title of the post. Maybe to Evil - don't a yank with only whitespace to register or something along those lines. yank is the emacs/vim term for copying. This is so that others with the same question can find it. Please also change the spacemacs tag to evil and add a register tag if it exists. – Aquaactress Mar 15 '18 at 19:31
  • My bad I made a typo. I meant Evil - don't yank with only whitespace to a register. Sorry for the inconvenience. – Aquaactress Mar 15 '18 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.