Friday, January 27, 2017

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

2017-01-27 5:46 GMT+03:00 Mayuresh <mayuresh@acm.org>:
> 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 vim_use+unsubscribe@googlegroups.com.
> 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 vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: