8

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?

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
toomey8
  • 195
  • 4
  • 1
    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 06 '16 at 08:11

1 Answers1

12

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.

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
  • I want similar mapping for inoremap "\begin{document}" "\begin{document}<CR>\end{document}<Esc>O" – alhelal Feb 12 '18 at 09:33