Question

How do I map a substitution to a key in my .vimrc without getting an error when I source it?

I'm trying to add

nnoremap <leader>re :'<,'>s/<%=*\s//g | '<,'>s/\s-*%>//g

to my .vimrc but when I do so and reload the file I get the following error:

The error I see in Vim: Mark not set

Was it helpful?

Solution

The problem is that you're combining two :s commands, but the command separator | concludes the :map command, so that the second substitution is executed immediately, causing the error. You need to escape the |, or better use the special <Bar> notation inside mappings:

nnoremap <leader>re :'<,'>s/<%=*\s//g <Bar> '<,'>s/\s-*%>//g

PS: Wouldn't it be more natural to define the mapping in visual mode (as it works on the last selection, anyway)? With a :vmap, the first '<,'> range will be inserted automatically:

xnoremap <leader>re :s/<%=*\s//g <Bar> '<,'>s/\s-*%>//g
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top