I open Vim and add the following lines to a buffer (call this state INITIAL):
xx xx xxx xx xxxx xx xxxxx xxxxxxxx xxxxx xxxxxxxxxxx x xxxxx xxx xxxx xxx x
xx xxxxxx xxx xxxxxxx xxxxxx xx x xxx xx xxx xxxxxx xx xxxxxx xx xxx xxx xx
xx xxxxx xxxxxxx xxxxxxxxx xxxxxxxxx xx xxx xxxxx xxxx xx xx xxxx xxxx xxx xx
xx xxxx xx x xxxxxxxx xxxxxxxx xx xx xxxx x xxxxxxx xxx xxx xxx xxx xxx xxx xxx
xx xxx xxx xxx xxx xxxx
I then go to the end of the buffer and enter insert mode (GA
). I then type <C-O>gqip
. The buffer changes to (call this state A):
xx xx xxx xx xxxx xx xxxxx xxxxxxxx xxxxx xxxxxxxxxxx x xxxxx xxx xxxx xxx x xx
xxxxxx xxx xxxxxxx xxxxxx xx x xxx xx xxx xxxxxx xx xxxxxx xx xxx xxx xx xx
xxxxx xxxxxxx xxxxxxxxx xxxxxxxxx xx xxx xxxxx xxxx xx xx xxxx xxxx xxx xx xx
xxxx xx x xxxxxxxx xxxxxxxx xx xx xxxx x xxxxxxx xxx xxx xxx xxx xxx xxx xxx xx
xxx xxx xxx xxx xxxx
Still in insert mode, I type <C-O>gqip
again. The buffer changes to (call this state B):
xx xx xxx xx xxxx xx xxxxx xxxxxxxx xxxxx xxxxxxxxxxx x xxxxx xxx xxxx xxx x xx
xxxxxx xxx xxxxxxx xxxxxx xx x xxx xx xxx xxxxxx xx xxxxxx xx xxx xxx xx xx
xxxxx xxxxxxx xxxxxxxxx xxxxxxxxx xx xxx xxxxx xxxx xx xx xxxx xxxx xxx xx xx
xxxx xx x xxxxxxxx xxxxxxxx xx xx xxxx x xxxxxxx xxx xxx xxx xxx xxx xxx xxx
xx xxx xxx xxx xxx xxxx
Still in insert mode, I type <C-O>gqip
again. The buffer changes back to A. It may be hard to tell, but A and B are actually distinct. Still in insert mode, if I keep typing <C-O>gqip
, the buffer state keeps toggling between A and B. The state progression is therefore INITIAL → A → B → A → B → A → B → ... .
When I try the above in normal mode, the state progression is INITIAL → A → A → A → A → ... (which is what I would expect).
(All of the above works if I use gwip
, gqap
, gwap
in place of gqip
.)
I'm not sure if this is a bug or if there's some odd subtlety to Vim's commands that I've never noticed before. I thought gqip
was supposed to be idempotent no matter what mode I ran it from.
My question is: What is going on here?
(If you're wondering why all the text is x
s, it's because I first noticed this behavior while working on some confidential code, and then replaced everything with x
s so I could ask this question publicly.)
<C-o>
in insert mode is called "insert normal mode". FWIW #2, there is no promise of idempotency made anywhere in the doc. FWIW #3, I think you have found a bug… that you should report.