The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Run Synchronous And Asynchronous External Commands In Neovim With Lua

TODO: Pull subtitle into page object

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

References

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Run Synchronous And Asynchronous External
Commands In Neovim With Lua

-- forward 

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.)

-- h2

Async


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

-- code
-- 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


-- h2

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

-- code
-- 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

-- h3

References

-- list

- https://github.com/nvim-lua/plenary.nvim

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

- An alternate way in post: 2nhsdbjkdvpq

-- blurb

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

-- categories
-- Neovim
-- Lua

-- metadata
-- date: 2023-03-20 01:16:01
-- id: 2ngtllok
-- site: aws
-- type: post
-- status: published