How to sort the vimgrep result in quickfix?

457 views
Skip to first unread message

anhnmncb

unread,
Dec 1, 2008, 7:42:18 AM12/1/08
to vim...@googlegroups.com
Hi, list,
I use vimgrep to search timeline like <\d\+-\d\+-\d\+> in my file, and let
quckfix show me the result, the result sorted by line nubmer by default, I
don't know how to let the result sorted by date?

--
Regards,
anhnmncb

Tony Mechelynck

unread,
Dec 1, 2008, 8:17:34 AM12/1/08
to vim...@googlegroups.com

The quickfix list is always sorted in the order matches (or errors, for
a make) were found. But you can use ":w filename" (or maybe ":saveas
filename" which is subtly different) to save a copy and then use the
":sort" command. I'm not sure whether the quickfix buffer can be
modified in situ.

See
:help :w_f
:help :saveas
:help :sort


Best regards,
Tony.
--
Sauron is alive in Argentina!

anhnmncb

unread,
Dec 1, 2008, 8:59:11 AM12/1/08
to vim...@googlegroups.com
On 2008-12-01, Tony Mechelynck wrote:
>
> On 01/12/08 13:42, anhnmncb wrote:
>> Hi, list,
>> I use vimgrep to search timeline like<\d\+-\d\+-\d\+> in my file, and let
>> quckfix show me the result, the result sorted by line nubmer by default, I
>> don't know how to let the result sorted by date?
>>
>
> The quickfix list is always sorted in the order matches (or errors, for
> a make) were found. But you can use ":w filename" (or maybe ":saveas
> filename" which is subtly different) to save a copy and then use the
> ":sort" command.

Yes, but I don't want this way, because sometimes I vimgrep a file which has
crypted by vim and I don't want so many result written to a file and I forgot
to del it.

I tried to save buffer to a temporary list variable which each atom is a line
in buffer then sort them and vimgrey, but without lucky (I'm a vimscript
newbie).


--
Regards,
anhnmncb

anhnmncb

unread,
Dec 1, 2008, 9:02:33 AM12/1/08
to vim...@googlegroups.com
On 2008-12-01, Tony Mechelynck wrote:
>
> The quickfix list is always sorted in the order matches (or errors, for
> a make) were found. But you can use ":w filename" (or maybe ":saveas
> filename" which is subtly different) to save a copy and then use the
> ":sort" command. I'm not sure whether the quickfix buffer can be
> modified in situ.

And I see some difference between :sort and sort(), with :sort, you can sort
buffer by pattern, which sort() doesn't support.

I tried to make quickfix modifiable then sort it then make it unmodifiable,
but it makes quickfix jumping to relevent line mess up.


--
Regards,
anhnmncb

Tony Mechelynck

unread,
Dec 1, 2008, 9:56:03 AM12/1/08
to vim...@googlegroups.com

Did you try internal ":sort" in the QF window? If it doesn't work, try
moving the contents of the quickfix window to some [No Name] window by
":%y" in the one then ":put" in the other (without the quotes in both
cases). Then try running an appropriate :sort.


Vimgrep is not very useful to search a single file (use plain "slash"
search then). It comes to hand when you have several (maybe many) files
to search for a given string or regexp.


IIUC, internal ":sort" (unlike external :!sort which is a filter)
doesn't need anything to be written out to disk.


Best regards,
Tony.
--
GALAHAD: No. Look, I can tackle this lot single-handed!
GIRLS: Yes, yes, let him Tackle us single-handed!
"Monty Python and the Holy Grail" PYTHON (MONTY)
PICTURES LTD

fritzophrenic

unread,
Dec 2, 2008, 11:31:13 AM12/2/08
to vim_use


On Dec 1, 8:56 am, Tony Mechelynck <antoine.mechely...@gmail.com>
wrote:
> Did you try internal ":sort" in the QF window? If it doesn't work, try
> moving the contents of the quickfix window to some [No Name] window by
> ":%y" in the one then ":put" in the other (without the quotes in both
> cases). Then try running an appropriate :sort.
>

If you do this, you can get the line-jumping behavior (sort of) using
the gF, ctrl-w F, and ctrl-w gF commands.

> Vimgrep is not very useful to search a single file (use plain "slash"
> search then). It comes to hand when you have several (maybe many) files
> to search for a given string or regexp.
>

Actually (though I don't do it very often) I can see much potential
use in brining up the quickfix window with all matches in a single
file.

Michael Wookey

unread,
Dec 2, 2008, 4:39:58 PM12/2/08
to vim...@googlegroups.com
>> Vimgrep is not very useful to search a single file (use plain "slash"
>> search then). It comes to hand when you have several (maybe many) files
>> to search for a given string or regexp.
>
> Actually (though I don't do it very often) I can see much potential
> use in bringing up the quickfix window with all matches in a single
> file.

In my .vimrc, I have the following that searches for all occurrences
of the word under the cursor within the current file. The results
appear in the quickfix window.

function! SearchCurrentFile()
let l:filename = expand("%")
let l:pat = expand("<cword>")
execute("vimgrep /".l:pat."/gj ".l:filename)
copen
endfunction

nmap <silent> <Leader>f :execute SearchCurrentFile()<CR>

I find this useful while programming to bring up a list that shows all
occurrences of a given variable. This makes it easy to see where the
variable has been used.

John Beckett

unread,
Dec 2, 2008, 8:26:05 PM12/2/08
to vim...@googlegroups.com
Michael Wookey wrote:
> In my .vimrc, I have the following that searches for all
> occurrences of the word under the cursor within the current
> file. The results appear in the quickfix window.
>
> function! SearchCurrentFile()
> let l:filename = expand("%")
> let l:pat = expand("<cword>")
> execute("vimgrep /".l:pat."/gj ".l:filename)
> copen
> endfunction
> nmap <silent> <Leader>f :execute SearchCurrentFile()<CR>

Good stuff (BTW the "execute" in the last line can be just "call").

I checked to see if this needed to be added to the wiki, and found that it is
covered in tip 1543 (with a command rather than a mapping):
http://vim.wikia.com/wiki/Find_in_files_within_Vim

command GREP :execute 'vimgrep /'.expand('<cword>').'/gj '.expand('%') | copen

Can anyone tell me if there is a reason to use "expand('%')" in the above? What
about:

command GREP2 :execute 'vimgrep /'.expand('<cword>').'/gj %' | copen

John

fritzophrenic

unread,
Dec 3, 2008, 8:57:04 AM12/3/08
to vim_use


On Dec 2, 7:26 pm, "John Beckett" <johnb.beck...@gmail.com> wrote:
> I checked to see if this needed to be added to the wiki, and found that it is
> covered in tip 1543 (with a command rather than a mapping):http://vim.wikia.com/wiki/Find_in_files_within_Vim
>
> command GREP :execute 'vimgrep /'.expand('<cword>').'/gj '.expand('%') | copen
>
> Can anyone tell me if there is a reason to use "expand('%')" in the above? What
> about:
>
> command GREP2 :execute 'vimgrep /'.expand('<cword>').'/gj %' | copen
>

Possible bonus of expand: you can search again in the same file if
using the command history, even if you are in a different buffer. This
would be even better if expand("%:p") were used instead, since options
like autochdir or commands that change the directory would interfere.

Just using '%' by itself would always search the current buffer, which
would also be useful. Because this is a "search current buffer only"
command, I think that not having the expand() would make more sense.

Note that for this method of searching a buffer to work, the buffer
must be as it appears on the disk.

Personally, I use the cabbrev from the same tip mentioned above, so
that I have a chance to tweak the command before running the search.
Reply all
Reply to author
Forward
0 new messages