pass count to nmap with call

17 views
Skip to first unread message

Bee

unread,
Jul 14, 2016, 2:28:12 AM7/14/16
to vim_use
I tried to pass a count to <F11> but got a range error.

nmap <F11> :call VMA()<bar>:bn<cr>

Can it be done?

nmap <F12> :bn<cr>

4<F12> works

Bill

Jürgen Krämer

unread,
Jul 14, 2016, 3:00:06 AM7/14/16
to vim...@googlegroups.com

Hi,
in my experience counts and mappings don't work together very well. A
preceding count is always used for the first command in the mapping only.
So what happens when you enter

4<F11>

is that Vim converts your count to an address for the command-line,
which effectively results in

:.,.+3call VMA()|:bn

In your mapping you first have to remove this address and "move" the
count to the front of the command :bn. The count is stored in the global
variable v:count. There is an example at :help v:count which can be
adopted:

:nmap <F11> :<C-U>call VMA()<bar>execute v:count1 . "bn"<cr>

Note that I removed the superfluous second colon and that instead of
v:count I used v:count1 which defaults to 1 if no count was given.

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)

Bee

unread,
Jul 14, 2016, 5:45:31 AM7/14/16
to vim_use
More reading... a count is not needed.
First time I have used bufdo!

function! B_() " field delimiter ';'
g/^\d*;\+$/d " remove blank records
v/^\d\+;/d " remove non-data records
let @4=expand('%:t:r') " remove path, extension
let @4=substitute(@4,'\s\+',' ','g') " remove extra whitespace, normalize
let @4=substitute(@4,'^ ','','') " remove leading whitespace
let @4=substitute(@4,' $','','') " remove trailing whitespace
let @4=substitute(@4,'.*\zs ',';','') " replace ' ' ';' before date
let @4.=";" " append ';' filename;date
g/^\d\+;/:normal "4P " prefix lines filename;date;
endfun " example :bufdo call B_()

function! B() " everyone keep quiet
silent bufdo call B_()
endfun " example :call B()

Bill
Reply all
Reply to author
Forward
0 new messages