wasd
wasd

Reputation: 1572

grep in VIM opens terminal with results before showing them in vim

I run vim in terminal and whenever I search for something using grep, like

:grep search_for filename

it closes the vim, shows result in terminal with the "Press Enter to continue" message and only after I press enter it shows the results back in vim. How to prevent this terminal opening and show results directly in vim?

Upvotes: 3

Views: 205

Answers (1)

Peter Rincker
Peter Rincker

Reputation: 45117

One method would be to do the following:

function! Grep(...)
    return system(join([&grepprg] + [a:1] + [expandcmd(join(a:000[1:-1], ' '))], ' '))
endfunction

command! -nargs=+ -complete=file_in_path -bar Grep  cgetexpr Grep(<f-args>)
command! -nargs=+ -complete=file_in_path -bar LGrep lgetexpr Grep(<f-args>)

cnoreabbrev <expr> grep  (getcmdtype() ==# ':' && getcmdline() ==# 'grep')  ? 'Grep'  : 'grep'
cnoreabbrev <expr> lgrep (getcmdtype() ==# ':' && getcmdline() ==# 'lgrep') ? 'LGrep' : 'lgrep'

augroup quickfix
    autocmd!
    autocmd QuickFixCmdPost cgetexpr cwindow
    autocmd QuickFixCmdPost lgetexpr lwindow
augroup END

This will let you type out :grep foo without the need for "Press Enter" prompt. I would recommend you read the article/gist as well.

Upvotes: 1

Related Questions