0

Suppose I map a key to enter a command in the command buffer, but without hitting enter:

function! BuildSystemTask()
    if inProject() "generic function that checks if I am in a buildable project
        :!make<Space>
    else
        echom("you are not in a valid project, whatever that means")
    endif
endfunction
nnoremap <Leader>b :BuildSystemTask()<CR>

The idea here is that I want to hit my mapping, and I would get "make " in the command buffer so that I can type whatever I want. However, this does not do it because it actually enters the whole command :!make<Space> as is in the command buffer.

I also tried :execute "normal! :make<Space>" and that didn't work either. It hits <CR> at the end.

Any idea how to do this? Or if if there is a better way to invoke a build command (make is here as an example. It could be node or go or maven). Or if there is a better way to achieve being able to pass arbitrary build command that would be helpful too.

3

I’d recommend an <expr> mapping—make the following changes:

nnoremap <expr> <Leader>b BuildSystemTask()

And then in your function:

if inProject()
  return ':make! '
else
  return ''

An alternative would be to use the feedkeys() function

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.