LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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, 11: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, 12:36 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Let me Google that for you
 
Old 06-30-2011, 01: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, 05: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, 06:51 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
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, 06: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, 01: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, 12:33 PM   #8
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 730

Rep: Reputation: 75
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, 04:52 PM   #9
littlejoe5
Member
 
Registered: Aug 2006
Location: Arizona
Distribution: Various Debian derivitives
Posts: 243

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 04:56 PM.
 
Old 07-07-2011, 07: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, 09:50 PM   #11
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 730

Rep: Reputation: 75
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, 06: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, 03:36 PM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 03:23 AM
Vim command to cut the current line and append to the clipboard action_owl Linux - Software 1 04-08-2011 06:58 PM
vim: How to configure to use the mouse, AND to copy and paste alex170872 Linux - Software 2 09-30-2010 03:35 AM
I don't know what the vim use the clipboard with ? haochao Linux - Desktop 4 03-14-2009 10:24 PM
xfce vim and clipboard Panci Slackware 3 02-06-2009 04:22 AM

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

All times are GMT -5. The time now is 02:40 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration