0

This answer how to map C-c ! to create a new python shell for the current buffer.

The main code is this:

(defun my-python-start-or-switch-repl (&optional msg)
  "Start and/or switch to the REPL."
  (interactive "p")
  (if (python-shell-get-process)
      (python-shell-switch-to-shell)
    (progn
      (run-python (python-shell-calculate-command) t t)
      (python-shell-switch-to-shell)
      )
    )
  )

As it can be seen, it uses run-python, so it only opens a shell that has the name of the buffer it was opened it from, e.g., if the program is foo.py, then the shell has the name: *Python[foo.py]*, but it doesn't run foo.py. Is there a way to have dedicated shells that by default run the program from which they being generated?

1 Answer 1

1
+100

You should be able to do what you need modifying your function like this:

defun my-python-start-or-switch-repl (&optional msg)
"Start and/or switch to the REPL."
(interactive "p")
(if (python-shell-get-process)
    (progn
      (python-shell-send-buffer)
      (python-shell-switch-to-shell)
      )
  (progn
    (run-python (python-shell-calculate-command) t t)
    (other-window 1)
    (python-shell-send-buffer)
    (python-shell-switch-to-shell)
    )
  )
)

The main change is the call to python-shell-send-buffer that sends the entire current buffer to the running Python process.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.