notmii
notmii

Reputation: 413

VIM finding words in code repository

I am trying to create a VIM function that will allow me to find words in my code repository by using the external grep function of linux then highlight all occurences of that word.

Also the silent on the execute is suppose to be suppressing the output of the external program but it is not working..

set grepprg=grep\ -rns\ -C\ 1\ "
set grepformat=%f:%l:%m

function! WordFind()
    let l:word = input("Find:")
    execute 'silent grep ' . l:word . ' .'
    :redraw!
    :copen
    execute '/' . l:word
endfunction

Upvotes: 0

Views: 106

Answers (1)

romainl
romainl

Reputation: 196606

Are you looking for words or symbols? If you are looking for symbols (functions, variables, arrays...) you might find ctags and/or cscope to be a better fit.

See :help tags for more information.

But... you didn't ask anything so, what do you want from us? A code critique?

The last line of your function seems useless because every line in the quickfix window contains your word at least once. It will just move the cursir horizontally on the first line which is of no use.

I would use :cwindow instead of :copen.

Other than that, your function seems ok.

EDIT

Well no, it is not OK, the last line wouldn't work anyway. The revised function below works for me using your grepprg and grepformat settings:

function! WordFind()
    let l:word = input("Find:")
    execute 'silent grep ' . l:word . ' .'
    redraw!
    cwindow
    wincmd p
    let @/ = l:word
endfunction

The wincmd p line may not be necessary after all.

I don't believe the output of grep can be completely hidden in CLI Vim.

Upvotes: 2

Related Questions