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?
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.
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.
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)
delete-forward/backward-charis 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 barrrand you want to change all
foo, then just yank
foothen paste that
footo other words.
- While a region is selected, then
dwould do the same job as original
x: “delete and yank”. So, change
xto “delete without yank” is fine, without limiting capability at all. For example, when you want to delete a line and yank it, press
- If there is no region selected, then
vdwould delete one character and yank. Or, you can just press
deleteto do the same job. So, again, change
xto delete without yank is also fine. Generally you do not need to delete and yank only one character anyway.
(sorry for my broken English)