5

In browsing markdown files, I find the following search useful

/\v^(\*|#)

However, when I try to map it I get an error

nmap <c-j> /\v^(\*|#)<cr>

E488: Trailing characters: #)

What would be the best way to make this work?

  • Questions with this problem have been asked a lot of time on this site. I made this answer to address the most common pitfalls in mappings (and the use of <bar> is one of them) maybe it would be useful for you a next time. – statox Sep 6 '16 at 8:11
9

You need to use <Bar> instead of |:

nmap <c-j> /\v^(\*<Bar>#)<cr>

This is because the | is used as a command separator, for example:

nmap <C-j> :echo "Foo" | echo "bar"

Is actually seen as:

nmap <C-j> :echo "Foo"
echo "bar"

The | separates the nmap command from the echo command. In your case, Vim reads it as:

nmap <c-j> /\v^(\*
#)<cr>

Hence the somewhat confusing error.

Also see :help map_bar.

  • I want similar mapping for inoremap "\begin{document}" "\begin{document}<CR>\end{document}<Esc>O" – alhelal Feb 12 '18 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.