0

How can I use the last thing yanked in a command I'm writing?

For example, if the last text I yanked was "test" I want to use this string somehow in my command.

(I use avy and evil.)

1 Answer 1

1

The last thing you yanked is typically the front of the kill-ring.

You can access that programmatically as (current-kill 0 'DONT-MOVE).

For example:

(defun my-command ()
  "..."
  (interactive)
  (message "The current kill string is: %s" (current-kill 0 'DONT-MOVE)))

Then you can do M-x my-command to yank what you last yanked.

Actually, the current kill is not necessarily what you last yanked. It's what you last yanked, killed, or copied to the kill-ring, whichever of those actions is most recent. And it's what your next yank would yank (by default).


(I think using Avy and Evil are irrelevant to what you're asking, so I removed those tags.)

(Your question as originally worded spoke of using the latest yank as an "argument" of the command or other function you're writing. It's not clear how you want to do that - from what you want to pass it to your function as an argument. But the idea is the same: you access the latest kill using function current-kill.)

(Your question as originally worded spoke of "interactive function", so I included interactive in the answer. Dunno if that's what you're really looking for.)

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.