Skip to content

Instantly share code, notes, and snippets.

@romainl
Last active November 9, 2024 21:01
Show Gist options
  • Save romainl/d2ad868afd7520519057475bd8e9db0c to your computer and use it in GitHub Desktop.
Save romainl/d2ad868afd7520519057475bd8e9db0c to your computer and use it in GitHub Desktop.
Formatting without moving
" gq wrapper that:
" - tries its best at keeping the cursor in place
" - tries to handle formatter errors
function! Format(type, ...)
normal! '[v']gq
if v:shell_error > 0
silent undo
redraw
echomsg 'formatprg "' . &formatprg . '" exited with status ' . v:shell_error
endif
call winrestview(w:gqview)
unlet w:gqview
endfunction
nmap <silent> GQ :let w:gqview = winsaveview()<CR>:set opfunc=Format<CR>g@
@mroavi
Copy link

mroavi commented Aug 21, 2022

I looked at it but couldn't understand how to use it. I will leave a comment asking for an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment