"Unexpected error: Invalid byte sequence in conversion input" in gedit

I have a text file. I created the content of the text file by copying from some webpage into it. During the creation, I was completely under gedit. I then saved the file and exited gedit. But since then, I haven't been able to open the file in gedit (although I could open it in Emacs). I tried to open it in gedit, but fails because "Unexpected error: Invalid byte sequence in conversion input".

  1. How shall I open such a file successfully, if without changing the content of the file?
  2. I guess there are some "bad" characters in it, and want to see what they are and remove them if they are not important. How shall I identify those culprit characters?

Thanks!


I’ve been facing the same issue. For me the problem was that the encoding of the file had changed since it was last opened with Gedit (from WINDOWS-1252 to UTF-8). As I had manually set the encoding when first opening the file, Gedit had saved this information and tried to reuse it the next time I opened the file after the encoding change; this resulted in said error message.

To fix the problem, I had to remove the saved encoding information from the gvfs metadata like so:

gvfs-set-attribute --type unset MY_FILE.txt 'metadata::gedit-encoding'

You can always find out what Gedit has saved about a particular file:

gvfs-info MY_FILE.txt | grep 'metadata::gedit'

For example, before issuing the above command, the output for my file was this:

  metadata::gedit-encoding: WINDOWS-1252
  metadata::gedit-position: 0
  metadata::gedit-spell-language: de_DE

After I had removed the encoding attribute, the output was just this:

  metadata::gedit-position: 0
  metadata::gedit-spell-language: de_DE

type this in terminal

gsettings set org.gnome.gedit.preferences.encodings auto-detected "['UTF-8', 'CURRENT', 'ISO-8859-15', 'UTF-16']"