How do I fix this bad vim mapping?

Ovid on 2006-08-11T18:11:41

OK, this is not behavior I was expecting. Sometimes I'll post code in places where I don't have code or ecode tags, so I need to manually encode the '&', '<' and '>' symbols. That gets very annoying, particularly since I often find myself encoding the ampersand last, screwing up the other encodings and having to redo it. I figured that a simple vim mapping would do this:

noremap ,e  :%s/&/\&/g | %s/>/\>/g |%s/

However, that fails because as soon as I source my .vimrc, it rus the second and third subtitutions! So I tried again:

noremap ,e :exe '%s/&/\&/g | %s/>/\>/g |%s/

That does the same thing. What gives? Am I going to have to write a function for this?


Putting bars in maps

Damian on 2006-08-11T23:54:01

From the fine manual:

Since the '|' character is used to separate a map command from the next
command, you will have to do something special to include  a '|' in {rhs}.

There are three methods:
   use       works when                    example
   <Bar>     '<' is not in 'cpoptions'     :map _l :!ls <Bar> more^M
   \|        'b' is not in 'cpoptions'     :map _l :!ls \| more^M
   ^V|       always, in Vim and Vi         :map _l :!ls ^V| more^M

Re:Putting bars in maps

Ovid on 2006-08-12T17:26:00

How did you find that in the fine manual? "help |" doesn't help, nor does my various attempts at escaping the bar. (Well, I've had trouble escaping bars before, but I equivocate ...)

Re:Putting bars in maps

Ovid on 2006-08-12T17:26:56

"nor do my various attempts".

Re:Putting bars in maps

Damian on 2006-08-12T22:48:27

How did you find that in the fine manual?
I've actually read the entire manual (because I'm writing a book on Vim), so it was more a matter of remembering than finding.

If I hadn't, then I would have typed:

:help bar<TAB>
(where that <TAB> is literally the act of hitting the TAB key). That would have produced:
bar                cpo-bar            'toolbar'          gui-scrollbars
:bar               map_bar            +toolbar           -scrollbarwidth
/bar               expr-barbar        gui-toolbar        'toolbariconsize'
/\bar              CTRL-W_bar         toolbar-icon
:\bar              escape-bar         hl-PmenuSbar
bars               <Bar>              hl-Scrollbar
:help bar
at which point it's trivial to find the map_bar entry.

Re:Putting bars in maps

Ovid on 2006-08-12T23:19:13

A book on vim? Thank god. We've desperately needed a really good one. A friend of mine is kind of a vim god and people keep telling him he needs to write such a book, but it's good to know it's actually going to get written. I'll buy it!

Re:Putting bars in maps

dws on 2006-08-13T00:25:33

This is good news. We need a good book on vim. There's a lot of power that goes way under-exploited because of the lack of readable, well-indexed documentation.

Re:Putting bars in maps

jmm on 2006-08-12T18:09:32

Google can do a pretty good job of putting bars in maps, I suspect. :-)