5

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
9

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.

| improve this answer | |
  • 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
  • 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 4 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 5 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.

| improve this answer | |
0

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".

| improve this answer | |
0

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"
| improve this answer | |

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.