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)?


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'."

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.

  • 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)))
    – B_old
    Feb 26 '19 at 15:33

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.