Whenever I open a file using emacs without gui (emacs --no-window-system) it modifies the file with the following text:


This text is inserted at the beginning of the file. How can I debug what is happening? How to prevent this from happening?


This problems appears only when using SSH.


The error appears in two cases CentOS 5 and CentOS 6.

emacs version 24.5.2

The command I'm using to open the files is: emacs --no-window-system file.txt

The problems happens even when disabling all customizations (How do I troubleshoot Emacs problems?), e.g.: it persists when using (emacs --no-window-system -Q file.txt).

  • Just for knowing, does it work normally when you type emacs -nw file.txt? – Nsukami _ Nov 14 '16 at 10:51
  • In some themes (or snippets) there are conditionals to use in terminal vs GUI. It looks like one of them fails to set color and writes to a file instead. 1) If you use theme, try disabling it and if you don't look through your config and check if you have color-related commands there. Look closely for black. (000000) 2) Try looking at Messages buffer, there might be some warnings there. – Roman Grazhdan Nov 14 '16 at 16:24
  • @Nsukami_ Same issue happens with emacs -nw file.txt. – rph Nov 15 '16 at 8:29
  • @RomanGrazhdan Yes, I'm using themes. Thanks for this suggestion. I'm trying to figure this out. – rph Nov 15 '16 at 8:30
  • @RomanGrazhdan Even disabling customization the error persists (using -Q flag). – rph Nov 15 '16 at 8:37

Some general debugging tips

Try to get the debugger invoked when something tries to insert this text. This will give you a backtrace which should at least let you know what is doing the inserting, and hopefully that will in turn lead you towards how to work around the bug. Locate a read-only file read-only-file.txt (for example an OS file) and run

emacs --eval '(setq debug-on-error t debug-ignored-errors nil)' -nw read-only-file.txt

You should also determine whether the issue is with Emacs startup or with file opening. If you run plain emacs -nw, is the text inserted somewhere? If you open another file, is the text inserted into it as well? In the latter case, you can set debug-on-error and debug-ignored-errors after starting Emacs and open any file read-only with M-x view-file.

If you have trouble locating which package or which part of the init file is responsible for something, the tips in How do I troubleshoot Emacs problems? and How to debug startup problem, if `--debug-init' has no effect should help.

What is causing this

Given the text that's inserted, there's a good chance that the above generic tips will be unhelpful because they'll point at self-insert-command. The text looks like a response from a terminal when an application sends the escape sequence to query the terminal's color configuration. Normally, Emacs should send \e]12;?\a (where \e is the escape character, octal code 033, and \a is the bell character, octal code 007, which can be replaced by character 034), and the terminal replies \e]12;rgb:0000/0000/0000\a to say that the cursor color is black. It seems that something here is going wrong: Emacs is sending the color query command but not reading the response back. Your terminal configuration may be incorrect, or it may be a bug in the terminal code.

Check that the value of TERM is correct. If you have any code or package that tries to talk to the terminal directly, review it. Check in particular the package whose name is the same as the value of TERM — see the manual section about terminal-specific initialization.

Unfortunately, I can't think of a simple way to set a breakpoint where something writes to the terminal. If you can't find the source of the problem on your own, there are some complicated ways, such as running Emacs under GDB, or running Emacs under an Expect script that sends SIGUSR2 when it sees the escape sequence in question.

To facilitate debugging, note that given the probable cause, it's likely that this text will be inserted every time you open a new terminal, so if you run the Emacs server you can set things up in an Emacs session and attach to a new terminal every time you want to test. This may help to debug: set a breakpoint at the beginning of the terminal setup function, and step through it until you find where it causes the problem (but note that the debugger's input/output may change the behavior of that part of the code).

  • I notice that this issue only appears if I'm launching emacs through SSH. The TERM variable is set to xterm. – rph Nov 15 '16 at 8:44
  • Indeed it was an issue with my terminal. I installed a new version of the terminal I was using and it fixed. – rph Nov 15 '16 at 9:05

Upgraded my mobaXterm ( from release 7 to 9.4 ) and this also fixed the "12;rgb:0000/0000/0000" problem for me.

  • same happens to me with mobaXterm 7.6 will upgrade now – Simon D. Feb 3 '20 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.