Friday, January 27, 2017

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

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 vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: