The following function is a start; it will probably fail on a number of cases. You can build off it:
function! MyMap (...)
let l:i = 0
if a:0 > 2
let l:opt = a:1
let l:i += 1
endif
let l:key = a:000[i]
let l:action = a:000[i+1]
let l:mod = 'A'
if has('mac')
let l:mod = 'D'
endif
echo 'nnoremap' l:opt printf('<%s-%s>', l:mod, l:key) l:action
exe 'nnoremap' l:opt printf('<%s-%s>', l:mod, l:key) l:action
endfunction
command! -na=+ MyMap call MyMap(<f-args>)
A break down:
function! MyMap (...)
defines a function that takes a variable number of arguments. From :h ...
:
a:0 a:1 a:000 E740 ...
Up to 20 arguments can be given, separated by commas. After the named
arguments an argument "..." can be specified, which means that more arguments
may optionally be following. In the function the extra arguments can be used
as "a:1", "a:2", etc. "a:0" is set to the number of extra arguments (which
can be 0). "a:000" is set to a List that contains these arguments. Note
that "a:1" is the same as "a:000[0]".
- Next we check if we have more than two arguments, and if so, use the first one as special arguments to
nnoremap
. The other arguments are the key and the action.
- Next, we set the modifier used to be
A
(or Alt) as a default, and change it to D
(or Command) if has('mac')
.
- Then we use
execute
to create and run the nnoremap
command. printf('<%s-%s>', l:mod, l:key)
will give <A-x>
or <D-x>
accordingly.
- Finally we define a command to wrap the function.
<f-args>
splits the arguments on spaces and tabs, so be careful about those.
You can run the command like so:
MyMap <silent> q :wqa<cr>:H
Or, if additional special arguments are used:
MyMap <silent>\ <buffer> q :wqa<cr>:H
has('mac')
?