Apple - Make Ctrl-k in Terminal copy to the system clipboard

There's a very simple solution if you're willing to extend your shell's functionality. (Though it is pretty cool to see the automator solution)

Assuming you're using zsh, the current macOS default shell, then just add this to your ~/.zshrc profile.

pb-kill-line () {
  zle kill-line   # `kill-line` is the default ctrl+k binding
  echo -n $CUTBUFFER | pbcopy
}

zle -N pb-kill-line  # register our new function

bindkey '^K' pb-kill-line  # change the ctrl+k binding to use our new function

All this is doing is wrapping the Ctrl+k key binding so that in addition to performing its default behavior (kill-line), it also copies the relevant text into the system wide clipboard. The mysterious $CUTBUFFER contains the text that was just cut, and the macOS pbcopy utility puts whatever it receives from the STDIN into the system clipboard.

This solution was mostly just copied from https://gist.github.com/welldan97/5127861 ! My only original content is the explanation. :-)