Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

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.

Code

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.

Code

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
  • https://github.com/nvim-lua/plenary.nvim

  • https://doriankarter.com/inspect-contents-of-lua-table-in-neovim/

  • An alternate way in post: 2nhsdbjkdvpq

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