Reputation: 40029
How do you create a normal mode command which accepts a number as part of it? Examples include 6>>
(indents 6 lines of text), 2dw
(deletes 2 words), and 23yy
(copies 23 lines to the copy buffer). Is there a way to do this using nmap
or its ilk?
Specifically, I would like to create a mapping such that if I enter
<leader>4bu
in Normal mode, Vim will do
:4buf<CR>
Upvotes: 0
Views: 154
Reputation: 53604
nnoremap
, not nmap
.Number supplied to last normal-mode command is accessible via v:count
variable, so, for example, the following mapping will add supplied number to the buffer text number times:
nnoremap ,a a<C-r>=v:count<CR><Esc>
The reason why 20,a
will add 20
20 times is because 20,a
is rewritten as 20a<C-r>...
. To avoid that you may use
nnoremap ,a :<C-u>call feedkeys("a".v:count."\e", "n")<CR>
. <C-u>
will discard the count for the ex command but it is still accessible from v:count
variable.
Upvotes: 2