hhh
hhh

Reputation: 52810

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+

Upvotes: 8

Views: 6046

Answers (4)

kev
kev

Reputation: 161604

append all search result to reg X(uppercase) separated by newline:

:g/^/call setreg('X', matchstr(getline('.'), '^\S\+') . "\n")

to clear reg x:

qxq

Upvotes: 1

1983
1983

Reputation: 5963

Try the following. First define a function for saving your matched regexps into a list (:help List).

:fun Add(list, item)
:   call add(a:list, a:item)
:   return a:item
:endfun

Note that it also returns the added item. This isn't necessary, but what you return here becomes the replacement text in the :substitute that follows below.

Next, create an empty list.

:let list=[]

Then run your regexp.

:%s/^\S\+/\=Add(list, submatch(0))/

Note that the replacement begins with \= (see :help sub-replace-special). The result is that we replace the match expression with itself, but at the same time capture the result in list.

The submatch() function returns matched parts of the :substitute command. submatch(0) returns the entire match.

Note that as a side effect the buffer is marked as having been changed. If you saved the buffer before you executed the substitution then you can fix this by simply hitting u for undo, or appending '| u' to your command. (If you are going to undo the operation then it doesn't matter what we return from our Add function.)

You can view your matches with

:echo list

or you can append them to your buffer with

:$put=list

I hope this helps. It's straightforward once you know how, but there's quite a lot to digest there.

Oh, I nearly forgot! You can add your matches to the clipboard with

:let @+ = join(list, '^@')

Then your matches will be in the clipboard, each on a separate line.

^@ represents a NUL character, and is generated with <C-V><C-J>, meaning press control-V, followed by control-J. (Why you have to use ^@ here is a whole topic in itself.)

Upvotes: 1

sehe
sehe

Reputation: 392833

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

Upvotes: 2

mathematical.coffee
mathematical.coffee

Reputation: 56905

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.

Upvotes: 7

Related Questions