2

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 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 6 '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? and Should 'Hi', 'thanks', taglines, and salutations be removed from posts? – Dan Jul 6 '16 at 19:09
  • OK. Fine, I see your point. – Andrea Borga Jul 6 '16 at 21:15
5

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 6 '16 at 18:12
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.