I have key mapping in my ~/.vimrc file that re-indents edited source code on the fly. It looks like as follows:
" press F4 to fix indentation in whole file; overwrites marker 'q' position noremap <F4> mqggVG=`qzz inoremap <F4> <Esc>mqggVG=`qzza
mq place marker 'q' at cursor position ggVG select all text = re-indnet text `q return cursor back to position stored in 'q' marker zz center the display over the cursor a return to insert mode if called from it
It basically works, but has two shortcomings.
The first one is that it overwrites the q marker. I used this marker to store cursor position. I choose q because it is very unlikely that I would use this letter as a marker. Despite this, is there any more clever approach to achieve this, without destroying q marker?
The second one occurs in the insert mode, when the cursor is on the beginning of a line. In such condition, F4 re-indents as expected, but also moves cursor one position right. I tried to fix it by using
<C-o> instead of
<Esc>, but it looks like
<C-o> is applicable only to editor :commands, not to move commands. How can I fix it?