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
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.
-
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– B_old2019-02-26 15:33:54 +00:00Commented Feb 26, 2019 at 15:33