Searching word under cursor in second window

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

Searching word under cursor in second window

Beat Burgener | NetSuccess GmbH
Hi all

Just a short question...

I tried to accomplish the following using the ":map key commands" method:

Currently displaying document "A" in a single window (window 1), the cursor
is on word "myTerm". By pressing a key combination (like Ctrl+X), a
predefined/configurable *) document (document B) should open in a second
window (window 2) underneath window 1.

The word under the cursor of window 1 (in this case "myTerm") should
then be
search in document B using RegEx so that only matches at the beginning of a
line are found (RegEx: "^myTerm").

Now, window B should get the focus, e.g. the cursor should be there ...

I was not sucessful, since I couldn't find a way to remember the word in
window A and then search this word using RegEx in window B.

*) This can also be hardcoded in a script or in the map command ...

Any ideas or suggestions?

Also any hints highly welcome!

Many thanks

Beat



Reply | Threaded
Open this post in threaded view
|

RE: Searching word under cursor in second window

Gareth Oakes
Hi Beat,

What you're suggesting sounds very similar to the "tags" system in vim.

Have a look under help topic ":help tags" and see if that does what you
need.  The only caveat being you may need to write your own ctags filter
thing to pickup the correct syntax in "B"-type documents.

Cheers,
Gareth

> -----Original Message-----
> From: Beat Burgener [mailto:[hidden email]]
> Sent: Wednesday, 17 August 2005 10:55 AM
> To: [hidden email]
> Subject: Searching word under cursor in second window
>
> Hi all
>
> Just a short question...
>
> I tried to accomplish the following using the ":map key
> commands" method:
>
> Currently displaying document "A" in a single window (window
> 1), the cursor is on word "myTerm". By pressing a key
> combination (like Ctrl+X), a predefined/configurable *)
> document (document B) should open in a second window (window
> 2) underneath window 1.
>
> The word under the cursor of window 1 (in this case "myTerm")
> should then be search in document B using RegEx so that only
> matches at the beginning of a line are found (RegEx: "^myTerm").
>
> Now, window B should get the focus, e.g. the cursor should be
> there ...
>
> I was not sucessful, since I couldn't find a way to remember
> the word in window A and then search this word using RegEx in
> window B.
>
> *) This can also be hardcoded in a script or in the map command ...
>
> Any ideas or suggestions?
>
> Also any hints highly welcome!
>
> Many thanks
>
> Beat
>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Searching word under cursor in second window

Tim Chase-2
In reply to this post by Beat Burgener | NetSuccess GmbH
> The word under the cursor of window 1 (in this case "myTerm")
> should then be search in document B using RegEx so that only
> matches at the beginning of a line are found (RegEx:
> "^myTerm").
>
> Now, window B should get the focus, e.g. the cursor should be
> there ...

The following mapping may do the trick:

map <f2> :let temp=expand('<cword>') <bar> sp foo.txt <bar>call
search(temp)<cr>

(all one line)  Upon hitting <f2> it saves the current word in
the variable as "temp" (if you want the current "Word" rather
than the current "word", use "cWORD" instead of "cword").  It
then splits the file you want (hard coded) and searches from the
beginning for the word in question.

It does leave the "temp" variable hanging around, but that should
be pretty harmless.  Additionally, you may want to wrap the
search() call's parameters with something like

        search('\<'.temp.'\>')

to search for whole words only (otherwise, searching for "an"
could return the word "human" if it appears in foo.txt before the
word "an" does).

Another slightly shorter alternative might be

map <f2> :exec ('sp +/\<'.expand('<cword>').'\>/ foo.txt')<cr>

but this just goes to the given line, not directly to the search
match.

Just a couple ideas to give you some ground from which to work.

For more info, check out

        :help search()
        :he expand()
        :he exec
        :he cword
        :he :sp

-tim