Copy/Paste in emacs ansi-term shell Copy/Paste in emacs ansi-term shell shell shell

Copy/Paste in emacs ansi-term shell


You may want to simply switch between character mode and line mode while using the terminal. C-c C-j will run term-line-mode, which treats the terminal buffer more like a normal text-buffer in which you can move the cursor and yank text. You can switch back to character mode by running term-char-mode with C-c C-k.


As described in this lovely blog snippet, there's a function, term-paste, in term.el, that does exactly what you want. By default it's bound only to S-insert but the blog's recommended C-c C-y seems like a good suggestion.


ansi-term, in char-mode, takes the ordinary bindings for the terminal emulation. You need a new binding, plus a way to output to ansi-term correctly. I use this:

(defun ash-term-hooks ()  ;; dabbrev-expand in term  (define-key term-raw-escape-map "/"    (lambda ()      (interactive)      (let ((beg (point)))        (dabbrev-expand nil)        (kill-region beg (point)))      (term-send-raw-string (substring-no-properties (current-kill 0)))))  ;; yank in term (bound to C-c C-y)  (define-key term-raw-escape-map "\C-y"    (lambda ()       (interactive)       (term-send-raw-string (current-kill 0)))))  (add-hook 'term-mode-hook 'ash-term-hooks)

When you do this, C-c C-y will yank. It only does one yank, though, and you can't cycle through your kill-buffer. It's possible to do this, but I haven't implemented it yet.