From fb441bc3df079f41725f0703615ea8dd438293e6 Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Fri, 22 Mar 2024 20:44:57 -0400 Subject: [PATCH] nvim: black-format python files on write --- src/.config/nvim/lua/coding.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/.config/nvim/lua/coding.lua b/src/.config/nvim/lua/coding.lua index a5acf03..b2e7439 100644 --- a/src/.config/nvim/lua/coding.lua +++ b/src/.config/nvim/lua/coding.lua @@ -4,6 +4,28 @@ confutil = require("confutil") keymap = confutil.keymap +------ +-- python format-on-save +-- https://stackoverflow.com/a/77467553 +------ + +-- to turn this off for a session (permanently), run +-- :autocmd! AutoFormat +-- https://superuser.com/a/1415274 +vim.api.nvim_create_augroup("AutoFormat", {}) + +vim.api.nvim_create_autocmd( + "BufWritePost", + { + pattern = "*.py", + group = "AutoFormat", + callback = function() + vim.cmd("silent !black --quiet %") + vim.cmd("edit") + end, + } +) + ------ -- syntax highlighting