The cleanest way is to not reuse Evil-specific commands or keystrokes unless absolutely necessary. Looking at the definition of :w
, which happens to be the evil-write
command, one can see that it parses the file name argument and calls shell-command-on-region
if it starts with a bang. Here's the equivalent specialized command:
(defun my-evil-run-python-region (beg end)
(interactive "r")
(if (region-active-p)
(shell-command-on-region beg end "python")
(shell-command-on-region (point-min) (point-max) "python")))
(global-set-key (kbd "<F5>") 'my-evil-run-python-region)