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.

Automatically Execute Anything In Nvim - TJ DeVries (11min)

Nice quick video showing the basic of how to setup automation with Neovim autocmd

- Get the Neovim buffer number for the current window with :

: echo nvim _ get _ current _ buf()

- The primary way to set lines in buffers is

local bufnr = 41 vim.api.nvim _ buf _ set _ lines(bufnr, 0, - 1, false {"hello", "world" })

Where "bufnr" is the buffer number. "0" is the start line and " - 1" is the last line, false just needs to be there, and the last thing are the lines too put in.

- to add something at the start of the file (and push everything else after it) use "0" instead of " - 1" for the third parameter :

vim.api.nvim _ buf _ set _ lines(bufnr, 0, 0, false {"hello", "world" })

- Auto commands are how to react to events in Neovim.

- BurWritePost is what is triggered after a buffer is written. (Looking at this for auto executing embedded rust scripts)

- This is what a basic setup for an autocommand looks like (note that I typed this in so there may be typos that I haven't caught yet)

vim.api.nvim _ create _ autocmd("BufWritePost", { group = vim.api.nvim _ create _ augroup("TutorailGroup", { clear = true }), pattern = "main.txt", callback = function() print("did the thing") end, })

- without [TODO: Code shorthand span ] in there, that would execute on every file save

- Using the [TODO: Code shorthand span ] prevents listeners from being added over and over again when you same the lua source files that contain them

- Running external commands is done with [TODO: Code shorthand span ]

(note : I typed this code in by hand and don't have a way setup to test it yet so it still needs to be vetted)

vim.fn.jobstart( {"go", "run", "main.go"}, { stdout _ buffered = true, on _ stdout = function( _ , data) if data then vim.api.nvim _ buf _ set _ lines(bufnr, - 1, - 1, false, data) end end, on _ stderr = function( _ , data) if data then vim.api.nvim _ buf _ set _ lines(bufnr, - 1, - 1, false, data) end end } )

Footnotes And References