Last active
April 29, 2025 23:04
-
-
Save romainl/d2ad868afd7520519057475bd8e9db0c to your computer and use it in GitHub Desktop.
Formatting without moving
This file contains hidden or 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@ |
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
Hi @mroavi, the issue happens even without vim-repeat so we can leave it out of the equation.
The simplest thing to do to prevent that message would be to check for the existence of
w:gqview
before doing anything with it:But that
w:gqview
is actually the most important part of the whole thing: "without moving". Without it, you still get the expected behaviour when usingGQ<motion>
explicitly but.
will leave the cursor at the end of the motion.Did you try @George-B's snippet?