Skip to content

Commit 0a76172

Browse files
committed
Format JSON on save with jq (indent with 2-spaces)
1 parent 98dd4f6 commit 0a76172

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

nvim/.config/nvim/init.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,15 @@ vim.api.nvim_create_autocmd("BufWritePre", {
257257
})
258258
end
259259
})
260+
--- set autoformatting on file save for json with jq
261+
vim.api.nvim_create_autocmd("BufWritePre", {
262+
pattern = "*.json",
263+
callback = function()
264+
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
265+
vim.cmd("%!jq --indent 2 .")
266+
pcall(vim.api.nvim_win_set_cursor, 0, { row, col })
267+
end,
268+
})
260269
--- create directory when creating file (if not created already)
261270
vim.api.nvim_create_autocmd("BufWritePre", {
262271
group = vim.api.nvim_create_augroup("auto_create_dir", { clear = true }),

0 commit comments

Comments
 (0)