home ~ projects ~ socials

An Alternate Way To Run An External Async Command In Neovim

This is a draft for an alt way to run an external command. I don't use it, I use plenary. See post 2ngtllokpfsv for the way I use.

Here's an example of how to setup a Neovim plugin to make an external async call.

local async_test_function = function()
  local output = ""
  local cmd = {'bash', '-c', 'sleep 2; echo "PING"'}
  vim.fn.jobstart(cmd, {
    on_stdout = function(j, d, e)
      output = output .. vim.fn.join(d)
      print(output)
    end
  })
end

Notes

  • The docs say you can use `[]` as the first argument to `jobstart()`. That didn't work for me. That example is for a .vim file which probably has something to do with it. I have yet to find one for .lua directly. This approach uses the local cmd {...} and then calls it as the first argument instead.
-- links

- docs
  https://neovim.io/doc/user/job_control.html

- reddit
  https://www.reddit.com/r/neovim/comments/tsevfp/what_function_should_i_use_in_neovim_lua_to_run/

- jobstart
  https://neovim.io/doc/user/builtin.html#jobstart()
-- end of line --