Is there a variable containing all tokens that matched a regex search term?

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there a variable containing all tokens that matched a regex search term?

txporter
If I enter a regex search term, all occurrences of the matched items
in a window will be highlighted.

Is there a way to create a new window and populate it with only the
found items?

OR is there a variable that contains all items matching the regex
search term?

Example:

A colleague is working on a some large files where we use substitution
variables than start with '&&'.  Assume these are terminated by white
space, so are 'words' to vim.

He would like to extract just the tokens that start with '&&' and
populate a new window with those, so he can pull the tokens from two
separate files and see if all that occur in one occur in the other.

I was stumped at how to get this done.

Thanks!

Tom P.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a variable containing all tokens that matched a regex search term?

Tim Chase
On 04/09/2010 04:25 PM, tomPorter wrote:
> Is there a way to create a new window and populate it with only the
> found items?

not readily (no single-step solution), but it can be hacked.
Start by cloning the document into a file you can rip up...either:

   :%y      " yank the whole document
   :new     " create a new blank window
   :0p      " dump in the yanked content

or
   :new
   :0r original.txt

or just use your OS to copy the file so you don't mung the original.

Then slice away the stuff you don't care about

   :%s/&&\w\+/\r&/g
            " put each item of interest on its own line
   :v/^&&\w\+/d
            " delete all the lines that don't start with "&&"
   :sort u  " sort the resulting lines and make unique

> A colleague is working on a some large files where we use
> substitution variables than start with '&&'. Assume these are
> terminated by white space, so are 'words' to vim.

I'm assuming, based on your example description that items of
interest can be found with the regexp "&&\w\+", or if they have
more complex punctuation in them, you could use "&&\S*"

> OR is there a variable that contains all items matching the regex
> search term?

no

> He would like to extract just the tokens that start with '&&' and
> populate a new window with those, so he can pull the tokens from two
> separate files and see if all that occur in one occur in the other.

Once you have this list of uniquely sorted tokens for file A and
for file B, you can use vimdiff on them to compare the
presence/absence between the two.  I use this pattern frequently
for comparing selected attributes of pairs of files (such as your
items-of-interest, or columns of tab/comma-delimited files, etc).
  The basic pattern is

   clone file A to A'
   remove the stuff from A' that you don't care about
   clone file B to B'
   remove the stuff from B' that you don't care about
   use vimdiff to compare A' and B'

-tim



--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a variable containing all tokens that matched a regex search term?

Antony Scriven-3
In reply to this post by txporter
On 9 April 2010 22:25, tomPorter wrote:

 > If I enter a regex search term, all occurrences of the
 > matched items in a window will be highlighted.
 >
 > Is there a way to create a new window and populate it
 > with only the found items?
 >
 > OR is there a variable that contains all items matching
 > the regex search term?
 >
 > Example:
 >
 > A colleague is working on a some large files where we use
 > substitution variables than start with '&&'.  Assume
 > these are terminated by white space, so are 'words' to
 > vim.
 >
 > [...]

For a quick a quick hack, try this.

   :let tokens=[]
   :%s/&&\S\+/\=add(tokens,submatch(0))/g
   :undo
   :new
   :put=tokens

--Antony

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a variable containing all tokens that matched a regex search term?

Tim Chase
On 04/09/2010 04:47 PM, Antony Scriven wrote:
> For a quick a quick hack, try this.
>
>     :let tokens=[]
>     :%s/&&\S\+/\=add(tokens,submatch(0))/g
>     :undo
>     :new
>     :put=tokens

I like it :)

As a quick abuse of add(), the :undo can be obviated by making it

   :let tokens=[]
   :%s/&&\S\+./\=add(tokens,submatch(0))[-1]/g
   :new
   :put=tokens

which returns the last item added, and thus the found-token gets
replaced by itself.

-tim




--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a variable containing all tokens that matched a regex search term?

Tony Mechelynck
In reply to this post by txporter
On 09/04/10 23:25, tomPorter wrote:

