Friday, April 9, 2010

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

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.

No comments: