-- remove trailing whitespace function my_remove_trailing_whitespace() local curpos = vim.api.nvim_win_get_cursor(0) vim.cmd([[keeppatterns %s/\s\+$//e]]) vim.api.nvim_win_set_cursor(0, curpos) end vim.api.nvim_create_autocmd( { 'BufWritePre' }, { pattern = { '*.lua', '*.cpp', '*.hpp' }, callback = my_remove_trailing_whitespace } )