0

Can python.el be set up in a way that it will automatically save the current buffer when python-shell-send-buffer is called (e.g. by C-c C-c)?

1 Answer 1

0

It sure can be!

First, create a function that saves the buffer before calling python-shell-send-buffer:

(defun my-python-shell-send-buffer ()
  "Save the current buffer before calling `python-shell-send-buffer'."
  (interactive)
  (save-buffer)
  (python-shell-send-buffer))

The interactive line allows you to call the function using M-x. I suspect the other lines are self-explanatory. If not, read about them using C-h f.

Next, override C-c C-c in python-mode with the new function:

(define-key python-mode-map (kbd "C-c C-c") 'my-python-shell-send-buffer)

After executing each of these lines, when you reload python-mode, C-c C-c will work as you desire. Put these lines in init.el and this will be the new behavior for python-mode.

1
  • Cool, thanks! I had to make a minor adjustment, though, in order to avoid an error message: (add-hook 'python-mode-hook (lambda () (define-key python-mode-map (kbd "C-c C-c") 'my-python-shell-send-buffer))) Commented Feb 26, 2019 at 15:33

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.