Thursday, January 26, 2017

How to open another file and go to a search pattern

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

No comments:

Post a Comment