LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-30-2011, 10:52 AM   #1
eddyq
LQ Newbie
 
Registered: Nov 2009
Posts: 20

Rep: Reputation: Disabled
Copy to clipboard from within vi / vim without mouse


I want to copy a highlighted field to the clipboard (not the copy/paste within vim but the clipboard that can be pasted outside of vim). I can do this by using the mouse but using the visual command (v) and then the yank command (y) does not do that.

Is there a way to do this without the mouse?

Eddy
 
Old 06-30-2011, 11:36 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Let me Google that for you
 
Old 06-30-2011, 12:20 PM   #3
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Do you use gvim or vim in a terminal?

If you use a vim in a terminal the answer depends on the configuration of the terminal.

Markus
 
Old 06-30-2011, 04:36 PM   #4
eddyq
LQ Newbie
 
Registered: Nov 2009
Posts: 20

Original Poster
Rep: Reputation: Disabled
I'm using whatever cscope uses because I'm doing this within cscope. I think it is vim, how can I tell?
 
Old 06-30-2011, 05:51 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by eddyq View Post
I'm using whatever cscope uses because I'm doing this within cscope. I think it is vim, how can I tell?
You're using vim if it's in a command line, and gvim if it's in a GUI.

And what's "cscope"?
 
Old 06-30-2011, 05:54 PM   #6
eddyq
LQ Newbie
 
Registered: Nov 2009
Posts: 20

Original Poster
Rep: Reputation: Disabled
It is command line so it must be vim.

Information on cscope can be found here: http://cscope.sourceforge.net/

Eddy
 
Old 07-01-2011, 12:05 AM   #7
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hello,

I can't find the information yet. But with vim in commandline you'll have to configure the terminal you use in order to copy text from a file opened in vim.
For example I'm using Konsole, the terminal of KDE and I cannot copy text out of vim. If I want to achieve this, I leave vim, cat the whole file and then I can copy it (and paste into another window).

It may help to try another terminal, for example xterm instead of konsole.

If one wants to paste text into vim from the clipboard (not the p command!) in a terminal one has to use <CTRL><SHIFT><V> and additionally be sure that vim is in insert mode. If vim isn't in insert mode, the text will be pasted from the first character which sets vim into insert mode.

Markus
 
Old 07-02-2011, 11:33 AM   #8
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.
Quote:
Originally Posted by eddyq View Post
I want to copy a highlighted field to the clipboard (not the copy/paste within vim but the clipboard that can be pasted outside of vim). I can do this by using the mouse but using the visual command (v) and then the yank command (y) does not do that.

Is there a way to do this without the mouse?

Eddy
Vi(m) allows accepts a "last line" (":") command:
Code:
:first_line,last_linew! !external_command
which will copy the range for the STDIN of the external command.
A sort, for example, is often useful.

The command xclip will read and write the system clipboard, so
putting these idea together suggest we use something like:
Code:
:first,lastw! !xclip -i
For example for file data1:
Code:
Now is the time
for all good men
to come to the aid
of their country.
The sequence:
Code:
:2,3! !xclip -i
Will result in lines:
Code:
for all good men
to come to the aid
being copied to the clipboard.

The action can be verified with the command:
Code:
xclip -o
which will copy the contents of the clipboard to STDOUT.

The clipboard may be cleared with:
Code:
xclip -i </dev/null
On my system, the versions of interest are:
Code:
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
VIM - Vi IMproved 7.1 (2007 May 12, compiled Oct 18 2008 09:05:15)
xclip version 0.08
The utility
Code:
xclip
was in the Debian repository. You'll need to check yours.

Best wishes ... cheers, makyo
 
Old 07-02-2011, 03:52 PM   #9
littlejoe5
Member
 
Registered: Aug 2006
Location: Arizona
Distribution: ubuntu dirivatives mostly Mate
Posts: 260

Rep: Reputation: 19
The simple way: In the distros that I have used:

Perhaps you are familiar with "Ctrl-c" to copy to the clip board, and "Ctrl-v" to paste what has been copied.

In a terminal (even when using vm, or whatever) use "Ctrl/Shift- c" to copy to the clipboard. and "Ctrl/Shift - v"

for example: to copy this line from where you are reading it in your internet browser to vim within a terminal window on your computer: Highlight the line with your mouse, in your browser, and press the control key, and with that key pressed, press also the c key. That should copy the line to the standard clip-board.

Then go to your terminal window, and this time press both the control key and the shift key. with both keys pressed, press also the v key, and you will have copied the text into your terminal (vim if you are using vim).

In a terminal always use Ctrl/shift (and then c or v) but in a gui, use Ctrl- (and then c or v)

Last edited by littlejoe5; 07-02-2011 at 03:56 PM.
 
Old 07-07-2011, 06:25 PM   #10
eddyq
LQ Newbie
 
Registered: Nov 2009
Posts: 20

Original Poster
Rep: Reputation: Disabled
I've been using the ctrl/c method but there are two problems:
(1) if you have line numbers displayed then swiping the mouse over several lines will copy the line numbers too.
(2) if the part to copy is longer than what is visible on the screen then you would have to do it in several parts.

Regarding the 2,3 method: that would be a bit painful but since I always display line numbers then maybe that is the way.

I was hoping to find something similar to what Windows has, where you can use the keyboard with shift/arrow to highlight text then use ctrl/c to copy it ... but I guess vi just doesn't have that feature.
 
Old 07-07-2011, 08:50 PM   #11
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.
Quote:
Originally Posted by eddyq View Post
.. Regarding the 2,3 method: that would be a bit painful but since I always display line numbers then maybe that is the way ...
Lines may may "addressed" in many more ways than just a line number -- that was a convenient example.

Code:
Specifier Description

number
	an absolute line number
.
	the current line
$
	the last line in the file
%
	the whole file. The same as 1,$
't
	position of mark "t"
/pattern[/]
	the next line where text "pattern" matches.
?pattern[?]
	the previous line where text "pattern" matches
\/
	the next line where the previously used search pattern matches
\?
	the previous line where the previously used search pattern matches
\&
	the next line where the previously used substitute pattern matches 

See http://vimregex.com/ , part 3.2 Range of Operation, Line Addressing and Marks for examples
using some of these methods, and there are many other similar pages found with Google.
Best wishes ... cheers, makyo
 
Old 06-11-2012, 05:51 AM   #12
nathan.f77
LQ Newbie
 
Registered: Jun 2012
Posts: 1

Rep: Reputation: Disabled
Talking lmgtfy recursion

Quote:
Originally Posted by David the H. View Post
lmgtfy.com/?q=vim+copy+to+clipboard - Let me Google that for you
Hey David the H., I thought you might like to know that your lmgtfy link is now right at the top of one of the top-ranked pages for the Google search "vim copy to clipboard".

Just thought there was some ironic recursion there
 
Old 06-11-2012, 02:36 PM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Now that's funny. Although I actually get a different thread as my first hit:

http://www.linuxquestions.org/questi...pboard-669721/

Which just goes to show, do a forum search first too.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to copy and paste things between vim and system clipboard grissiom Slackware 43 09-30-2013 02:23 AM
Vim command to cut the current line and append to the clipboard action_owl Linux - Software 1 04-08-2011 05:58 PM
vim: How to configure to use the mouse, AND to copy and paste alex170872 Linux - Software 2 09-30-2010 02:35 AM
I don't know what the vim use the clipboard with ? haochao Linux - Desktop 4 03-14-2009 09:24 PM
xfce vim and clipboard Panci Slackware 3 02-06-2009 03:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration