How to reproduce this mapping:

nnoremap <Leader>a :DoSomething <C-r>=do_something_arguments()<CR>

..in a function?

The desired effect is to open command-line mode with some text filled in, and leave it at that.

:normal only works on "complete commands", same seems to be also true for :exec.

Reasoning behind this is to define an operator (using vim-operator-user) that would prepopulate a command:

call operator#user#define('conf-entry', 'Op_command_conf_entry')
function! Op_command_conf_entry(motion_wiseness)
  normal! `[yv`]

  let l:conf_path = '~/.vim/conf/_' . @" . '/'

  exec ':EditCreating ' . l:conf_path

nmap _c  <Plug>(operator-conf-entry)

Here, the exec line executes immediately, which is the behavior I want to change.

What I'm probably looking for is the command-line-mode equivalent to :startinsert, something like :startcommand.

The alternative solution is to use input() which can offer both prepopulating as well as enabling completion. However this is not exactly command-line-mode.

1 Answer 1


You should make use of :h map-<expr>. That is

"instead of exec
return ':EditCreating ' . l:conf_path


nnoremap <expr><Plug>(operator-conf-entry) ... whatever

Probably, you'll need some more fixes in your code, but it's impossible to say without seeing the whole picture.

Otherwise, there's no such thing as startcommand, so only feedkeys or input remain.

  • 1
    Unfortunately, <expr> seems incompatible with vim-operator-user's <Plug> mappings, however feedkeys produced exactly the result I was looking for.
    – mwgkgk
    Commented Dec 7, 2019 at 8:27
  • @mwgkgk Using feedkeys() should be avoided whenever possible. In your case, you should probably call() your operator function manually and return the result as <expr>, instead of execute-ing g@.
    – Matt
    Commented Dec 7, 2019 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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