Question

I am trying to define a vim key map for putting the selected line inside an HTML p tag:

vnoremap <leader>bp c<p><cr></p><esc>P

It doesn't work, I think vim is interpreting <p> in a special way. How can I solve this?

Was it helpful?

Solution

It looks like you are using Vim in "compatible mode" which is something only hopelessly masochistic people do. In "nocompatible mode", your mapping works as expected so you should probably make sure nocompatible is set (creating a blank ~/.vimrc should be enough).

Anyway, your <p>s are not where the problem is because they are inserted normally, it's your <cr> and your <esc> that are causing a mess: since you are running Vim in "compatible mode", the cpoptions option includes < which causes Vim to not recognize <CR> and friends as special keys.

Running Vim in "nocompatible mode" is the best way to go but you can also use the following notation if you really insist on going "compatible":

vnoremap <leader>bp c<p>^M</p>^]P

where ^M is inserted with <C-v><CR> and ^] is inserted with <C-v><Esc>.

OTHER TIPS

You may want to look into Tim Pope's Surround plugin. Then you can do S<p> and surround the visually selected text with <p> tags.

However the answer please see @romainl answer. I would also suggest you read up on key-notation via :h key-notation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top