1

I am using Neovim and I want to go to insert mode at the end of this command.

vim.cmd([[command! DunB lua run_only_current_file("below") ]])

I have tried | startinsert and | :startinsert. but it did not work. The error says unexpected symbol near |. I also put an i at the end of the command but it typed the whole command in the editor. This is my function if it help:

function run_only_current_file(direction)
  local name=vim.fn.expand('%:t')
  local compiler="python" --defult
  local format=vim.fn.expand('%:e'):match('[^.]+$')
  if  format== 'py' then
    compiler="python"
  end
  if  format== 'js' then
    compiler="node"
  end
    vim.api.nvim_feedkeys(":" ..direction.. " split term://cd %:p:h &&" ..compiler.. " %:t\n","n", false)
end

6
  • try to use :norm! A Jan 17 at 19:13
  • You may need to :execute 'lua run_only_current_file("below")' | startinsert; :lua is not documented as consuming the bar, but it probably does (:python and :perl do)
    – D. Ben Knoble
    Jan 17 at 19:16
  • @D.BenKnoble No, :norm! A gets error : function arguments expected near '!' . and execute .... will type the content of the command in the editor. Jan 17 at 19:24
  • Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one of the answers. It allow the question to rest :-) Jan 22 at 3:57
  • @VivianDeSmedt Thanks, your answer is perfect. Jan 22 at 5:27

1 Answer 1

0

I see that you are using feedkeys.

I suspect that feedkey will somehow trigger the next action asynchronously.

  • Your function will first complete
  • Neovim will treat the keys that you send via feedkey

The problem you have is that the buffer you want to enter in insert mode do not exist yet at that moment.

If you could you should get rid of the feedkey and replace it by an execute command:

direction .. " split term://cd %:p:h &&" ..compiler.. " %:t"

If that doesn't work because Neovim refuse to change the layout within that command then you can complete your mapping with A

 vim.api.nvim_feedkeys(":" ..direction.. " split term://cd %:p:h &&" ..compiler.. " %:t\nA","n", false)

Otherwise you could trigger a timer to enter in Insert mode after the feedkeys have been consumed by Neovim.

1
  • 1
    I edited my question. Maybe it depends on my function. Jan 18 at 7:39

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.