0

I'm looking for ways to get the last copy from the kill ring in Emacs.

I run

(insert (yank))[cursor]

with C-x C-e and I get this *Backtrace*:

Debugger entered--Lisp error: (wrong-type-argument char-or-string-p nil)
  insert(nil)
  eval((insert (yank)) nil)
  elisp--eval-last-sexp(nil)
  eval-last-sexp(nil)
  funcall-interactively(eval-last-sexp nil)
  call-interactively(eval-last-sexp nil nil)
  command-execute(eval-last-sexp)

What I'm trying to do is get the last copied text from the kill-ring (I think it would make it feel more polished if when I call this it removes the last copy from the kill-ring when ran, but I haven't found how to do that yet.), so I can have the defun that calls this get the last think I copied.

The command works correctly, but also calls up this Backtrace. I don't know what the backtrace message wants me to do? I would suspect that I'm not supposed to call yank in this manner.

1 Answer 1

2

Let's look at the documentation for yank. From C-h f yank RET:

Reinsert ("paste") the last stretch of killed text.

Oh, so yank doesn't just return the last killed text; it inserts it too. So you can simply run (yank) to put that text into your buffer.

Errors?

So why are we seeing the wrong-type-argument error? Let's run (yank) in IELM, so we can see its return value

ELISP> (yank)
nil
ELISP> I killed this text previously.

Oh! So (yank) returns nil. Amusingly, the inserted text is inserted into IELM's next prompt! To complete the circle, let's try running (insert nil). When I do that, I also get a wrong-type-argument error.

2
  • I get it now. Yank in the equivalent of paste, it actually pastes the copied text into the buffer, so then insert wouldn't have any work to do resulting in an error.
    – 9716278
    Commented Nov 18, 2019 at 1:16
  • Yank pastes, yeah. It's not so much that insert "doesn't have any work to do" -- that ascribes too much agency to the code. It's that yank returns nil, and insert errors on nil. If yank also returned the string inserted into the buffer, you'd end up with the string inserted twice, and no error.
    – zck
    Commented Nov 18, 2019 at 14:06

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.