tags:

views:

342

answers:

5

Hello, My problem is simple. I search a specific pattern in a file (let's say label in a Tex file)

:g/label/#

but there are lots of ocurrences. So I'd like to redirect this output to another file to be able to work easily with it.

Do you have a trick or a command that I don't know? Thanks

+3  A: 

What you're doing is essentially 'grep -n label file' from command line. So you can run that command and > it into a file easily enough.

The derivation of 'grep' is even from basically the same source.

chaos
Thanks for your help. Your solution works perfectly of course. It was so simple that I didn't find it.
Taurus Olson
+2  A: 

I've gotten this of the net at some point:

function GoToLine(mainbuffer)
   let linenumber = expand("<cword>")
   silent bd!
   silent execute "buffer" a:mainbuffer
   silent execute ":"linenumber
   silent nunmap <Enter>
endfunction
command -nargs=1 GoToLine :call GoToLine(<f-args>)

function GrepToBuffer(pattern)
   let mainbuffer = bufnr("%")
   silent %yank g

   enew
   silent put! g
   execute "%!egrep -n" a:pattern "| cut -b1-80 | sed 's/:/ /'"
   silent 1s/^/\="# Press Enter on a line to view it\n"/
   silent :2

   silent execute "nmap <Enter> 0:silent GoToLine" mainbuffer "<Enter>"
   " silent nmap <C-G> <C-O>:bd!<Enter>
endfunction
command -nargs=+ Grep :call GrepToBuffer(<q-args>)

Put it in your .vimrc, then :Grep Foo

Requires external grep program to work properly.

I don't see how this is any different than just "/pattern<ret>n<ret>"
Paul Tomblin
Sorry, was a bit quick to answer. Redid my answer. Hope it helps.
Thank you. I think this will help me. It's even better than what I asked.
Taurus Olson
A: 

(Just an idea -- untested.)

You can delete all the lines with your pattern in it, write to another file, and undo the delete.

:g/label/d
:w matches
u
strager
+2  A: 
:redir > matches.txt|execute 'g/foo/#'|redir END

See :h :redir, you can also redirect to registers, variables, the clipboard etc.

Brian Carper
+1  A: 

it's not clear from the original post what you mean by "work easily with it" but it's often useful to see and quickly jump between all of the matches in a buffer without "extracting" the matches to a separate buffer.

vim has an internal grep built in. your example would be something like this (in vim, % denotes the current file)

:vimgrep /label/ %

This will take you to the first occurrence and report how many matches there were. What's cool is that you can look at all of the matches listed by opening up the quickfix error list using

:cope

Now you can just scroll around and press enter on a line to jump to the exact position of the match.

The quickfix error list is exactly the same buffer you use if you run make from inside vim and your compiler throws errors: it gives you a list of what and where the errors are.

After you've jumped to one location pointed by quickfix, you can go to forwards and backwards in the list via :cn and :cp. :ccl closes the error list.

You can also expand your "error" list via :vimgrepa /newpattern/ % or :vimgrepadd

The (documented) caveat is that vim's internal grep is slower than most native grep implementations (but you do get it "for free" in windows, for example). If you do have a grep installed, you can use :grep instead of :vimgrep for similar results.

quoting :help grep

Vim has two ways to find matches for a pattern: Internal and external. The advantage of the internal grep is that it works on all systems and uses the powerful Vim search patterns. An external grep program can be used when the Vim grep does not do what you want.

The internal method will be slower, because files are read into memory. The advantages are: - Line separators and encoding are automatically recognized, as if a file is being edited. - Uses Vim search patterns. Multi-line patterns can be used. - When plugins are enabled: compressed and remote files can be searched.

You can also use the location list if you're already using the error list for dealing with compilation errors. just add l (for location) to the beginning of the grep command (:lvimgrep,:lvimgrepa :lgrep, :lgrepa) and use :lopen :ln :lp :lcl instead of the :c* ones.

For more commands consult

:help grep
:help quickfix-window 
:help quickfix
:help quickfix-error-lists
Paul Ivanov
Thanks for your answer. These commands also helped to do what I wanted and I've learned something new. Well, my problem was that I had used the same label in a tex file so I needed to find its extra occurrences easily which is easier in a file where I could put them all.
Taurus Olson