Copy and paste into a VTerm buffer

First, extended Avy to copy an entire line to the kill-ring.

Then added a keybinding to Vterm to copy/paste into the buffer.
This commit is contained in:
Howard Abrams 2026-02-12 14:46:19 -08:00
parent 222d904d3e
commit a02093de9c
2 changed files with 52 additions and 1 deletions

View file

@ -809,6 +809,37 @@ If you hit the following keys /before/ you select a target, you get special acti
- ~z~ :: =zap-to-char= … kill from current point to the target
Im not thinking of ideas of what would be useful, e.g. ~v~ to highlight from cursor to target, etc.
What is missing is copying the entire line:
#+BEGIN_SRC emacs-lisp
(use-package avy
:config
(defun avy-action-copy-whole-line (pt)
(save-excursion
(goto-char pt)
(cl-destructuring-bind (start . end)
(bounds-of-thing-at-point 'line)
(copy-region-as-kill start end)))
(select-window
(cdr
(ring-ref avy-ring 0)))
t)
(defun avy-action-copy-rest-line (pt)
(save-excursion
(goto-char pt)
(let ((start pt)
(end (line-end-position)))
(copy-region-as-kill start end)))
(select-window
(cdr
(ring-ref avy-ring 0)))
t)
(setf (alist-get ?w avy-dispatch-alist) 'avy-action-copy
(alist-get ?W avy-dispatch-alist) 'avy-action-copy-whole-line
(alist-get ?N avy-dispatch-alist) 'avy-action-copy-rest-line))
#+END_SRC
Want to know something amazing. In a Terminal, like =vterm= or =eshell=, I run ~s-g~ and pinpoint the UUID in the output of a long command. Then type ~y~ and then ~C-y~ to paste that ID without even moving the mouse.
*** Link Hint, the Link Jumper
The [[info:emacs#Goto Address mode][Goto Address]] mode (see this [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Goto-Address-mode.html][online link]]) turns URLs into clickable links. Nice feature and built into Emacs, but it requires using the mouse or moving to the URL and hitting ~Return~ (if you like this idea, check out [[https://xenodium.com/actionable-urls-in-emacs-buffers/][Álvaro Ramírez's configuration]] for this).

View file

@ -62,7 +62,7 @@ Will Schenk has [[https://willschenk.com/articles/2020/tramp_tricks/][a simple e
(apply orig-fun args)))
(advice-add 'tramp-completion-handle-file-name-all-completions
:around #'ha-tramp-completion-docker)
:around #'ha-tramp-completion-docker))
#+end_src
Keep in mind you need to /name/ your Docker session, with the =—name= option. I actually do more docker work on remote systems (as Docker seems to make my fans levitate my laptop over the desk). Granted, the =URL= is a bit lengthy, for instance:
@ -157,6 +157,26 @@ VTerm has an issue (at least for me) with ~M-Backspace~ not deleting the previou
#+end_src
The advantage of running terminals in Emacs is the ability to copy text without a mouse. For that, hit ~C-c C-t~ to enter a special copy-mode. If I go into this mode, I might as well also go into normal mode to move the cursor. To exit the copy-mode (and copy the selected text to the clipboard), hit ~Return~.
An odd use case I have: working in a Terminal session in one buffer window, and reading instructions in another. I want to copy commands from the document into the session using Avy to mark the command. Normally, I would use ~s-g~ to call [[file:ha-config.org::*Jump with Avy][avy-goto-char-timer]], and typing the /beginning of the command/, but before /jumping/, I press ~N~ to copy the line into the clipboard (kill-ring), and then /paste/ that into the Terminal using ~C-y~. One-too-many keystrokes.
Now, I can type: ~s-G~ (with the shift), select the line with Avy, and type ~N~ to have it copy/paste into the buffer:
#+BEGIN_SRC emacs-lisp
(use-package vterm
:config
(defun ha-vterm-avy-paste-line ()
"docstring"
(interactive)
(let ((buf (current-buffer)))
(call-interactively 'avy-goto-char-timer)
(when (equal buf (current-buffer))
(vterm-yank))))
(general-def 'vterm-mode-map "s-G" 'ha-vterm-avy-paste-line))
#+END_SRC
** Eat
While not as fast as [[https://github.com/akermu/emacs-libvterm][vterm]], the [[https://codeberg.org/akib/emacs-eat][Emulate a Terminal]] project (eat) is fast enough, and doesnt require a dedicate library that requires re-compilation. While offering [[https://elpa.nongnu.org/nongnu-devel/doc/eat.html][online documentation]], Im glad for an [[info:eat#Top][Info version]].