0

I have a question about how to implement a command specific to avy. A template of a command which I would like to generalize is avy-kill-ring-save-whole-line. I want to be able to copy/delete from char to char on a certain line.

Given some line ..... ------ ..... I want to select the line with avy shortletters notation while being on a different line and then to copy ------ with avy. How can I create such a function? I use Doom Emacs on macOS.

1 Answer 1

2
+50

As far as I understand your question, you would like to make a version of avy-kill-ring-save-whole-line that kills only a selected part of a line. Starting from avy-goto-char-timer, you could achieve it as follows:

(defun my-avy-kill-region (arg)
  (interactive "P")
  (let ((beg (let ((cands (avy--read-candidates)))
               (car (avy-process cands))))
        (end (let ((cands (avy--read-candidates)))
               (cdr (avy-process cands)))))
    (if arg
        (kill-region beg end)
      (kill-new (buffer-substring beg end)))))

or if you prefer to jump to the line first and then select chars on that line like avy-goto-char, then starting from avy-goto-char/avy-jump you could achieve it as follows

(defun my-avy-kill-line-region (arg)
  (interactive "P")
  (save-excursion
    (goto-char (avy--line))
    (let* ((eol (pos-eol))
           (beg (goto-char (car (avy-process (avy--regex-candidates (string (read-char)) (pos-bol) (pos-eol))))))
           (end (cdr (avy-process (avy--regex-candidates (string (read-char)) beg eol)))))
      (if arg
          (kill-region beg end)
        (kill-new (buffer-substring beg end))))))

When using this last command, you first, temporarily, jump to some line, and subsequently (type)select a char twice for 'beg' and 'end'.

Both commands you can prepend with a universal-argument (C-u) to delete the region instead of copying it (if you prefer to 'swap' this behavior, then swap the clauses in the if form).

1
  • @Rareform I have updated the answer with a slightly modified version of your editing suggestion (it is generally preferable to use 'native' functions instead of evil functions if possible). Thanks for the suggestion! Commented Dec 12, 2023 at 16:25

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.