I want to define <C-s>
to save current editing file in any mode.
map <C-s> :call StripWhitespace()<CR>
works fine in normal mode. But if I'm in insert mode, always need to Esc
before saving.
Tried below methods, but failed.
imap <C-s> :call StripWhitespace()<CR>
function! My_Force_Save()
if mode() != 'n'
exe "normal! \<esc>"
endif
call StripWhitespace()
endfunction
function! StripWhitespace ()
let save_cursor = getpos(".")
let old_query = getreg('/')
:%s/\s\+$//e
call setpos('.', save_cursor)
call setreg('/', old_query)
exe ":w!"
endfunction