Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I defined the following function, which works fine when called directly:

function EncloseParagraphs()
   execute "normal `>a</p>\<Esc>`<i<p>\<Esc>"

But when I use the following mapping

map <silent> <C-P> :call EncloseParagraphs()<CR>

The results look like this

<p><p><p>This is a paragraph.</p></p></p>

<p><p><p>This is a paragraph.</p>

In other words, multiple p tags are added when more than one paragraph is selected in visual mode. Why does the function behave differently when called via the mapped shortcut?

share|improve this question
up vote 0 down vote accepted

From the Vim help documentation:

help :call

When a range is given and the function doesn't handle it
itself, the function is executed for each line in the range,
with the cursor in the first column of that line.  The cursor
is left at the last line (possibly moved by the last function
call).  The arguments are re-evaluated for each line.

Looks like since you're selecting (highlighting) a block of lines then pressing C-P, your function is executed multiple times for that block, once for each line selected.

I imagine you'll need to implement range handling within your function to prevent the multiple execution provided (!) by the :call function.

The remainder of the :call function help documentation discusses how to handle ranges and avoid this problem.

share|improve this answer
Thanks! I suspected something like that, but since I do not specify a range explicitly (which is why the direct call works fine), I did not know where to look. Now I found the relevant information (Note: The <C-U> is required to remove the line range that you get when typing ':' after a count.). – janeden Sep 27 '11 at 12:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.