There's a lot missing there. Ignoring the part after the first
<CR> for now...
First, there are some contexts, i.e. where a filename is expected, where you can use
<cword> bare. But in many cases, including this one, you need to force it to be resolved or "expanded". That can be done with, naturally,
expand('<cword>'). Unfortunately, you can't just stick this in between your delimiters (
:lvimgrep only accepts a clean, static string there.
The standard way to deal with dynamic/variable components on the command line is to make a string out of the command and pass it to
Within the string divide things into static and dynamic parts and concatenate them together. Separating into static/dynamic/delimiter parts:
Note that I removed
# from #3 because it's going to be picked up by
Now we use single quotes around the static parts to avoid any extra evaluation/interpretation that double quotes may cause. This is just the pattern part (#3-5):
'\v^#+.*(' . expand('<cword>') . ')'
Bringing in the rest including delimiters (I'm using
/ to avoid complications of quotes) and passing to
nnoremap gO :exec 'lvimgrep /\v^#+.*(' . expand('<cword>') . ')/ %'
Now you need the second command. That has to be separated from the preceding part with
| and in mappings that must be escaped as
nnoremap gO :exec 'lvimgrep /\v^#+.*(' . expand('<cword>') . ')/ %' \| :lopen<CR>
Note that the first
<CR> you had was unnecessary as the
| takes care of submitting both commands.
FYI, I tested this and, if I'm understanding your use case correctly, it worked fine.