I have some python code, that has some long string literals containing more python code. These strings should most of the time be uniformly colored, as happens by default.

However, I would like to be able to some times narrow my buffer to any of these strings, and then have emacs syntax highlight them as if they were code, not strings within code (i.e. as if the """ weren't there in the original buffer)

Is there any way to do that, that does not involve lots of elisp?

NOTE: I do not need to necessarily need narrowing, just seemed better to change the syntax highlighting like that, as in the full buffer it would be confusing...

  • Wouldn't narrowing to only the contents of the string, not including the quotes, solve the problem? Commented Mar 9, 2016 at 15:28
  • 1
    A while ago I saw someone doing something similar in HTML mode, but right now I remember where I saw it. Anyway, the basic idea was to copy the embedded code to a new buffer and display it using the proper major mode. Once done, a magic key sequence (like C-c C-c) would copy the result back where it originally case from. Unfortunately, it would require writing some elisp, I'm afraid. Commented Mar 9, 2016 at 16:06
  • @JordonBiondo In my, narrowing to the string (without quotes) does not change highlighting... also running font-lock-fontify-buffer does not help
    – jmlorenzi
    Commented Mar 9, 2016 at 16:13
  • @Lindydancer Thanks! I thought something like that should be doable, but my elisp skill is really low... I hope I get some time to try and do this...
    – jmlorenzi
    Commented Mar 9, 2016 at 16:15
  • @jmlorenzi after you narrow you can change the major mode to support whatever highlighting you want. Then change it back once you widen. @lindydancer is talking about the string-edit package, but it would not support highlighting like you want. Commented Mar 9, 2016 at 16:15

1 Answer 1


This can be done with polymode.

(require 'polymode)
(defcustom pm-host/python
  (pm-bchunkmode "python"
                 :mode 'python-mode
                 :font-lock-narrow nil)
  "Python host chunkmode"
  :group 'hostmodes
  :type 'object)

(defcustom pm-inner/python
  (pm-hbtchunkmode "python"
                   :mode 'python-mode
                   :head-reg  "\"\"\""
                   :tail-reg  "\"\"\"")
  "Python typical chunk."
  :group 'innermodes
  :type 'object)

(defcustom pm-poly/python
  (pm-polymode-one "python"
                   :hostmode 'pm-host/python
                   :innermode 'pm-inner/python)
  "Python typical polymode."
  :group 'polymodes
  :type 'object)

(define-polymode poly-python-mode pm-poly/python)
  1. C-x b test.py RET
  2. M-x poly-python-mode RET

With this mode, triple-quoted strings are highlighted as python code. (Not normally a good idea, but you can toggle it).

enter image description here

Currently, polymode seems to have issues updating the font-locking. However, this should work in practice.

  • I hope this works for the OP, but for me it seems to work great! You say that you can toggle it though, but how do you do that? I can toggle it on, but can't figure out how to toggle it off...
    – elethan
    Commented Mar 10, 2016 at 2:42
  • @elethan, as I note at the end, there seems to be some trouble relating to the font-locking when toggling. I'm looking into it, and will likely produce an issue to see if it can be resolved.
    – PythonNut
    Commented Mar 10, 2016 at 3:38
  • But when I say I can't toggle it off, I mean I can't figure out the command. I understand if the syntax highlighting doesn't go away, but when I call poly-python-mode a second time, I still see PM in my mode-line, indicating it is still active
    – elethan
    Commented Mar 10, 2016 at 3:44
  • @elethan Ah, major modes don't toggle. ;) You'll need to use python-mode to return to the non-polymode.
    – PythonNut
    Commented Mar 10, 2016 at 3:45
  • OK, switching back to python-mode works like a charm! I didn't realize that I had ever left python mode...(BTW, I will award the bounty once the site allows me too. Thanks for the answer...even though it isn't my question, haha!)
    – elethan
    Commented Mar 10, 2016 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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