[mini.trailspace] How to setup automatic trim on write IF there are any whitespaces? #309
-
Hello folks! I managed to set an autocmd but I couldn't figure out how to check if there were trailing whitespaces on the buffer. I'd guess I could do the same check myself but It'd be great if I could use anything in the module to check that instead of rewriting some of the logic. vim.api.nvim_create_autocmd({ "BufWritePre" }, {
callback = function()
vim.fn.confirm("Trim Whitespaces? [Y/n]", "&y\n&n, 1)
-- Conditional to check the option (Didn't do it since I was focusing on the trigger first
-- if choice == 1then
-- require("mini.trailspace").trim()
-- require("mini.trailspace").trim_last_lines() Thanks (= |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi! There is no way to check any of that with 'mini.trailspace'. You can write custom functions for that: local has_trailspace = function(buf_id)
buf_id = buf_id or vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(buf_id, 0, -1, true)
for _, l in ipairs(lines) do
if l:find('%s$') then return true end
end
return false
end
local has_trail_lines = function(buf_id)
buf_id = buf_id or vim.api.nvim_get_current_buf()
local n_lines = vim.api.nvim_buf_line_count(buf_id)
local last_nonblank = vim.api.nvim_buf_call(buf_id, function() return vim.fn.prevnonblank(n_lines) end)
return last_nonblank < n_lines
end Now you can add something like |
Beta Was this translation helpful? Give feedback.
Hi!
There is no way to check any of that with 'mini.trailspace'. You can write custom functions for that:
Now you can add something like
if not (has_t…