I'm struggling to understand a character escaping situation involving
First I'm running the following search which matches either the
A character or
B character. I'm using the default
magic option so the parentheses
() and alternatives separator
| must be preceded by backslashes:
Next I'd like to execute this as a
normal command, so I run the following command and it works as expected. Since the search expression is now in a double quoted string, I have to escape each backslash character:
:execute "normal! /\\(A\\|B\\)\r"
Finally I'd like to create a normal mode mapping which executes this command. I run the following command:
:nnoremap <Leader>h :<C-u>execute "normal! /\\(A\\|B\\)\r"<CR>
However, when attempting to use this mapping, I get the following error:
E486: Pattern not found: \(A|B\)
In order to get this mapping to work, I have to add an additional backslash before the
| character, but I don't have to add additional backslashes before the
) characters! The working command is as follows:
:nnoremap <Leader>h :<C-u>execute "normal! /\\(A\\\|B\\)\r"<CR> ^ | three backslashes instead of two
Why is an extra backslash required here to make this work? And why do the parentheses each not also require an extra backslash?
I'm running Vim 8.0, patches 1-1453, compiled myself on WSL.