8

How can I copy from the command line and paste in Emacs in terminal mode?

If I select the text with the mouse and paste it from the clipboard with the third button (the wheel) it is OK.

But if I try to run:

$ emacs -nw file_name.sh (for example)

emacs opens the file which I start editing, then I C-z to go back to the terminal.

$ recall a command from the history

go to the beginning of the line and press C-k to kill it

$ fg

go back to Emacs and try to paste it with C-y doesn't work.

Dan
  • 32,584
  • 6
  • 98
  • 168
Andrea Borga
  • 261
  • 1
  • 4
  • 9
  • Dan I have to admit that it ticks me off when people edit my posts to remove my sign of gratitude toward the others who will help me out. I find this a very rude practice of stack exchange moderators. Perhaps it is a feature of the younger generations, but I grow up in an era where people said "thank you!". Please respect the others more. Thanks Andrea. – Andrea Borga Jul 06 '16 at 18:17
  • 1
    Please do not take offense. Stack Exchange site norms are such that we don't have salutations, thank-yous, and sign-offs. Unless you're otherwise rude in your post, most people will understand that you're grateful for their help. See, for example, [What should I keep out of my posts and titles?](http://meta.stackexchange.com/questions/131009/what-should-i-keep-out-of-my-posts-and-titles) and [Should 'Hi', 'thanks', taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts) – Dan Jul 06 '16 at 19:09
  • OK. Fine, I see your point. – Andrea Borga Jul 06 '16 at 21:15

5 Answers5

10

You can't copy-paste between Emacs and a shell without using a system-wide copy-paste facility. Text terminals in general do not provide a copy-paste facility.

It seems that you're running this terminal Emacs in a terminal window in a window environment. Window environments do provide copy-paste; that's what happens when you use the mouse. However, this happens outside the knowledge of Emacs and Bash: what you copy is the text that appears on the screen, and what you paste appears to the program as if you'd typed it very quickly.

If you want to integrate Bash's clipboard with the window environment's clipboard, you can use command line copy-paste tools: xsel or xclip on X11, pbcopy/pbpaste on OS X, /dev/clipboard on Cygwin. See Share the clipboard between bash and X11 for the Bash side. You can do something similar on the Emacs side, though it doesn't make that much sense since you can run a window Emacs.

If no window environment is available, you can run both Bash and Emacs inside a terminal multiplexer such as Screen or tmux. Their copy-paste facility suffers the same limitation as the mouse-based one: since it's provided by the terminal, it can only copy output that's on the terminal and provide input as if it came from the terminal.

If you want to exchange data between Emacs and Bash, and no common clipboard is available (e.g. because the programs are running on a remote machine), use a temporary file.

Another possibility is to run your shell within Emacs, as suggested by DoMiNeLa10 . In that case, Emacs provides the copy-paste facility.

  • wow! Thanks a lot for the complete picture of the problem Gilles! Very useful and. I'll use the trick of the temp file as you suggest. – Andrea Borga Jul 06 '16 at 18:12
  • If I use Screen or tmux and one terminal is in `local/shell` and other one is in `host(ssh_connected)/emacs` can I do the copy/paste operation in between? – alper Oct 04 '20 at 10:42
  • 1
    @alper You can use the GUI copy-paste but that only lets you copy the text that's visible on the screen and pasting can do things like reformat the text depending on the capabilities of the terminal. It's a lot more convenient to use a single local Emacs and open remote files from within Emacs. – Gilles 'SO- stop being evil' Oct 05 '20 at 11:13
2

If you want to copy-paste easily between a shell and Emacs, you can try using M-x shell from Emacs, instead of running the shell independently. I haven't tried integrating my Emacs and Bash kill rings.

  • This does not provide an answer to the question. Once you have sufficient [reputation](http://emacs.stackexchange.com/help/whats-reputation) you will be able to [comment on any post](http://emacs.stackexchange.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/6343) – Drew Jul 06 '16 at 14:12
  • @Drew It does solve the problem, or at least works around it, even if the solution isn't in the originally-expected direction. – Gilles 'SO- stop being evil' Jul 06 '16 at 17:04
  • Hi @DoMiNeLa10, thanks for the tip. But I needed a more complete analysis of my problem. As Gilles answered. – Andrea Borga Jul 06 '16 at 18:13
2

If you are using Windows you can run your emacs -nw within a Cygwin Mintty. Then you can use the mouse to yank/paste from/in Emacs. In default configuration simply use your mouse together with Shift key (<S-mouse-1> to yank, <S-mouse-2> button to paste).
You can change the modifier key via Mintty "Options/Mouse/Modifier for overriding default".

Stefan
  • 26,154
  • 3
  • 46
  • 84
lgnom
  • 21
  • 1
2

Hope the answer is still relevant.

If you are running Emacs in GNU OS, you can paste the contents of the clipboard (if properly configured; more information: (info "Clipboard")) in a term mode buffer (e.g. "*terminal*" buffer) in Emacs if you press <S-insert> (Shift key + Insert key) which is bound to the function "term-paste" and you can copy to Emacs' kill ring by pressing <C-insert> (Control key + Insert key) which is bound to "kill-ring-save".

What follows is an elisp snippet that you can evaluate in the "*scratch*" buffer (given that it is in "Lisp Interaction mode") and you have a term mode buffer already running (that has the name "*terminal*"), by placing point at the end of the code block and pressing C-j (Control key + "j" key).

(with-current-buffer (get-buffer "*terminal*")
  (print (describe-key-briefly (kbd "<C-insert>")))
  (describe-key-briefly (kbd "<S-insert>")))
  
;; This is the output if I run it in my Emacs.
"<C-insert> runs the command kill-ring-save"
"<S-insert> runs the command term-paste"
0

If you're using spacemacs you can use clipboard :) https://develop.spacemacs.org/layers/+tools/xclipboard/README.html

It worked for me

laertida
  • 1
  • 1