How to run external command in neovim on BufWritePre set in Lua

1k Views Asked by At

I would like to format my code using /bin/black everytime I save a python file in neovim. So I have the following in my ~/.config/nvim/init.lua

a.nvim_create_autocmd( { "BufWritePre" }, {
  pattern = { "*.py" },
  command = [[ /bin/black ]],
})

but I get the error like:

E492: Not an editor command:  /bin/black

Could someone let me know how can I run black (an external command) in BufWritePre set.

Cheers, DD.

1

There are 1 best solutions below

0
On

sorry for the question and I got it.

it command should be

a.nvim_create_autocmd( { "BufWritePre" }, {
  pattern = { "*.py" },
  command = [[ !/bin/black % ]],
})

where ! for external command and % for current file.