Search results on a different buffer

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

Search results on a different buffer

gregory.sacre
Hi all,

I was wondering if it is possible to search for a word in a file and
the result will appear in a new splitted buffer. Like this:

   +----------------------------------------------------------------------+
   |                                                                        |
   |                                                                  
     |
   |                                                                        |
   +----------------------------------------------------------------------+
   | 1. <the line where the keyword is found (line 1)>  |
   | 65. <the second line... (line 65)>                        |
   | ...                                                                    |
   +----------------------------------------------------------------------+

So, more or less like [I would do except that it would stay in the
buffer, and even better (if it is possible), if i press <Enter> on the
line result, it brings me to the line of the file i was making the
search in.

Thanks in advance and happy Vimming :-)

Gregory
Reply | Threaded
Open this post in threaded view
|

Re: Search results on a different buffer

A.J.Mechelynck
----- Original Message -----
From: "Gregory SACRE" <[hidden email]>
To: <[hidden email]>
Sent: Sunday, September 04, 2005 10:49 AM
Subject: Search results on a different buffer


> Hi all,
>
> I was wondering if it is possible to search for a word in a file and
> the result will appear in a new splitted buffer. Like this:
>
>   +----------------------------------------------------------------------+
>   |
> |
>   |
>     |
>   |
> |
>   +----------------------------------------------------------------------+
>   | 1. <the line where the keyword is found (line 1)>  |
>   | 65. <the second line... (line 65)>                        |
>   | ...
> |
>   +----------------------------------------------------------------------+
>
> So, more or less like [I would do except that it would stay in the
> buffer, and even better (if it is possible), if i press <Enter> on the
> line result, it brings me to the line of the file i was making the
> search in.
>
> Thanks in advance and happy Vimming :-)
>
> Gregory

I am not sure I understand what you are asking for, but I _think_ that the
":vimgrep" command provides it in Vim version 7 (not 6.3 where you can use
the ":grep" command which invokes an external "grep" program). The matching
lines appear in the quickfix window.

See
    :help quickfix.txt
    :help :grep
    :help :vimgrep
    " the latter is version 7 only


Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: Search results on a different buffer

A.J.Mechelynck
----- Original Message -----
From: "Gregory SACRE" <[hidden email]>
To: "Tony Mechelynck" <[hidden email]>
Sent: Monday, September 05, 2005 12:06 AM
Subject: Re: Search results on a different buffer


> Sorry, I wasn't surely really clear :-)
>
> Let's take an example:
>
> I have a C file. I start a search for "int". I'd like it to split the
> window i work in and display all the lines where "int" is.
> Furthermore, i'd like to switch to the result buffer and press <Enter>
> on one of the result, and it to bring me to that line.
>
> BTW, i'm using gvim63 at work, so i won't be able to use the latest
> version, because if i understood correctly, this is only for the
> sources for *nix systems.
>
> Thanks for the help.

Well, even in 6.3, if you have a "grep" program in your path you should be
able to do something similar to

    :grep -F -w int  %

(the syntax may vary slightly from one version of "grep" to the next). All
lines containing "int" as a word will be extracted into a quickfix error
file (on Windows, a dos box may temporarily open while the grep program is
being run). Once grep finishes, display these lines in a split window by
using

    :copen

(Instead of % which means "in the current file", you can use one or more
filenames with wildcards; all lines containing the pattern in all matching
files will then be found -- example:

    :grep -F MyNewFunction *.c *.h

)

Hitting "Enter" on any line of the quickfix window will make the
corresponding line (and the surrounding part of its file) appear in the
other window.

See
    :help quickfix.txt
    :help :grep
    :help :copen
    :help 'grepprg'
    :help 'grepformat'

and the additional help items mentioned in them.

You're saying you use Vim 6.3 at work; you might try installing Vim 7.00aa
ALPHA at home and try its ":vimgrep" command, which is the same as ":grep"
except it uses vim-style search patterns and requires no external program.
You may either download Vim 7 in source form for any system from
http://ftp.vim.org/pub/vim/unstable/snapshot/ (a single zipfile contains all
the sources and all the necessary makefiles to compile Vim on all supported
systems and produce messages in all supported languages) or in executable
form (Win32 only) from a link on my Vim page
http://users.skynet.be/antoine.mechelynck/vim/ ; up-to-date runtime files
are also included in both cases. Please read the blurb before downloading.


