Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Multiple Patterns in Vimgrep in Key Mapping

Tags:

vim

vimgrep

In my vimrc I had a mapping to find all line with TODO in them and put them in the quickfix window:

 nnoremap <leader>f :vimgrep /TODO/j % \| :cw<CR>

I now want to add the alternative pattern FIXME in the same way. So I tried

 nnoremap <leader>f :vimgrep /TODO\|FIXME/j % \| :cw<CR>

and

nnoremap <leader>f :vimgrep /TODO<bar>FIXME/j % \| :cw<CR>

but neither return any results.

 vimgrep /TODO|FIXME/j %

works at the : prompt when typed manually. So far my work-around is this:

function! FindFixme()
    vimgrep /TODO\|FIXME/j %
    cw
endfunction
nnoremap <leader>f :call FindFixme()<CR>

But I don't really understand why I can't get it to work as a single map command.

Thanks.

like image 242
PaulM Avatar asked Nov 25 '15 18:11

PaulM


1 Answers

The regular expression item for alternation is \|, and you indeed need to escape a | so that it doesn't end the mapping command. Taken together, you need two backslashes: one for escaping and one to remain for the item:

nnoremap <leader>f :vimgrep /TODO\\|FIXME/j % \| :cw<CR>

But I would prefer the <Bar> notation, maybe even combined with <Bslash>:

nnoremap <leader>f :vimgrep /TODO<Bslash><Bar>FIXME/j % <Bar> :cw<CR>

You can further shorten this to:

nnoremap <leader>f :vimgrep /TODO<Bslash><Bar>FIXME/j %<Bar>cw<CR>
like image 78
Ingo Karkat Avatar answered Oct 22 '22 02:10

Ingo Karkat