I have created a few mappings like so:

:nnoremap <buffer> <leader>xyzABC   :call SomeFunc('ABC')<CR>
:nnoremap <buffer> <leader>xyzDE    :call SomeFunc('DE')<CR>
:nnoremap <buffer> <leader>xyzFoo18 :call SomeFunc('Foo18')<CR>
:nnoremap <buffer> <leader>xyzBar22 :call SomeFunc('Bar22')<CR>

which works as desired.

I am now looking for a possibility to create a map-template that would allow me to remove the obvious redundant parts of my mappings, something like

:nnoremap <buffer> <leader> xyz? :call SomeFunc(?)<CR>

so that I could pass any value as parameter to SomeFunc.

Is there such a possibility?

  • 7
    You can use input() or getchar(). However it is probably more normal to use a command instead of a mapping for something that takes a parameter. Sep 17 '15 at 22:44
  • I agree with Peter's comment; commands are better suited to the job than mappings.
    – romainl
    Sep 18 '15 at 8:48

As @peter-rinckler said, it is probably easier to do it with a command:

command! -nargs=* Xyz :call SomeFunc(<q-args>)

Makes it possible to do (in normal mode)

:Xyz ABC

I don't really like the input() because you can't repeat those kinds of things easily.


Thanks to Peter Rincker's comment, I have found the answer:

:nnoremap <buffer> <leader> xyz :call SomeFunc(input('Param: '))<CR>
  • 1
    Here is another example: nnoremap <silent> <leader>tn :exe "tabn" nr2char(getchar())<cr> . The idea is to type <leader>tn2 to navigate to tab 2.
    – Jasha
    May 6 '19 at 21:13

You might want to take a look at SnipMate, which aims at giving TextMate-like snippets to vim. It proved very helpful to me, anyway.

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.