3

Is there a shortcut key to copy a line from vi editor and then paste it into a terminal. Currently I select the text with mouse and then press crtl+shift+c (I'm using default settings of KDE) and then do ctrl+shift+v on the konsole. The problem with this option is when the line is long enough to wrap. In that case copy-paste inserts extra spaces which I have to fix after pasting on terminal.

This is very annoying specially when the line copied is very long and contains file names (typically commands used to invoke compiler).

A. K.
  • 34,395
  • 15
  • 52
  • 89
  • Maybe this is what you are looking for: [how to copy codes in vi to clipboard](http://stackoverflow.com/questions/5532878/how-to-copy-codes-in-vi-to-clipboard) – Robert Gomez May 09 '13 at 03:50

2 Answers2

5

You can yank to the X clipboard by putting the contents in the * register.

To do this use "*yy on the line you want to yank. Then outside of vim you should be able to paste it.

FDinoff
  • 30,689
  • 5
  • 75
  • 96
  • So I have to press " and then * and then yy? I tried but not working for me. – A. K. May 09 '13 at 03:53
  • when you do `vim --version | grep clipboard` do you see a `+clipboard` or a `-clipboard` – FDinoff May 09 '13 at 03:55
  • You might also need `+X11` and/or `+xterm_clipboard`. See: http://stackoverflow.com/a/10104125/955926 – Gary Fixler May 09 '13 at 03:57
  • Yes I think; 6:-clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments 19:-X11 -xfontset -xim -xsmp -xterm_clipboard -xterm_save – A. K. May 09 '13 at 03:59
  • You might need to get compile a different version of vim. You need to have `+clipboard` for this to work. and potentially the other one Gary Fixler mentioned. – FDinoff May 09 '13 at 04:00
  • Oh, I see. I'll try that. The vim I have is the default one. – A. K. May 09 '13 at 04:01
  • Try using apt-get with either vim-gtk or vim-gnome packages I believe these should have all the options enabled. – FDinoff May 09 '13 at 04:03
0

If you don't have access to a clipboard via X or screen or tmux, you can write the contents of the buffer to a temp file, then go to the terminal and invoke $(cat temp-file) or open an editor for the command line and read in the file. Whatever shell you are using probably provides a mechanism for opening an editor on the command line. In bash with vi-style readline keybindings you can type v to get a vi session. If you shell does not provide that functionality, try a different shell.

William Pursell
  • 204,365
  • 48
  • 270
  • 300