Okay, number 1 was easy, just run the command. I have never used that because I always had a terminal handy (and with focus follows mouse very easy to activate).
And I always did copy and paste, of course, middle button.
But now I found in kate the function “send selection to terminal”, mapped it to a key, and it does send. And the terminal keeps running, so I incrementally add functions and tests to the running program shell (REPL in other languages). It is like copy with a shortcut paste - it simply is faster.
Your code snippet always starts a new shell with the selection as input, thank you, noted.
I suspect Kate may be giving you a more “targeted” solution, whereas ours is more low level in that it’s up to you to define how the command executes exactly. So for our use-case you need to keep in mind that once a command ends your terminal will close. That’s why my example command has ;exec bash at the end.
Be sure to also check out the Shell scope, if you use Komodo IDE. It allows you to run terminal commands as well.