Using python.el and ipython.exe (from Miniconda3), I'd like to highlight a portion of code and execute it in the associated inferior python buffer. Example:


myvar = 100+200
        ^^^^^^^ (this is my selected region)

If I use C-c C-c, it correctly sources the file. That's not what I want.

If I highlight the region as depicted above and use C-c C-r (python-shell-send-region), the entire line is executed, not just the region. That is, myvar should not be defined by this step. I'd like to send just the highlight region.

So I created an edited version of python-shell-buffer-substring, omitting the part that redefines start:

  (let* ((start (save-excursion
                  ;; Normalize start to the line beginning position.
                  (goto-char start)

I also create my-python-shell-send-region, where the only difference is it calls my-python-shell-buffer-substring. This properly creates the substring

# -*- coding: utf-8 -*-


and sends it correctly to the inferior process. Except ... the output is inhibited (generally a good thing), and I'd like to see the output 300. I see the function python-shell-send-string-no-output, but I don't see a way to allow output when using *-send-string.

Q: how to allow the output, and/or is there a better mechanism for doing this?

(In the unrelated package ess, I use C-u C-c C-r for show-no-output, and C-c C-r when I want the output shown. I can deal with a reversal of this logic, that's not the issue for this question.)

(emacs-25.2.1 (x86_64-w64-mingw32) and ein-20171128.516)


After some more digging (thanks wvxvw), I found that removing the inserted # -*- coding: utf-8 -*- line from the pasted string allowed the inferior process to print the output, but only on single-line regions. For instance:


myvar = 100+200

Highlighting just 100+200 produced the output 300, good. Highlighting from 100 through 300 on the next line, however, produced no output. If the highlighted string is on a single line and contains semicolon-delimited commands (e.g., 100+200;200+300), then the output from the last command is shown, nothing else (which is, I believe, correct behavior for python.) So the problem remains with multi-line regions.

  • The best I can think of so far is if you add your function to the list comint-output-filter-functions, then it will be called with the output that would be sent to the comint buffer, our you could entirely bypass comint and get hold of the process associated with the buffer, temporarily modify its filter-output-function to collect the results, and then restore it to the previous state.
    – wvxvw
    Nov 29, 2017 at 12:34
  • Thank you for your suggestions. Though I couldn't get it to work, it did cause me to find a culprit ... whether it is the best/only solution is tbd.
    – r2evans
    Nov 29, 2017 at 16:25


Your Answer

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

Browse other questions tagged or ask your own question.