Search code examples
vim

Mapping a working substitute command to a shortcut via vimrc


The following substitute command works fine on the command line:

:s/\vlabel\{(\w|:)+}/nonumber

It searches for label{eq:xyz123} and replaces its occurence on the current line line with nonumber.

I would like to map this to a command via vimrc. I tried:

nnoremap <leader>nn :s/\vlabel\{(\w|:)+}/nonumber<CR>

but this gives the error:

E492: Not an editor command: :)+}/nonumber<CR>

What is the right way to effect this mapping?


Solution

  • You can use:

    nnoremap <leader>nn :s/\vlabel\{(\w<bar>:)+}/nonumber<CR>
    

    Note the use of <bar> instead of |. The error occurs because | is interpreted as a delimiter of commands; i.e., Vim is interpreting your command as if there were two separate lines:

    nnoremap <leader>nn :s/\vlabel\{(\w
    :)+}/nonumber<CR>
    

    For more, you can read :help map-bar.