Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: yank Regex match to +clipboard

Example :%y+ yanks to external +clipboard. Now I want results by regexes such as ^\S\+ to the external +clipboard. How?

Trial 1. [Fail] :g@^\S\+@y+

Trial 2. [Fail] :^\S\+y+

like image 986
hhh Avatar asked Dec 13 '11 23:12

hhh


2 Answers

Have a look here http://vim.wikia.com/wiki/Copy_the_search_results_into_clipboard, the section 'Copying lines containing search hits'.

This worked for me (clear register a, yank pattern results into it, copy over into clipboard):

qaq
:g/pattern/y A
:let @+ = @a

When I tried :g/pattern/y+ it only copied the first line.

That section on the link also gives a function you can write to copy all lines, but it still looks like a 2-step process (do /pattern and then call the function): but you could probably write your own macro/shortcut to do both in sequence.

like image 122
mathematical.coffee Avatar answered Sep 19 '22 22:09

mathematical.coffee


Perhaps you can find some inspiration in the following:

:saveas %.tmp

to start working on a copy

:v//d
:exec '%s/^.\{-\}\(' . @/ . '\).*$/\1/g'

So for example doing a search /word\d on the following text:

Tincidunt. Proin sagittis2. Curabitur auctor metus non mauris. Nunc condimentum nisl non augue. Donec leo urna, dignissim vitae, porttitor ut, iaculis sit amet, sem.

Class aptent taciti sociosqu3 ad litora torquent per conubia nostra, per inceptos himenaeos. Suspendisse potenti. Quisque augue metus, hendrerit sit amet, commodo vel, scelerisque ut, ante. Praesent euismod euismod risus. Mauris ut metus sit amet mi cursus commodo. Morbi congue mauris ac sapien. Donec justo. Sed congue nunc vel mauris0. Pellentesque vehicula orci id libero. In hac habitasse platea dictumst. Nulla sollicitudin, purus1 id elementum dictum, dolor augue hendrerit ante, vel semper metus enim et dolor. Pellentesque molestie9.

will highlight the words as shown. Doing

:v//d|exec '%s/^.\{-\}\(' . @/ . '\).*$/\1/g'

results in the following buffer:

sagittis2
sociosqu3
mauris0
purus1
molestie9
like image 24
sehe Avatar answered Sep 19 '22 22:09

sehe