[SOLVED] Copy to clipboard from within vi / vim without mouse
Linux - NewbieThis 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.
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.
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.
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.
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.
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.
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.
.. 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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.