How to open another file and go to a search pattern

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to open another file and go to a search pattern

Mayuresh
I have a requirement where the cursor is on following line:

SomeFileName : Some Multiple Words of Search Pattern

I want to define a hot key (Say <C-o>) to open SomeFileName and go to and
highlight the whole pattern "Some Multiple Words of Search Pattern"

Following is my attempt to solve it, which has not succeeded fully:

In vimrc:

function! GoToFile()
    let line = getline(".")
    let searchpat = split(line,":")[1]
    let file = split(line," ")[1]
    " See below what all I tried here
endfunction
map <C-o> :exec GoToFile()<CR>

Following things are tried at the end of the function:


1. Tried loading the file and searching in it using two different
statements:

    exec "e" file
    search(searchpat,"")

With this, I find that such search is applied to current file instead of
new file and then the file is loaded. I find this strange.


2. From above, assuming loading and searching as two statements won't
work, tried:

    exec "+/" searchpat file

But this breaks with searchpat having spaces (even if I use \" to cover
the search pattern).



3. To overcome above problem, I replaced all spaces in searchpat with ".".
Now this is closest to what I need. But now the pattern is not highlighted
automatically. I have to say "/<Up>" to get the search pattern again
interactively and then it jumps to and highlights the pattern.


Sorry about the length of mail. Just wanted to share whatever is already
tried to describe the problem.

Hope some solution is possible.

Mayuresh

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to open another file and go to a search pattern

Nikolay Aleksandrovich Pavlov
2017-01-27 5:46 GMT+03:00 Mayuresh <[hidden email]>:

> I have a requirement where the cursor is on following line:
>
> SomeFileName : Some Multiple Words of Search Pattern
>
> I want to define a hot key (Say <C-o>) to open SomeFileName and go to and
> highlight the whole pattern "Some Multiple Words of Search Pattern"
>
> Following is my attempt to solve it, which has not succeeded fully:
>
> In vimrc:
>
> function! GoToFile()
>     let line = getline(".")
>     let searchpat = split(line,":")[1]
>     let file = split(line," ")[1]
>     " See below what all I tried here
> endfunction
> map <C-o> :exec GoToFile()<CR>

Given how this function looks, you should not be using `:execute`. For
calling functions there is `:call`. This works only because

1. Without explicit `:return` statement function returns `0` at exit.
2. Vim can implicitly convert numbers to strings.
3. `:{range}` takes you to the last line in range if run without a command.
4. Line number 0 means first line, even though first line has number 1.

>
> Following things are tried at the end of the function:
>
>
> 1. Tried loading the file and searching in it using two different
> statements:
>
>     exec "e" file
>     search(searchpat,"")
>
> With this, I find that such search is applied to current file instead of
> new file and then the file is loaded. I find this strange.

How did you check that “search is applied to current file”? I see that
these commands are not going to work because there is no `:search`
command.

And you forgot `fnameescape()`. First line should be `:execute 'e'
fnameescape(file)` or you get problems with special characters in
`file`.

>
>
> 2. From above, assuming loading and searching as two statements won't
> work, tried:
>
>     exec "+/" searchpat file
>
> But this breaks with searchpat having spaces (even if I use \" to cover
> the search pattern).

?! This command is not opening a file, it will run something like

    +/{space}{search pattern}{space}{filename}

: perform search in the current file, starting from the current line
(same {range} without command interpretation).

>
>
>
> 3. To overcome above problem, I replaced all spaces in searchpat with ".".
> Now this is closest to what I need. But now the pattern is not highlighted
> automatically. I have to say "/<Up>" to get the search pattern again
> interactively and then it jumps to and highlights the pattern.
>
>
> Sorry about the length of mail. Just wanted to share whatever is already
> tried to describe the problem.
>
> Hope some solution is possible.
>
> Mayuresh
>
> --
> --
> 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
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to open another file and go to a search pattern

Mayuresh
Sorry, when typing out text I have made some mistakes, which may have
given wrong impressions of what I tried at a couple of places. I should
have rather pasted the code I tried, but I had changed it by the time I
thought of writing a mail. Will correct it now.

On Fri, Jan 27, 2017 at 02:13:24PM +0300, Nikolay Aleksandrovich Pavlov wrote:

> > function! GoToFile()
> >     let line = getline(".")
> >     let searchpat = split(line,":")[1]
> >     let file = split(line," ")[1]
> >     " See below what all I tried here
> > endfunction
> > map <C-o> :exec GoToFile()<CR>
>
> Given how this function looks, you should not be using `:execute`. For
> calling functions there is `:call`. This works only because
Point taken.

> > 1. Tried loading the file and searching in it using two different
> > statements:
> >
> >     exec "e" file
> >     search(searchpat,"")
> >
> > With this, I find that such search is applied to current file instead of
> > new file and then the file is loaded. I find this strange.
>
> How did you check that “search is applied to current file”? I see that
> these commands are not going to work because there is no `:search`
> command.

Clarification: Tried exec search(searchpat)
It did not search in the current file. (My mistake in above  mail.)

However the other thing that I tried was:
    exec "/"searchpat
or more precisely, to avoid an extra space after "/"
    exec join(["/",searchpat],"")

On either of these the message "Search hit bottom ...." appeared in the
current buffer itself. Only on hitting Enter the next file was opened.
From this I thought it is searching in current file.

> And you forgot `fnameescape()`. First line should be `:execute 'e'
> fnameescape(file)` or you get problems with special characters in
> `file`.
Thanks for the tip. Will incorporate.