> If I enter a regex search term, all occurrences of the matched items
> in a window will be highlighted.
>
> Is there a way to create a new window and populate it with only the
> found items?
>
> OR is there a variable that contains all items matching the regex
> search term?
>
> Example:
>
> A colleague is working on a some large files where we use substitution
> variables than start with '&&'.  Assume these are terminated by white
> space, so are 'words' to vim.
>
> He would like to extract just the tokens that start with '&&' and
> populate a new window with those, so he can pull the tokens from two
> separate files and see if all that occur in one occur in the other.
>
> I was stumped at how to get this done.
>
> Thanks!
>
> Tom P.
>

There is no preexisting function or keybinding for that.

You can construct one, see
        :help :function
        :help search()
        :help quote/

Best regards,
Tony.
--
Cabbage, n.:
        A familiar kitchen-garden vegetable about as large and wise as
a man's head.
                -- Ambrose Bierce, "The Devil's Dictionary"

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a variable containing all tokens that matched a regex search term?

Adam-281
In reply to this post by txporter
On Fri, Apr 9, 2010 at 16:25, tomPorter <[hidden email]> wrote:
OR is there a variable that contains all items matching the regex
search term

I found this function (CopyMatches) online a while ago from here: http://vim.wikia.com/wiki/Copy_the_search_results_into_clipboard.  Just follow the instructions and you should be good to go.  Don't forget to clear the register first!  (q {register letter} q);  So if you're going to copy them into register 'a' this is what I do (*<CR> denotes press enter):

/&&\w*<CR>
qaq
:%CopyMatches a<CR>
{Navigate to wherever you want the text}
"ap


Since I use this a lot I have this shortcut defined in my vimrc:
:noremap ,a  qaq:%CopyMatches a<CR>

So once I have my search down I just go ',a' and then everything is in buffer a :D

~Adam~

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

RE: Is there a variable containing all tokens that matched a regex search term?

JohnBeckett
Adam wrote:
> I found this function (CopyMatches) online a while ago from
> here:
> http://vim.wikia.com/wiki/Copy_the_search_results_into_clipboard

You reminded me that I have enhanced that script to behave more
usefully. It now defaults to copying from the whole file, and
copying to the clipboard, and the register does not need to be
cleared first.

I have just uploaded the new version.

John

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a variable containing all tokens that matched a regex search term?

Christian Brabandt
In reply to this post by txporter
Hi tomPorter!

On Fr, 09 Apr 2010, tomPorter wrote:
> Is there a way to create a new window and populate it with only the
> found items?

I would do it like this, which means
:redir => a              " redirect output into variable a
:g/&&/                   " print all lines matching the pattern &&
:redir end               " end redirection
:vert new                " open a new window
:put =a                  " put the result into that window


regards,
Christian

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a variable containing all tokens that matched a regex search term?

Antony Scriven-3
On 10 April 2010 12:29, Christian Brabandt wrote:

 > Hi tomPorter!
 >
 > On Fr, 09 Apr 2010, tomPorter wrote:
 > > Is there a way to create a new window and populate it
 > > with only the found items?

You snipped the rest of the specification.

 > I would do it like this, which means
 > :redir => a              " redirect output into variable a
 > :g/&&/                   " print all lines matching the pattern &&
 > :redir end               " end redirection
 > :vert new                " open a new window
 > :put =a                  " put the result into that window

That's not what the OP asked for. --Antony

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a variable containing all tokens that matched a regex search term?

Christian Brabandt
Hi Antony!

On Sa, 10 Apr 2010, Antony Scriven wrote:

> That's not what the OP asked for. --Antony

Sorry, I misunderstood.

regards,
Christian

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

To unsubscribe, reply using "remove me" as the subject.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a variable containing all tokens that matched a regex search term?

Adam-281
In reply to this post by JohnBeckett
On Sat, Apr 10, 2010 at 05:14, John Beckett <[hidden email]> wrote:
Adam wrote:
> I found this function (CopyMatches) online a while ago from
> here:
> http://vim.wikia.com/wiki/Copy_the_search_results_into_clipboard

You reminded me that I have enhanced that script to behave more
usefully. It now defaults to copying from the whole file, and
copying to the clipboard, and the register does not need to be
cleared first.

I have just uploaded the new version.

John

Awesome that works a lot better, thanks much!

~Adam~

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php