After migrating my dotfiles over to Mac M3 I stumbled into roadblock issue for my neovim setup. Every time I saved a PHP file, a lot of garbled data got prepended at the beginning of said file. If you remember so far back, my setup uses prettier for formatting everything, mostly via coc-prettier, but since @prettier/plugin-php is just a plugin anot not part of the mainline, I use vim-prettier specifically only for .php files. The setup works well for my needs, required little to no tweaking once it gotten to place.

So back to the garbage, it looked exactly like this:

␛]4;1;rgb:cc/24/1d␇␛]4;2;rgb:98/97/1a␇␛]4;3;rgb:d7/99/21␇␛]4;4;rgb:45/85/88␇␛]4;5;rgb:b1/62/86␇␛]4;6;rgb:68/9d/6a␇␛]11;rgb:28/28/28␇␛]10;rgb:eb/db/b2␇␛]4;0;rgb:28/28/28␇␛]4;7;rgb:a8/99/84␇␛]4;8;rgb:92/83/74␇␛]4;9;rgb:fb/59/34␇␛]4;10;rgb:b8/bb/26␇␛]4;11;rgb:fa/bd/2f␇␛]4;12;rgb:83/a5/98␇␛]4;13;rgb:d3/86/9b␇␛]4;14;rgb:8e/c0/7c␇␛]4;15;rgb:eb/db/b2␇␛]4;236;rgb:32/30/2f␇␛]4;234;rgb:1d/20/21␇␛]4;235;rgb:28/28/28␇␛]4;237;rgb:3c/38/36␇␛]4;239;rgb:50/49/45␇␛]4;241;rgb:66/5c/54␇␛]4;243;rgb:7c/6f/64␇␛]4;244;rgb:92/83/74␇␛]4;245;rgb:92/83/74␇␛]4;228;rgb:f2/e5/bc␇␛]4;230;rgb:f9/f5/d7␇␛]4;229;rgb:fb/f1/c7␇␛]4;223;rgb:eb/db/b2␇␛]4;250;rgb:d5/c4/a1␇␛]4;248;rgb:bd/ae/93␇␛]4;246;rgb:a8/99/84␇␛]4;167;rgb:fb/49/34␇␛]4;142;rgb:b8/bb/26␇␛]4;214;rgb:fa/bd/2f␇␛]4;109;rgb:83/a5/98␇␛]4;175;rgb:d3/86/9b␇␛]4;108;rgb:8e/c0/7c␇␛]4;208;rgb:fe/80/19␇␛]4;88;rgb:9d/00/06␇␛]4;100;rgb:79/74/0e␇␛]4;136;rgb:b5/76/14␇␛]4;24;rgb:07/66/78␇␛]4;96;rgb:8f/3f/71␇␛]4;66;rgb:42/7b/58␇␛]4;130;rgb:af/3a/03␇<?php

// rest of the file ...

Here a verbatim output of the ChatGPT that explains what the sequences mean, as I could not provide such information by heart, not even produce a relevant search query:

  • ]4;N;rgb:RR/GG/BB ␇ sets the color for palette entry N to the specified RGB color
  • ]10;rgb:RR/GG/BB ␇ sets the foreground (text) color
  • ]11;rgb:RR/GG/BB ␇ sets the background color

And examples of the above:

  • ]4;1;rgb:cc/24/1d ␇ sets color 1 to RGB(204, 36, 29)
  • ]10;rgb:eb/db/b2 ␇ sets the text color to RGB(235, 219, 178)
  • ]11;rgb:28/28/28 ␇ sets the background color to RGB(40, 40, 40)

Pinpointing the issue

To be fair, I have spent quite a lot of time pinpointing this issue, but it was quite fun, to be honest. Part of my switch from Arch to Mac was changing zsh to fish shell. I cannot tell right now if that was a good or a bad decision, and also if the timing was correct, but this probably deserves a separate article.

First thing I tried was getting sure the issue is somehow really connected to prettier as it was only my unverified thought at this point. After commenting out my "autosave" line in my ~/.config/nvim/init.vim:

autocmd BufWritePre *.php PrettierAsync

The garbage was not present at the beginning of the file, but running :PrettierAsync in neovim manually produced it again. As you can see, this made the prettier for PHP completely unusable. Thus I tested if native Prettier formats did the same. But the result was negative. Fixing formatting for example markdown .md file did not produce any garbage. This made me pretty confident that the issue lies in plugin-php, thus I tried it manually:

npx prettier --write file.php

To my great surprise, there was no garbage in such file. I was out of lead at this point, so I went on a looked for any open issues in these tools, but nothing produced any results. Also, as I said already, it is very hard to produce relevant search query for such issue.

Execute!

After poking around a little longer, I tried running the above directly in neovim via execute using :!

:!npx prettier --write %

This produced a surprise as the exact same garbled data were in the result (and not yet in the file). I had another hint, but still not sure what exactly was causing the issue. I still thought plugin-php was the culprit at this point, results of my tests:

  • neovim with my config in fish produced issue, but in zsh and bash tools
  • neovim with -u NONE (no extra config) produced the issue in all three shells
  • pure vim in any shell did not produce the issue, another hint

Since plugin-php did not produce issue via npx directly, nor in vim and only in neovim, I shifted my focus there. I was still clueless as running it in any shell and even without my config produced the issue. There was no recent neovim update, I was running v0.9.5 for some quite time, even though v0.10.0 was released three days ago, I have not yet updated there as there are some breaking changes announced and I could not be sure if they affect me or not. Better wait a week or two for the most obvious bugs get ironed out.

The lead

I have no idea how it occured to me, maybe out of desperation, but I tried running the following:

:!ls

This produced the same garbage before the actual ls output in neovim. Gotcha, the problem did not lie in prettier nor plugin-php at all! Now funnily the search term vim dumb shell got me to this stackoverflow answer that had an actual solution to my problem.

Adding this statement into my init.nvim:

set shell=/bin/bash

Produced no garbage. I believe the problem is in fish shell color formatting. Neovim probably reads the default shell and uses it inside, no matter which shell it is being run from. Enjoy!