tpope / vim-surround

surround.vim: Delete/change/add parentheses/quotes/XML-tags/much more with ease
https://www.vim.org/scripts/script.php?script_id=1697
13.39k stars 444 forks source link

surround word with backticks inserts invalid utf-8 sequence #375

Closed ldegen closed 1 year ago

ldegen commented 1 year ago

Hi there,

something strange is happening to me when I use vim-surround with backticks (`).

I am using

I open a new empty buffer that only contains the word "word":

word

Then I place the cursor on the word and type ysiw`.

Expected result would have been

`word`

But instead I get something looking like

<80><fc>^B`word<80><fc>^B`

Now I saved this to a file /tmp/bla to look at the content byte-by-byte:

lukas@kikuchiyo:~$  hd /tmp/bla 
00000000  80 fc 02 60 77 6f 72 64  80 fc 02 60 0a           |...`word...`.|
0000000d
lukas@kikuchiyo:~$ LANG=C iconv -f UTF-8 /tmp/bla -o /dev/null;
iconv: illegal input sequence at position 0

Right, 0x80 I think is not allowed at the beginning of a UTF-8 sequence. But maybe this was never ment to be UTF-8 in the first place?

I suspect there might be some other plugin interfering, but I have no idea what to look out for. Any suggestions? Anything I should try to narrow this down?

tpope commented 1 year ago

That <80><fc>^B is how Vim internally represents the shift key for certain keys sequences. Example:

:call writefile(["\<S-CR>"], '/tmp/bla', 'b')
:!hd /tmp/bla
00000000  80 fc 02 0d                                       |....|
00000004

That's our sequence (80 fc 02) followed by a carriage return (0d).

I'm not sure why it's manifesting for a basic ASCII character, but my best guess is it's because your terminal is sending something instead of a literal backtick. So I would start by trying in a different terminal to confirm.

You could also try a different locale.

ldegen commented 1 year ago

Hi @tpope, just to confirm: yes, this seems indeed to be related to the terminal emulator I am using (xterm). I just tested this with kitty and there it works fine. Interesting. Will have a closer look at my xterm config then. Thanks for pointing me into the right direction. ;-)

orangenschalen commented 1 year ago

@ldegen : I'm having exactly the same problem and could not yet find the proper xterm option(?). Would you be so kind to document any found solution here?

ldegen commented 1 year ago

@orangenschalen: No luck so far. If I find something, I'll report back here.