Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Run Synchronous And Asynchronous External Commands In Neovim With Lua

I need to write this up but here's the code in the mean time. It uses plenary which is a collection of helper functions. Installation instructions are on that page.)

Async

This is the one I usually want since it doesn't lock up the interface.

lua
local asynchronous_command_example = function()
  local Job = require 'plenary.job'
  Job:new({
    command = 'bash',
    args = { '-c', 'sleep 3; echo ASYNC_COMPLETE'},
    on_exit = function(job, return_val)
      print(vim.inspect(job:result()))
    end,
  }):start() 
end

This is the synchronous version that does lock up neovim while it's running.

lua
local synchronous_command_example = function()
  local Job = require 'plenary.job'
  Job:new({
    command = 'bash',
    args = { '-c', 'sleep 3; echo SYNC_COMPLETE'},
    on_exit = function(job, return_val)
      print(vim.inspect(job:result()))
    end,
  }):sync() 
end

References

Run an external command from Neovim without locking up the interface. Or, do. It's up to you.