Which is the best way of doing some mapping reuse?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Which is the best way of doing some mapping reuse?

Raúl Núñez de Arenas Coronado-2
Hi all :)

Usually in my scripts I use constructs like this (just an example):

nnoremap <silent><buffer> <LocalLeader>KEY m'gg"+yG''
imap     <silent><buffer> <LocalLeader>KEY <C-\><C-O><LocalLeader>KEY
...maybe more mappings here for other modes...

This way I put the logic only in one map (the nnoremap) one, and reuse
it in the other mappings.

Of course this doesn't work because after the <C-\><C-O> only the m'
command is executed, Vim returns to insert mode and gg"+yG'' is inserted
as typed text.

There are plenty of solutions to this problem, and I don't know which
one is better/shorter/more clever.

First one is copying and pasting the map, but that is not good for
maintenance, because if you have to modify the mapping later, you have
to do it in probably many lines:

nnoremap <silent><buffer> <LocalLeader>KEY m'gg"+yG''
inoremap <silent><buffer> <LocalLeader>KEY <Esc>m'gg"+yG''i

I don't like this, really.

Second solution is using a function:

function! s:whatever()
    normal! m'gg"+yG''
endfunction
nnoremap <buffer><silent> <LocalLeader>KEY :call <SID>whatever()<CR>
inoremap <buffer><silent> <LocalLeader>KEY <C-R>=<SID>whatever()<CR>

Far from elegant, I think, but it should work.

Third solution, probably the best?:

nnoremap <silent><buffer> <LocalLeader>KEY m'gg"+yG''
imap     <silent><buffer> <LocalLeader>KEY <C-\><C-N><LocalLeader>KEYi

According to the help, the above insert mode mapping won't work in ex
mode. Not a big problem, though, for my needs.

Any better way of achieving the same? Any problem I'm not aware of?

Thanks in advance!

--
Raúl "DervishD" Núñez de Arenas Coronado
Linux Registered User 88736 | http://www.dervishd.net
The revolution will not be televised

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Which is the best way of doing some mapping reuse?

John Little-4

On May 2, 10:45 pm, Raúl Núñez de Arenas Coronado <[hidden email]>
wrote:
> First one is copying and pasting the map...

How about a level of indirection using :exe :

let mapped = "<silent><buffer> <LocalLeader>KEY "
let mapping = "m'gg\"+yG\""
exe 'nnoremap ' . mapped . mapping
exe 'inoremap ' . mapped . '<esc>' . mapping . 'i'

I don't know how usable this is, I just checked :map output to see
that the mappings had been made.

You could extend this to a general function, f. ex.
    call Makemaps(mapped, mapping, modes)

Regards, John

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Which is the best way of doing some mapping reuse?

Raúl Núñez de Arenas Coronado-2
On Sun, May 2, 2010 at 14:16, John Little <[hidden email]> wrote:

>
> On May 2, 10:45 pm, Raúl Núñez de Arenas Coronado <[hidden email]>
> wrote:
>> First one is copying and pasting the map...
>
> How about a level of indirection using :exe :
>
> let mapped = "<silent><buffer> <LocalLeader>KEY "
> let mapping = "m'gg\"+yG\""
> exe 'nnoremap ' . mapped . mapping
> exe 'inoremap ' . mapped . '<esc>' . mapping . 'i'
>
> I don't know how usable this is, I just checked :map output to see
> that the mappings had been made.
>
> You could extend this to a general function, f. ex.
>    call Makemaps(mapped, mapping, modes)
>
> Regards, John
>
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>



--
Raúl "DervishD" Núñez de Arenas Coronado
Linux Registered User 88736 | http://www.dervishd.net
The revolution will not be televised

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php