I'd like to define a custom command for doing the following steps: grep the current word, copen, incremental search the grep'ed word. Here's the command I'm using:
nmap <C-p> :execute "grep! -Irn " . shellescape(expand("<cword>")) . " ."<cr>:copen<cr>:g/<cword>/<cr>
But the second cword is expanded to the current word after the quickfix window is opened. How can I use the word that's used by grep earlier?
The same way you got <cword>
into grep
:
nmap <C-p> :execute "grep! -Irn " . shellescape(expand("<cword>")) . " .\<cr>:copen\<cr>:g/" . expand("<cword>"). "/\<cr>"<cr>
Some thoughts:
:g
. E.g. :let @/ = "foo"
nnoremap
escape()
to make it more resilient. See :h escape(
:vimgrep
. See :h :vimg