-- highlights trailing spaces vim.cmd("match Search /\\s\\+$/") -- disable highlight when going into insert mode vim.api.nvim_create_autocmd({ "ModeChanged" }, { pattern = { "*:i" }, command = "match none", }) -- highlights trailing spaces when going back to normal mode vim.api.nvim_create_autocmd({ "ModeChanged" }, { pattern = { "*:n" }, command = "match Search /\\s\\+$/", }) vim.keymap.set( "n", "t", ":%s/\\s\\+$//g", { noremap = true, silent = true, desc = "Remove Trailing Whitespaces" } )