An Hour Well Spent
I spent a bout an hour at the office today getting my F6 mapped to to a vimscript function that tidies up the whitespace in my code.
Usually I map F6 to trim trailing whitespace, and F7 to replace tabs with spaces.
But today I decided that I was going to combine those two into a single TidyWS() vimscript function.
It turns out getting this to work cleanly was more challenging than I expected.
Long story short, here's how I did it.
Usually I map F6 to trim trailing whitespace, and F7 to replace tabs with spaces.
inoremap <F6> %s/\s+$//g <CR>
inoremap <F7> %s/\<TAB>/ /g <CR>
But today I decided that I was going to combine those two into a single TidyWS() vimscript function.
It turns out getting this to work cleanly was more challenging than I expected.
Long story short, here's how I did it.
function! TidyWS()
let l:save = winsaveview()
silent! %s/\s+$//g
silent! %s/\<Tab>/ /g
call winrestview(l:save)
echo "whitespace tidied"
endfunction
nnoremap <F6> :call TidyWS()<CR>
Comments