Best regards,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: Search results on a different buffer

Hari Krishna Dara
In reply to this post by gregory.sacre

On Sun, 4 Sep 2005 at 1:49am, Gregory SACRE wrote:

> Hi all,
>
> I was wondering if it is possible to search for a word in a file and
> the result will appear in a new splitted buffer. Like this:
>
>    +----------------------------------------------------------------------+
>    |                                                                        |
>    |
>      |
>    |                                                                        |
>    +----------------------------------------------------------------------+
>    | 1. <the line where the keyword is found (line 1)>  |
>    | 65. <the second line... (line 65)>                        |
>    | ...                                                                    |
>    +----------------------------------------------------------------------+
>
> So, more or less like [I would do except that it would stay in the
> buffer, and even better (if it is possible), if i press <Enter> on the
> line result, it brings me to the line of the file i was making the
> search in.
>
> Thanks in advance and happy Vimming :-)
>
> Gregory
>
>

Other than what Tony already suggested, you could try my greputils.vim
plugin on vim.org, especially if you don't have/don't want external
grep (or not using vim7). You would need to use:

:GrepBufs % <keyword>

--
HTH,
Hari


       
               
______________________________________________________
Click here to donate to the Hurricane Katrina relief effort.
http://store.yahoo.com/redcross-donate3/
Reply | Threaded
Open this post in threaded view
|

Re: Search results on a different buffer

gregory.sacre
Hi,

I tried your suggestion, Hari, and ti doesn't give me the results, but
something like the command history of the GrepBufs. It opens a new
window with this:
+----------------------------------------------------------------------------+
Last grep command: GregpBufs % <keyword1>
[Grep Preview]|1| Last grep command: GregpBufs % <keyword2>
+-----------------------------------------------------------------------------+

But unfortunately, I don't have the results of my query.

Thanks for your help,

Gregory

On 9/9/05, Hari Krishna Dara <[hidden email]> wrote:

>
> On Sun, 4 Sep 2005 at 1:49am, Gregory SACRE wrote:
>
> > Hi all,
> >
> > I was wondering if it is possible to search for a word in a file and
> > the result will appear in a new splitted buffer. Like this:
> >
> >    +----------------------------------------------------------------------+
> >    |                                                                        |
> >    |
> >      |
> >    |                                                                        |
> >    +----------------------------------------------------------------------+
> >    | 1. <the line where the keyword is found (line 1)>  |
> >    | 65. <the second line... (line 65)>                        |
> >    | ...                                                                    |
> >    +----------------------------------------------------------------------+
> >
> > So, more or less like [I would do except that it would stay in the
> > buffer, and even better (if it is possible), if i press <Enter> on the
> > line result, it brings me to the line of the file i was making the
> > search in.
> >
> > Thanks in advance and happy Vimming :-)
> >
> > Gregory
> >
> >
>
> Other than what Tony already suggested, you could try my greputils.vim
> plugin on vim.org, especially if you don't have/don't want external
> grep (or not using vim7). You would need to use:
>
> :GrepBufs % <keyword>
>
> --
> HTH,
> Hari
>
>
>
>
> ______________________________________________________
> Click here to donate to the Hurricane Katrina relief effort.
> http://store.yahoo.com/redcross-donate3/
>
Reply | Threaded
Open this post in threaded view
|

RE: Search results on a different buffer

jason heddings-2
Hi Gregory-

I do this using a background search for vim.  You could easilly make this
into a one-step function.

First, you'll need these two functions (watch for line wrapping if you
copy/paste):

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""
" runs a search in the background
function! BackgroundGrep(args)
  let g:bg_grep_file = tempname()
  silent execute "!start cmd /c \"" . &grepprg . " " . a:args . " >"  .
g:bg_grep_file . " 2>&1\""
  echo g:bg_grep_file
endfunction

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""
" loads the results of the last background search
function! LoadBackgroundGrepResults()
  let l:gfm = escape(&grepformat, "\" ")
  let l:efm = escape(&errorformat, "\" ")

  if !exists("g:bg_grep_file")
    echohl ErrorMsg | echo "BackgroundGrep() must be run first" | echohl
None
    return
  endif

  if !filereadable(g:bg_grep_file)
    echohl ErrorMsg | echo "Cannot open bg_grep_file: " . g:bg_grep_file |
