Last active
November 9, 2024 21:01
-
-
Save romainl/d2ad868afd7520519057475bd8e9db0c to your computer and use it in GitHub Desktop.
Formatting without moving
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I looked at it but couldn't understand how to use it. I will leave a comment asking for an example.