> > 2. From above, assuming loading and searching as two statements won't
> > work, tried:
> >
> >     exec "+/" searchpat file
> >
> > But this breaks with searchpat having spaces (even if I use \" to cover
> > the search pattern).
>
> ?! This command is not opening a file, it will run something like
>
>     +/{space}{search pattern}{space}{filename}
>
> : perform search in the current file, starting from the current line
> (same {range} without command interpretation).

Precise command is:

    exec "e " join(["+/",searchpat],"") file

This is opening the file, but to highlight the pattern I have to press
"/<Up>"<CR>. Looking for this last step to happen automatically.

Mayuresh

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to open another file and go to a search pattern

Nikolay Aleksandrovich Pavlov
2017-01-27 15:20 GMT+03:00 Mayuresh <[hidden email]>:

> Sorry, when typing out text I have made some mistakes, which may have
> given wrong impressions of what I tried at a couple of places. I should
> have rather pasted the code I tried, but I had changed it by the time I
> thought of writing a mail. Will correct it now.
>
> On Fri, Jan 27, 2017 at 02:13:24PM +0300, Nikolay Aleksandrovich Pavlov wrote:
>> > function! GoToFile()
>> >     let line = getline(".")
>> >     let searchpat = split(line,":")[1]
>> >     let file = split(line," ")[1]
>> >     " See below what all I tried here
>> > endfunction
>> > map <C-o> :exec GoToFile()<CR>
>>
>> Given how this function looks, you should not be using `:execute`. For
>> calling functions there is `:call`. This works only because
> Point taken.
>
>> > 1. Tried loading the file and searching in it using two different
>> > statements:
>> >
>> >     exec "e" file
>> >     search(searchpat,"")
>> >
>> > With this, I find that such search is applied to current file instead of
>> > new file and then the file is loaded. I find this strange.
>>
>> How did you check that “search is applied to current file”? I see that
>> these commands are not going to work because there is no `:search`
>> command.
>
> Clarification: Tried exec search(searchpat)

That does not make sense, `search` already moves cursor. No need to
make `exec` move cursor afterwards (similar problem to the above
point).

> It did not search in the current file. (My mistake in above  mail.)
>
> However the other thing that I tried was:
>     exec "/"searchpat
> or more precisely, to avoid an extra space after "/"
>     exec join(["/",searchpat],"")

If you want to avoid spaces, do use concat.

    :execute arg1 arg2

is the same thing as

    :execute arg1." ".arg2

for convenience. You just need to have explicit concat:

    :execute arg1.arg2

does not add any spaces.

>
> On either of these the message "Search hit bottom ...." appeared in the
> current buffer itself. Only on hitting Enter the next file was opened.
> From this I thought it is searching in current file.
>
>> And you forgot `fnameescape()`. First line should be `:execute 'e'
>> fnameescape(file)` or you get problems with special characters in
>> `file`.
> Thanks for the tip. Will incorporate.
>
>> > 2. From above, assuming loading and searching as two statements won't
>> > work, tried:
>> >
>> >     exec "+/" searchpat file
>> >
>> > But this breaks with searchpat having spaces (even if I use \" to cover
>> > the search pattern).
>>
>> ?! This command is not opening a file, it will run something like
>>
>>     +/{space}{search pattern}{space}{filename}
>>
>> : perform search in the current file, starting from the current line
>> (same {range} without command interpretation).
>
> Precise command is:
>
>     exec "e " join(["+/",searchpat],"") file
>
> This is opening the file, but to highlight the pattern I have to press
> "/<Up>"<CR>. Looking for this last step to happen automatically.

I am unable to move to the search result even with this code. You are
using :execute on a function which returns 0, it *must* bring you to
the first line once function exits, so all your attempts to move to
the search pattern are useless. To move and highlight you need to use
something like

    execute 'edit' fnameescape(file)
    let @/ = searchpat
    normal! gg0n

. E.g. this works as I expect:

    vim -u gtf.vim -i NONE -N

    " gtf.vim:
    function GoToFile() abort
        let line = getline(".")
        let [file, searchpat] = matchlist(line, '\v\C^(.{-})\ \:\ (.*)$')[1:2]
        execute 'edit' fnameescape(file)
        let @/ = searchpat
        normal! gg0n
    endfunction
    nnoremap <C-o> :<C-u>call GoToFile()<CR>

    call writefile(['/tmp/gtf-test : abc def'], '/tmp/gtf-test-init')
    call writefile(['foo', 'abc def'], '/tmp/gtf-test')
    set hlsearch
    edit /tmp/gtf-test-init
    call feedkeys("gg\<C-o>", "t")

. Note:

0. `nnoremap`: you must not use `*map` without `nore` unless you know
what you are doing. This avoids possible issues with other mappings.
1. `:<C-u>`: check how function behaves when you press `10<C-o>` without this.
2. `call GoToFile()`, not `execute`.
3. Original `file` and `searchpat` definition will not work correctly
with `SomeFileName : Some Multiple Words of Search Pattern`. I assumed
that code is wrong, not example line.

>
> Mayuresh
>
> --
> --
> 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
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to open another file and go to a search pattern

Mayuresh
On Fri, Jan 27, 2017 at 10:58:25PM +0300, Nikolay Aleksandrovich Pavlov wrote:
> If you want to avoid spaces, do use concat.

Thanks, that helped.

>     let @/ = searchpat
>     normal! gg0n

Thanks. This has solved the problem. Only one more thing I wish is if it
could highlight the pattern as well. Just hitting "n" once highlights it,
though wish it was automatic.

Will incorporate other suggested improvements as well. Thanks for those.

Mayuresh.

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.