I've write myself a function, which receive a motion and perform it in other window. However, a problem raises when I need to map a real motion to a key sequence:

nnoremap <c-p> :call Motion('\<C-F>')<cr>

Unexpectedly, vim executes <C-F> for me when I just want vim to put <C-F> there as string literal.

Is there any solution to this?

  • 1
    try replacing the first < with <lt>, it solves a problem I had that may rely to this one – nobe4 Aug 28 '15 at 8:54
  • 2
    Or try ctl-v, ctl-f. – cxw Aug 28 '15 at 9:57
  • use double quotes – Christian Brabandt Apr 15 '16 at 8:08

You can pass a literal Ctrl+f by pressing CtrlvCtrlf in insert mode, but I'm not sure this will work for you.

The other method which I find more correct (or easier and more widely used) is to pass the key/motion as a string and use :execute to create the maps inside your function. Here is an example from the CtrlP plugin:

The part in the plugin:

if g:ctrlp_map != '' && !hasmapto('<plug>(ctrlp)')
        exe 'map' g:ctrlp_map '<plug>(ctrlp)'

You can set the motion in .vimrc as:

let g:ctrlp_map = '<C-f>'

and then <plug>(ctrlp) would be mapped to it.

In your case you would have a simple call in .vimrc like this:

nnoremap <c-p> :call Motion('<C-F>')<cr>

and in Motion:

function Motion(sequence)
    " other stuff
    execute 'map' a:sequence '<Plug>(action)'
    " more stuff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.