2

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

Drew
  • 75,699
  • 9
  • 109
  • 225
henadzit
  • 123
  • 4

2 Answers2

3

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.

Aquaactress
  • 1,393
  • 8
  • 11
  • 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
  • FYI this seems to conflict with evil-surround and can lead to "ds*" commands not working. Is there any easy fix? – jds Dec 08 '21 at 22:16
0

Here is what I use:

(define-key evil-normal-state-map "x" 'delete-forward-char)
(define-key evil-normal-state-map "X" 'delete-backward-char)

Explanation:

  • delete-forward/backward-char is the function to delete something without yanking it to any register.
  • Vx: this would select a line (V) then delete it (x) without yanking it, so it might be what you want when you delete a blank line.
  • vexP: select a word (ve) and change it (xP) with content in the register without changing the content of the register, ie. paste the register over a word without changing the content of the register. This is quite usefull, for example, when you have a text foo bar barr barr barrr and you want to change all bar barr barrr to foo, then just yank foo then paste that foo to other words.

Logic:

  • While a region is selected, then d would do the same job as original x: “delete and yank”. So, change x to “delete without yank” is fine, without limiting capability at all. For example, when you want to delete a line and yank it, press Vd or dd.
  • If there is no region selected, then vd would delete one character and yank. Or, you can just press delete to do the same job. So, again, change x to delete without yank is also fine. Generally you do not need to delete and yank only one character anyway.

(sorry for my broken English)

Bach Lien
  • 131
  • 1
  • 4