echohl None
    return
  endif

  " we have to reload the errorformat to match grep
  execute "set errorformat=" . l:gfm
  execute "cgetfile " . g:bg_grep_file
  execute "set errorformat=" . l:efm

  " open the quickfix window
  copen
endfunction


Next, you'll want to make a mapping to these.  I use the following:
nmap <A-/> :call BackgroundGrep("-R -w '<cword>' *")<CR>
nmap <A-?> :call BackgroundGrep("-R -w '" . input("-- ") . "' *")<CR>
nmap <A-'> :call LoadBackgroundGrepResults()<CR>

The result of these is, <A-/> will search all files for the current word
under the cursos.  <A-?> gives me a prompt to type a search word.  <A-'>
loads the results once the grep has been finished.  This search is run from
the currend directory recursively (:pwd).

If you don't want the search to run in the background, simply add the line
"call LoadBackgroundGrepResults()" (no quotes) to the end of the
"BackgroundGrep() function.  NOTE: the options I have in my mappings are for
grep.  If you are on Win32 using findstr, you'll need to modify them.

Now, all searches run this way will open the quickfix window to display your
search results.  Happy to answer questions if you have them.

--jah


-----Original Message-----
From: Gregory SACRE [mailto:[hidden email]]
Sent: Monday, 12 September, 2005 03:54
To: Hari Krishna Dara
Cc: [hidden email]
Subject: Re: Search results on a different buffer

Hi,

I tried your suggestion, Hari, and ti doesn't give me the results, but
something like the command history of the GrepBufs. It opens a new window
with this:
+---------------------------------------------------------------------------
-+
Last grep command: GregpBufs % <keyword1> [Grep Preview]|1| Last grep
command: GregpBufs % <keyword2>
+---------------------------------------------------------------------------
--+

But unfortunately, I don't have the results of my query.

Thanks for your help,

Gregory

On 9/9/05, Hari Krishna Dara <[hidden email]> wrote:
>
> On Sun, 4 Sep 2005 at 1:49am, Gregory SACRE wrote:
>
> > Hi all,
> >
> > I was wondering if it is possible to search for a word in a file and
> > the result will appear in a new splitted buffer. Like this:
> >
> >
+----------------------------------------------------------------------+
> >    |
|
> >    |
> >      |
> >    |
|
> >
+----------------------------------------------------------------------+
> >    | 1. <the line where the keyword is found (line 1)>  |
> >    | 65. <the second line... (line 65)>                        |
> >    | ...
|

> >    
> > +-------------------------------------------------------------------
> > ---+
> >
> > So, more or less like [I would do except that it would stay in the
> > buffer, and even better (if it is possible), if i press <Enter> on
> > the line result, it brings me to the line of the file i was making
> > the search in.
> >
> > Thanks in advance and happy Vimming :-)
> >
> > Gregory
> >
> >
>
> Other than what Tony already suggested, you could try my greputils.vim
> plugin on vim.org, especially if you don't have/don't want external
> grep (or not using vim7). You would need to use:
>
> :GrepBufs % <keyword>
>
> --
> HTH,
> Hari
>
>
>
>
> ______________________________________________________
> Click here to donate to the Hurricane Katrina relief effort.
> http://store.yahoo.com/redcross-donate3/
>

Reply | Threaded
Open this post in threaded view
|

Re: Search results on a different buffer

Hari Krishna Dara
In reply to this post by gregory.sacre

I apologize, the usage should have been:

:GrepBufs <keyword> %

It is like for the grep command itself where you say

grep <keyword> <files>

--
Thanks,
Hari

On Mon, 12 Sep 2005 at 10:54am, Gregory SACRE wrote:

> Hi,
>
> I tried your suggestion, Hari, and ti doesn't give me the results, but
> something like the command history of the GrepBufs. It opens a new
> window with this:
>
+----------------------------------------------------------------------------+
> Last grep command: GregpBufs % <keyword1>
> [Grep Preview]|1| Last grep command: GregpBufs % <keyword2>
>
+-----------------------------------------------------------------------------+

