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 An External Process Every Time You Save A File In Neovim

TODO: Pull subtitle into page object
code_start_default_section code_end_default_section

Notes

Debugging Stuff

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

        -- title

Run An External Process Every Time You Save A File In Neovim

-- todo

[] thing here

[] another thing


-- code/
-- lua

vim.api.nvim_create_autocmd("BufWritePost", {
  group = vim.api.nvim_create_augroup("DoTheThingGroup", { clear = true }),
  callback = function() 
    vim.fn.jobstart(
      {"bash", "-c", "sleep 2 && echo Hello"},
      {
        stdout_buffered = true,
        on_stdout = function(_, data) 
          if data then
            print(data)
          end
        end
      }
    )
  end,
})

-- /code

-- code/
-- lua

-- NOTE: This is the original code that uses
-- plenary. Trying it above with the built-in
-- jobstart. If that works, this will be removed

local do_the_thing = function()

  local Job = require 'plenary.job'
  Job:new({
    command = 'bash',
    args = { '-c', 'echo "Doing The Thing"'},
    on_exit = function(job, return_val)
      print(vim.inspect(job:result()))
    end,
  }):start() 

end

-- /code

-- notes

- This uses `BufWritePost`lua` to pickup when files
are saved and then runs whatever is in the callback

- I tried `callback = do_the_thing()`lua` directly but
it didn't work without the wrapper `function()`lua`

- This runs the command asycn and prints the
response when it finishes

- There's no error handling in this example, that
would depend on the response in `on_exit`lua`

- TBD on accessing STDERR from on_exit


-- categories
-- Neovim 
-- Lua

-- metadata
-- date: 2023-10-19 01:43:55
-- id: 2wya29ct
-- site: aws
-- type: post
-- status: published