Letter g added to beginning of line when editing in VIM and using XTERM (Cygwin mintty from windows)

75 Views Asked by At

Mysteriously a letter g would get added to the beginning of the line when first opening up a file to edit with VIM when using an XTERM terminal on Windows onto a Linux host.

The bug has something to do with xterm passing in the terminal colors into vim but vim ends up trying to read "rgb" and ends up running ":rg" which replaces the beginning of the line with a letter g upon startup

In my case I am using Cygwin mintty set to use terminal XTERM.

Searching around, I found that this is a bug between XTERM and VIM and some sites had mentioned it's a transparency bug and putting "set background=dark" in a .vimrc would fix the issue. This did fix the issue on VIM 7.4 for me on RHEL7 Linux hosts. However, recently I started using RHEL9 hosts with VIM 8.2 and the "set background=dark" no longer worked and I'd still get letter g's inserted automatically. After trying a bunch of things, I finally found that using "set compatible" in .vimrc will fix the issue and the reason I have to do that is that VIM 8.X no longer has vi compatible enabled by default like what happens in VIM 7.X.

Has anyone else had similar issues that you resolved this in another way?

Am I going to lose anything important using "set compatible"? Seems like arrow keys, block text, etc still all works.

1

There are 1 best solutions below

0
native0ne On

Looking through :help 'compatible' and experimenting a bit more, it seems like losing escape keys is the biggest impact to me. When you are in insert mode you can no longer use arrow keys to move around. I tested to see what happens if I take set compatible back out and just have set noesckeys and the "g" bug doesn't happen. So those escape keys are related to the bug it seems.

As a super hacky workaround I tried using an autocmd that asynchronously waits 100 ms and then reloads the file with :e! after starting Vim (to basically reverse out the inserted letter g) and that does the trick and leaves all my xterm functionality in place. Reloading the file seemed overkill though so I found a global undo function that I can use in VIM 8.1+ so I'm using :u0 to undo all changes instead of the :e! file reload.

Here's that auto command I ultimately added to .vimrc:

" upon opening the vim file, wait 100ms asynchronously and then undo all changes using :u0.  This is available in VIM 8.1+.  This gets rid of the letter g entry from the xterm/vim bug
autocmd VimEnter * call timer_start(100, { tid -> execute(':u0')})