Hello,

On 2/21/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
Can someone assist with the following:

How do I get a map to repeat a certain number of times?

e.g., given a simple map like:

:map = 0Ea  ;^[j

I'd like to be able to do something along the lines of
95.

to get the map to repeat 95 times. What I find is that the 'j'
key in the map is not being implemented. I have experimented
a bit with using parentheses inside the map to group
commands but that hasn't panned out.

I'm sure a simple vimscript would do this, but I haven't
yet ventured into that domain and would like to find out
what I'm missing here re: mapping.


You can refer to the Vim keymap tutorial about specifying a count
to a map:

http://www.geocities.com/yegappan/vim_keymap.html

The section that describes specifying a count to a map is below:

---------------------------------------------------------------------------------------------------
When a count is entered before invoking a map, the map will not be
repeated the specified number of 'count' times. Instead the count will
be prepended to the key sequence executed for the map. For example,
assume you have mapped <F7> to move the cursor by 5 characters to the
right:

   :nnoremap <F7> 5l

If you invoke the above map with a count of 2 using 2<F7>, the cursor
will not be moved 10 characters to the right. Instead, the cursor will
be moved 25 characters to the right. This is because the count 2 is
prepended to the 5 in the map. You have to use the @= expression
register to repeat the map by the specified number of times. For
example, change the above map command to:

   :nnoremap <F7> @='5l'<CR>

Now, if you use 2<F7>, the cursor will be moved 10 characters to the right.

If you are invoking a function from a normal mode map, then the count
will be supplied to the function. If the function doesn't accept a
range, then this will result in an error. Example:

   function! Myfunc()
       " Function that doesn't accept a range
   endfunction
   :nnoremap _w :call Myfunc()<CR>

If you specify a count to _w, then this will result in an error
message. To remedy this problem you can use the <C-U> command to erase
the text on the command-line before invoking the function as shown
below:

   :nnoremap _w :<C-U>call Myfunc()<CR>

If you want your map to accept a range, then you have to modify the
function to accept a range as shown below:

   function! Myfunc() range
       echo 'range = ' . a:firstline . ',' . a:lastline
   endfunction
   :nnoremap _w :call Myfunc()<CR>

Now you can pass a count to the _w map. The a:firstline and a:lastline
variables in the function refer to the starting line number and ending
line number of the range supplied to the function. The default is the
current line number.

You can also use the internal v:count and v:count1 variables to get
the count specified to the last normal mode command or map. Example:

   :nnoremap <C-W> :<C-U>call Myfunc()<CR>
   function! Myfunc()
       let c = v:count
       " Do something count number of times
   endfunction

The v:count1 variable returns 1 if a count is not specified to the
normal mode command/map.
---------------------------------------------------------------------------------------------------

- Yegappan

Reply via email to