Tutorial :How to copy and paste from terminal with keyboard? [closed]


I'm trying to move away from using the mouse as much as I can (just a pet project). I know that in the terminal you can do c-k to cut a line and c-u to paste that line back into that same terminal, but I'm looking for something where I can copy a line in the terminal into gnomes clipboard so I can paste it into say a browser or somesuch.

I know that if I use the mouse to highlight text, I can then hit shift-insert to paste that text, which is great, but now I just want to be able to do it without the mouse. Something like c-space highlighting in emacs (but even that doesn't copy into the gnome clipboard).


Some more clarification, I have several different terminals open, and on many I've ssh'ed into various remote machines where utilities like xclip/xsel aren't as helpful.


Stated here, the default commands for GNOME copy and paste are CTRL-SHIFT-C and CTRL-SHIFT-V.

Edit: I found a useful blog post that allows you to enable terminal copy/paste to CTRL-C and CTRL-V in GNOME.


Shift + Insert to paste. I think is what you need, but I do not know how to copy from the terminal.


For interaction with the X11 clipboard from the command line, see this question.

Maybe a side track to what you ask for, but for terminal window interaction I can recommend screen, where you use keys to copy and paste (from a searchable scrollback buffer!). The copy and paste operation only is between screen controlled windows though like you describe with the terminal window and c-u & c-k, but you can easily dump to /tmp/screen-xchange and then use xclip to copy to the X11 clipboard.


It's easy! Just run two commands and you will be able to copy and paste with Ctrl+C Ctrl+V!

gconftool-2 -t str -s /apps/gnome-terminal/keybindings/copy "<Control>c"  gconftool-2 -t str -s /apps/gnome-terminal/keybindings/paste "<Control>v"  

Happy copy-pasting!


I tried what hlovdal suggested and used GNU screen and xclip. The result was a simple script:

cat /tmp/screen-exchange | xclip

that I configured to C-a e in .screenrc:

bind e exec !!! $HOME/bin/screen-exchange-to-primary

then I simply enter copy/scrollback mode in screen with C-a [ mark/select with > and copy to primary selection with C-a e. Perhaps not the smoothest sequence of combinations, but it's the best I've got so far.


You can run the shell inside Emacs, then you can select and copy to the clipboard as you would usually do that in Emacs.

As for "running the shell inside Emacs", there are several options: M-x shell, M-x eshell, and various term/eterm/ansi-term-based stuff (modes) (these "*term" must be better for curses pseudographics etc.)

As for me personally, trying to keep away from the mouse, I try to use eshell primarily (I don't need curses apps usually), emacs-w3m for web-browsing, just Emacs mostly for everything else, and ratpoison as the window manager.


I know if your shell program is bash or tcsh, it uses readline library to do line editing, so the Control-K function is handled by readline and is stored in a "kill-ring" buffer inside your shell program. It is impossible to steal the contents of the kill-ring from your shell (unless your shell has specific functionality programmed into it that allow it), however you can re-configure the Control-K to perform some other action by configuring the .inputrc file in your home directory. So you could configure it to perform a macro, for example, outputting the text "xclip -in\n" and then dumping the contents of the kill-ring to the output along with a "Control-D" action to terminate the text. This is mostly all explained in the readline manpage.

I've never tried anything like it before so I don't know if that would work or not, but that's the route I would try first.


For long time I was also trying to sort out it, using vt100.translations.

Taken from http://www.fifi.org/doc/xterm/xterm.faq.html#future_work:

"XTerm*vt100.translations:     #override \n\      <Key>Insert:            string(\001) \n\      Shift <Key>Up:          scroll-back(1,lines) \n\      Shift <Key>Down:        scroll-forw(1,lines) \n\      Shift <Key>Right:       string(0x1b) string("f") \n\      Shift <Key>Left:        string(0x1b) string("b") \n\      Shift <Key>Delete:      string(0x1b) string(0x08) \n\      Shift <Key>Tab:         string(0x1b) string("*") \n\      <Key>0x1000FF0D:        scroll-back(1,page) \n\      <Key>0x1000FF0E:        scroll-forw(1,page) \n\      <Key>0x1000FF09:        string(\010) \n\      <Key>0x1000FF0A:        string(\005) \n\      <Key>BackSpace:         string(0xff) \n\      <Key>Select:            select-start() \n\              <Btn1Motion>:           select-extend() \n\      <Key>0x1000FF02:        select-end(PRIMARY,CUT_BUFFER0) \n\      Meta <Key>0x1000FF02:   select-end(CLIPBOARD) \n\      <Key>0x1000FF04:        insert-selection(PRIMARY,CUT_BUFFER0) \n\      Meta <Key>0x1000FF04:   insert-selection(CLIPBOARD) \n\      <Key>F1:                string(0x1b) string("OP")  

no success

OK if we have set some key for select-start select-end ..extend but how to bring cursor their.

gnu screen may be solution but, it will not properly in zsh, and we can not bring cut text in Xclipboard.


Might I suggest RatPoison as your window manager? As the name suggests, you don't need a mouse at all. I'm not sure how it handles the clipboard though, as I've only ever used it for fun. It may depend on what terminal program you're using.


This is a super-clunky hack, but it works for copying the last line of output from the last run command in a terminal using only the keyboard (and a python script), and it can work over ssh.

Here's how you use it.

  1. In gnome-terminal (or the like), Alt+E, A. This is to Select All.
  2. If you're in ssh, open a different terminal on your local machine.
  3. Run the attached script, which i call lastline on my machine.

Then you've got one line of text with no newline on your gtk clipboard. The script works by using the x selection clipboard from selecting all the terminal's text and putting the third-to-last line of it on the gtk clipboard. This script can be modified to select different parts of the x selection, which would correspond to different parts of the terminal's text buffer. However, it's still a long ways from an interactive cursor :(

It'd be great if gnome-terminal integrated keyboard-driven selection support.

This example puts "/usr/bin/gcc" on the gtk clipboard (remember to Select All before running lastline):

$ which gcc  /usr/bin/gcc  $ lastline  

Here's the script i call lastline:

#!/usr/bin/env python    import sys, subprocess  import optparse  __version__ = "0.1"    def main():      try:          process = subprocess.Popen(["xsel"], stdout=subprocess.PIPE)      except OSError:          sys.exit("you must install xsel")      (stdout, _) = process.communicate()        lines = stdout.split("\n")      if len(lines) < 3:          text = ""      else:          text = lines[-3]        import gtk      clipboard = gtk.clipboard_get()      clipboard.set_text(text)      clipboard.store()    if __name__ == "__main__":      parser = optparse.OptionParser(version=__version__)      (options, args) = parser.parse_args()      main()  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »