3

I pressed some combination of letters by mistake and part of my Python buffer got a purple background color (or magenta, fuschia, violet, orchid, depending on knowledge of colors).

Region of text turned purple

The only way I found to undo it is to restart Emacs. How can I undo it otherwise?

  • 3
    This looks like it could be a misfired secondary selection, though its color is yellow by default. Does clicking the left mouse button while holding Alt make it go away? – undercat supports Monica Mar 31 at 14:49
  • 2
    You can use what-cursor-position C-u C-x = while cursor stay in the area. It will tell you what font face that area is using right now, then you can find out what feature you enabled. – Rangi Lin Mar 31 at 15:51
  • @RangiLin That C-u C-x = command with point inside the purple area shows display: by this font (glyph code) and no further font information. The problem was isearch-yank-kill, as I mentioned in a comment to the accepted answer. – miguelmorin Jun 3 at 17:44
  • @undercat Clicking the left mouse with Alt down shows a new region with yellow background and does not make the purple area go away. The problem was isearch-yank-kill, as I mentioned in a comment to the accepted answer. – miguelmorin Jun 3 at 17:46
7

There's too little information in the question to be sure, so I can't definitively answer what happened. Rather I'll show how to investigate this and give some plausible cases.

I pressed some combination of letters by mistake

Once you realize that, press F1 l or C-h l (that's lowercase ell) to invoke the command view-lossage. This displays the last 300 keys you pressed as well as other input events such as mouse clicks. Since Emacs 25, this also shows the commands that they invoked. In earlier versions, you have to use C-h k to find what command a key or key sequence invokes, and that can give wrong results if you've changed some setting or moved to some buffer since that event, or for mouse events if you click in a different place.

If Emacs is behaving strangely, check the mode line (the line just above the bottom line, or at the bottom of the current window, that displays information such as the file name and line number) and the minibuffer (the line at the bottom where commands prompt for input). Maybe you switched to the buffer while some command (e.g. a search) was in progress? If the mode indication in the mode line has square brackets around the parentheses (e.g. [(Python …)] instead of the normal (Python …)), it means that a recursive edit is in progress, i.e. some command lets you edit the buffer. To exit the recursive edit and resume the command that started it, press C-M-c. To exit the recursive edit and abort the command that started it, press C-] or ESC ESC ESC. If there's a prompt in the minibuffer, click there to switch back to that prompt.

If you want to know why a particular character is in a particular color or font, move the cursor to that character and run M-x describe-text-properties. This command is also available from the menu ‘Edit’ → ‘Text Properties’ → ‘Describe Properties’ or C-mouse-2 ‘Text Properties’ → ‘Describe Properties’. Typically text formatting is done through faces and the face's documentation should at least indicate which package is responsible for it.

The command M-x list-faces-display (available as ‘Display Faces’ in the ‘Text Properties’ menu) shows the available faces. With the default light background theme, two faces use a purple background that matches the image in your question: isearch and query-replace, both used to highlight matching text during a search command. I don't know how you could have reached that my mistake, but based on the colors, it's the most plausible scenario.

Emacs has a built-in package to highlight things temporarily. It leaves a discrete clue in the modeline: the minor mode indicator Hi.

Although anything could happen depending on what packages you have installed, highlighting is likely to be a per-buffer configuration. So if you can't figure out what's going on, it should be enough to close the file and reopen it.

  • I will try the view-lossage hint next time I have the same problem and add another comment. In the meantime, I am accepting this answer because of the ability to trace this and any other problem from a mistake in keys and because of the range of possible explanations. – miguelmorin Apr 17 at 16:31
  • This happened again and thanks to view-lossage, I found it was due to an isearch-yank-kill or s-e, Cmd-E on mac, which I probably pressed instead of Control-E. – miguelmorin May 17 at 16:55
  • describe-text-properties showed There is an overlay here: ... face isearch, priority 1001. You were right that it was the isearch background, well done! – miguelmorin Jun 3 at 17:42
1

The face of the highlighted text could be face secondary-selection, although that has a yellow background by default.

You can tell what the face is by putting your cursor on a highlighted character and hitting C-u C-x =. Near the bottom of buffer *Help* describing that character it tells you what face is used.

If that's the case then you probably used the M- (Meta key) modifier while dragging the mouse with the first mouse button (<mouse-1>) pressed, and then released. (That makes the secondary selection empty.)

To get rid of it just hold the Meta key and click <mouse-1> (without dragging it).

(If this is in fact the answer then please add tags secondary-selection and mouse to the question.)

  • The C-u C-x = command with point inside the purple area shows display: by this font (glyph code) and no further font information. Holding the Meta key and clicking <mouse-1> did not make the area go away. The problem was isearch-yank-kill, as I mentioned in a comment to the accepted answer. – miguelmorin Jun 3 at 17:45
1

On second thought, I'm guessing that you used Isearch, query-replace or hi-lock. The default face for Isearch and query-replace, face isearch, looks just like the highlighting you show.

Isearch highlighting appears when you search using C-s, C-r, C-M-s, or C-M-r. Query-replace highlighting appears when you use use M-% or C-M-%. Normally this search highlighting disappears when you're done with the command, but things can get "stuck".

Hi-lock highlighting appears when you use a hi-lock command, such as hi-lock-line-face-buffer or hi-lock-face-buffer (aka highlight-regexp). Hi-lock commands are on prefix key C-x w, by default.

Such commands either read a face name or automatically use the next face. If you previously used Isearch then the face chosen could easily be face isearch, which looks like what you show. Hi-lock highlighting persists after the command that causes the highlighting. My guess would be that you used hi-locking, perhaps without realizing it.

If you see submenu Regexp Highlighting in the menu-bar Edit menu then hi-lock-mode is on, and this is probably the explanation. To get rid of hi-lock highlighting you can usually just turn off hi-lock-mode by repeating that toggle command.

  • Yes, it was isearch, which I enabled with Cmd-E. describe-text-properties showed There is an overlay here: ... face isearch, priority 1001. – miguelmorin Jun 3 at 17:43

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.