0

I am trying to create a command which deletes preceding whole lines, just like evil-delete-whole-line, but backwards. So far I have the following

(evil-define-operator delete-previous-line
  (beg end type register yank-handler)
  "Delete whole line."
  :motion evil-line-or-visual-line

  (interactive "<R><x>")
  (evil-previous-line 1)

  (evil-delete-whole-line beg end type register yank-handler))

It does delete the previous line, but unfortunately doesn't respond to the count operator. That is if I press certain number before using the operator the behavior isn't the expected one. How can I have the behavior mentioned in the beginning?

2
  • Are you sure that this deletes the previous line? It does not work here (the beg and end are set when invoking the command). Also, the command must be defined using 'evil' tools? Or would it be fine to just create an Emacs command? Or maybe even a recorded keyboard macro, which would definitely be the fastest and simplest solution (although that counts deleting multiple lines as multiple edits for undo). Dec 30, 2022 at 9:10
  • 1
    Evil command preferred, as I want it to be integrated in the evil ecosystem (e.g. yank goes into clipboard, the number written before the command executes it x times). Thanks, your answer does the job.
    – Rareform
    Dec 31, 2022 at 10:07

1 Answer 1

1

I am not sure how you would like the command to behave exactly, but I suppose that you would like to include the save-excursion. Also, I am not sure if I do things correctly (how they are supposed to be used). But by inspecting the evil code, I came up with the following command, which seems to do the trick:

(evil-define-operator delete-previous-line (beg end type register count yank-handler)
  "Delete preceding line(s)."
  :motion nil
  (interactive "<R><x><c><y>")
  (save-excursion
    (evil-previous-line 1)
    (setq end (or end (1+ (line-end-position))))
    (when count
      (evil-previous-line (1- count)))
    (setq beg (or beg (line-beginning-position)))
    (evil-delete beg end type register yank-handler)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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