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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-30-2011, 10:52 AM
|
#1
|
LQ Newbie
Registered: Nov 2009
Posts: 20
Rep: 
|
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
|
|
|
06-30-2011, 11:36 AM
|
#2
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
|
|
|
06-30-2011, 12:20 PM
|
#3
|
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979
Rep: 
|
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
|
|
|
06-30-2011, 04:36 PM
|
#4
|
LQ Newbie
Registered: Nov 2009
Posts: 20
Original Poster
Rep: 
|
I'm using whatever cscope uses because I'm doing this within cscope. I think it is vim, how can I tell?
|
|
|
06-30-2011, 05:51 PM
|
#5
|
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
|
Quote:
Originally Posted by eddyq
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"?
|
|
|
06-30-2011, 05:54 PM
|
#6
|
LQ Newbie
Registered: Nov 2009
Posts: 20
Original Poster
Rep: 
|
It is command line so it must be vim.
Information on cscope can be found here: http://cscope.sourceforge.net/
Eddy
|
|
|
07-01-2011, 12:05 AM
|
#7
|
Senior Member
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979
Rep: 
|
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
|
|
|
07-02-2011, 11:33 AM
|
#8
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735
Rep:
|
Hi.
Quote:
Originally Posted by eddyq
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:
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:
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 was in the Debian repository. You'll need to check yours.
Best wishes ... cheers, makyo
|
|
|
07-02-2011, 03:52 PM
|
#9
|
Member
Registered: Aug 2006
Location: Arizona
Distribution: ubuntu dirivatives mostly Mate
Posts: 260
Rep:
|
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.
|
|
|
07-07-2011, 06:25 PM
|
#10
|
LQ Newbie
Registered: Nov 2009
Posts: 20
Original Poster
Rep: 
|
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.
|
|
|
07-07-2011, 08:50 PM
|
#11
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735
Rep:
|
Hi.
Quote:
Originally Posted by eddyq
.. 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
|
|
|
06-11-2012, 05:51 AM
|
#12
|
LQ Newbie
Registered: Jun 2012
Posts: 1
Rep: 
|
lmgtfy recursion
Quote:
Originally Posted by David the H.
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 
|
|
|
06-11-2012, 02:36 PM
|
#13
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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. 
|
|
|
All times are GMT -5. The time now is 01:54 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|