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. :-)


I do not believe what you're asking is going to be easily doable. I believe the secondary cut and paste functions are isolated in each app that supports them and it would probably require programmatically trapping the target key sequence and processing accordingly. This is something that would probably need to be written in C and or Objective-C, compiled to executable and stay memory resident.

That said though, I believe that the ^K shortcut only cuts the last line of text from the position of the cursor and places it on the secondary pasteboard. So with that as the understanding, let me offer the following as a workaround solution.

The following example AppleScript code, shown further below, will be used in an Automator service, where one can then assigned a keyboard shortcut to the service. This service will use e.g. ⌃⇧K (Control-Shift-K) and achieve the goal of the OP. Which is to get what is placed on the secondary pasteboard in Terminal, by normally pressing ⌃K, onto the general system pasteboard a.k.a. the Clipboard, albeit with pressing e.g. ⌃⇧K instead.

The following was created, tested and works for me in macOS High Sierra.


Create the Automator service...

In Automator:

  1. File > New, or press: ⌘N
  2. Select Service and click the Choose button.
  3. Configure the settings as shown in the first image below.
  4. Add a Run AppleScript action.
  5. Replace the default code with the example AppleScript code.
  6. Save as e.g.: My Ctrl-K for Terminal

Automator Service


Example AppleScript code:

tell application "Terminal"
    if (count windows) is greater than 0 then
        try
            activate
            set c1 to contents of selected tab of front window
            tell application "System Events" to ¬
                keystroke "k" using control down
            delay 0.5
            set c2 to contents of selected tab of front window
        end try
    else
        return
    end if
end tell

tell current application
    set pCount to (count paragraphs of text of c1)
    repeat with i from 1 to pCount
        if (count characters of (paragraph i of c1)) is greater than 0 then
            set t1 to (paragraph i of c1)
            set t2 to (paragraph i of c2)
        end if
    end repeat

    if (count characters of t1) is not equal to (count characters of t2) then
        set AppleScript's text item delimiters to {t2}
        set CtrlY to text items of t1
        set AppleScript's text item delimiters to {}
        set the clipboard to CtrlY as text
    end if
end tell
  • Note: The example AppleScript code is just that and does not contain any other error handling then what may be present. The onus is always upon the user to employ proper error handling as necessary and or wanted.
  • The value of the delay command may or may not need to be adjusted for your system. Adjust the value as may be needed and or add additional delay commands as may be needed.

Assigning the keyboard shortcut...

In System Preferences...

  • System Preferences > Keyboard > Shortcuts > Services add ⌃⇧K (press Control-Shift-K) to the My Ctrl-K for Terminal service, as shown in the image below.

    • Note: A word about the shortcut you assign... ⌃⇧K is offered as a suggestion however, you'll need to select one that doesn't conflict with Terminal (or any other app that has focus if setting the service to any application) when pressed. This may require a strange combination or a four character combo. ⌃⇧K worked fo me in Terminal. YMMV

Keyboard Shortcuts


Now in Terminal, when one presses ⌃⇧K you'll get on the Clipboard that which ⌃K places on the secondary pasteboard.


As a side note, if you use a program like FastScripts, you only need to use the example AppleScript code as a .scpt in Script Editor, not create an Automator service and can assigned the keyboard shortcut in the Preferences for FastScripts. None of the other instructions apply unless you want to do it all natively without the use of third-party software.

Note that I am not affiliated with the developer of FastScripts, just a satisfied user.