>
> But unfortunately, I don't have the results of my query.
>
> Thanks for your help,
>
> Gregory
>
> On 9/9/05, Hari Krishna Dara <[hidden email]> wrote:
> >
> > On Sun, 4 Sep 2005 at 1:49am, Gregory SACRE wrote:
> >
> > > Hi all,
> > >
> > > I was wondering if it is possible to search for a word in a file and
> > > the result will appear in a new splitted buffer. Like this:
> > >
> > >  
+----------------------------------------------------------------------+
> > >    |                                                                    
  |
> > >    |
> > >      |
> > >    |                                                                    
  |
> > >  
+----------------------------------------------------------------------+
> > >    | 1. <the line where the keyword is found (line 1)>  |
> > >    | 65. <the second line... (line 65)>                        |
> > >    | ...                                                                
  |
> > >  
+----------------------------------------------------------------------+

> > >
> > > So, more or less like [I would do except that it would stay in the
> > > buffer, and even better (if it is possible), if i press <Enter> on the
> > > line result, it brings me to the line of the file i was making the
> > > search in.
> > >
> > > Thanks in advance and happy Vimming :-)
> > >
> > > Gregory
> > >
> > >
> >
> > Other than what Tony already suggested, you could try my greputils.vim
> > plugin on vim.org, especially if you don't have/don't want external
> > grep (or not using vim7). You would need to use:
> >
> > :GrepBufs % <keyword>
> >
> > --
> > HTH,
> > Hari
> >
> >
> >
> >
> > ______________________________________________________
> > Click here to donate to the Hurricane Katrina relief effort.
> > http://store.yahoo.com/redcross-donate3/
> >
>
>

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
Reply | Threaded
Open this post in threaded view
|

Re: Search results on a different buffer

Hari Krishna Dara
In reply to this post by gregory.sacre

On Mon, 12 Sep 2005 at 7:18pm, Gregory SACRE wrote:

> Thank you all for your answers :-)
>
> Jason: unfortunately, I'm using Win32 at work, so i'll need to look
> for changes regarding grep.
>
> Hari: Thanks a lot! this is what i needed. Do you think it would be
> possible to retrieve the line number and to make some kind of "Goto
> <line>" when you press <Enter> on the line instead of "Open File" ?
>
> Thanks a lot for your help!

I am not sure what you mean by "Goto <line>". What happens currently
when you press <Enter> on a result line? Doesn't the cursor "goto" the
line in the original buffer? This functionality should work just like
for quick-fix buffer. In fact, you can use the :Cfile command in the
Grep Preview window to convert the results into a quickfix result. Take
a look at the complete documentation of greputils.vim at the start of
the file (or see the vim.org script page).

PS: Ccing vim list as you seem to have intended it.

--
HTH,
Hari


               
__________________________________
Yahoo! Mail - PC Magazine Editors' Choice 2005
http://mail.yahoo.com
Reply | Threaded
Open this post in threaded view
|

Re: Search results on a different buffer

gregory.sacre
If I press <Enter> on the line, it gives me an error:
Can't read file: C:\<path_of_the_file>:<line_where_the_keyword_was_found>: 56 16

I don't know what these 56 and 16 mean but I'll have a look at
greputils as you suggested.

Thanks a lot again to everybody!

On 9/12/05, Hari Krishna Dara <[hidden email]> wrote:

>
> On Mon, 12 Sep 2005 at 7:18pm, Gregory SACRE wrote:
>
> > Thank you all for your answers :-)
> >
> > Jason: unfortunately, I'm using Win32 at work, so i'll need to look
> > for changes regarding grep.
> >
> > Hari: Thanks a lot! this is what i needed. Do you think it would be
> > possible to retrieve the line number and to make some kind of "Goto
> > <line>" when you press <Enter> on the line instead of "Open File" ?
> >
> > Thanks a lot for your help!
>
> I am not sure what you mean by "Goto <line>". What happens currently
> when you press <Enter> on a result line? Doesn't the cursor "goto" the
> line in the original buffer? This functionality should work just like
> for quick-fix buffer. In fact, you can use the :Cfile command in the
> Grep Preview window to convert the results into a quickfix result. Take
> a look at the complete documentation of greputils.vim at the start of
> the file (or see the vim.org script page).
>
> PS: Ccing vim list as you seem to have intended it.
>
> --
> HTH,
> Hari
>
>
>
> __________________________________
> Yahoo! Mail - PC Magazine Editors' Choice 2005
> http://mail.yahoo.com
>