Use Multiple Patterns in Vimgrep in Key Mapping
Asked Answered
L

1

5

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.

Lammers answered 25/11, 2015 at 18:29 Comment(0)
P
9

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>
Pete answered 25/11, 2015 at 19:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.