If you want to remove all trailing whitespace in a file in Neovim, use this command:
:%s/\s\+$//e
Explanation:
:%s→ substitute across the entire file\s\+$→ matches one or more spaces or tabs at the end of a line//e→ suppress errors if no match is found
Optional: Automatically remove trailing whitespace on save:
Vimscript version (init.vim):
autocmd BufWritePre * :%s/\s\+$//e
Lua version (init.lua):
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*",
command = [[%s/\s\+$//e]],
})
This ensures your files are always clean of trailing spaces.
You must log in or # to comment.
