I'd like whatever I last "cut" with Ctrl-k in Terminal to be available in my text editor when I hit Cmd-v. Is that possible? Or something close?

Update with additional explanation: Ctrl-k is by default in Terminal "delete to end of line". As well as deleting it puts the deleted text into the Terminal's clipboard (emacs "kill ring") and it can be retrieved with Ctrl-y. This is essentially a second clipboard separate from the system clipboard. I want to use the same clipboard for Terminal and for the rest of the system such as my text editor.

  • 1
    what's wrong with cmd-x? – jmh Sep 12 at 2:28
  • Cmd-x doesn't 'cut' from the Terminal (or even copy). – Thomas David Baker Sep 12 at 15:37
  • I miss read the question. I deleted my answer. – David Anderson Sep 12 at 15:46
  • Are you running bash shell? Cmd-x doesn't cut from terminal but it copies. For me anyway. I'm using iTerm. I just tried on terminal and cmd-x did not work. But commmand-c did work. – jmh Sep 12 at 15:59
  • Yes, bash. I can copy from the Terminal using the mouse and Cmd-c but I don't want to interrupt my flow to do that. I want whatever keyboard shortcuts I am using in terminal to cut/copy to send that same text to the system clipboard. For example in terminal using emacs-like shortcuts (the out-of-the-box-default) I can use Ctrl-a to jump to the beginning of the commandline and Ctrl-k to "cut" (kill in emacs terms) the contents of the line. This text can then be pasted (yanked in emacs terms) with Ctrl-y. But if I go to another program I have no access to what I cut. Does that make sense? – Thomas David Baker Sep 12 at 16:02
up vote 1 down vote accepted

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.

  • This is awesome! Thank you. I had trouble with ⌃⇧K as you suggested I might but it works on other keyboard combinations. It has the slightly weird side effect of bringing all open Terminal windows in front of all other windows - is there any way to avoid that? Thanks again. – Thomas David Baker Sep 13 at 14:33
  • I removed both the delay and the call to 'activate'. Not sure if that's bad but it now seems to do what I want :) – Thomas David Baker Sep 13 at 14:41
  • 1
    @Thomas David Baker, Yeah, I believe there is a way to address that "slightly weird side effect"... let me test out some different code and window arrangements and get back to you. I would leave the delay in because in testing I found it necessary but if it works for you, that's great. The reason the activate command is there is to insure the keystroke goes to that window, but probably really isn't necessary because the target window would have focus because you moved the cursor to the cut point right before pressing the shortcut. – user3439894 Sep 13 at 14:48
  • 1
    @Thomas David Baker, Yeah, workarounds are rarely perfect, hence why they are called workarounds. :) Glad you can make use of it! – user3439894 Sep 13 at 15:01
  • 1
    @Thomas David Baker, BTW As an alternative to using the activate command, in this particular case (which as mentioned earlier probably really isn't necessary) is to use the following command instead, tell application "System Events" to tell process "Terminal" to perform action "AXRaise" of front window, which will only act on the front window and not the other Terminal windows. – user3439894 Sep 13 at 15:17

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.