6

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.

6
  • 1
    what's wrong with cmd-x?
    – Natsfan
    Commented Sep 12, 2018 at 2:28
  • Cmd-x doesn't 'cut' from the Terminal (or even copy). Commented Sep 12, 2018 at 15:37
  • I miss read the question. I deleted my answer. Commented Sep 12, 2018 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.
    – Natsfan
    Commented Sep 12, 2018 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? Commented Sep 12, 2018 at 16:02

2 Answers 2

3

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

3
  • hah thanks for still reading such an old question! Glad it was handy
    – Chris
    Commented Sep 1, 2020 at 4:36
  • I do not find anything about pb-kill in ~/.zshrc, however, deleted text by ^k are moved to the pasteboard, which is really strange. I don't know what I did to my pasteboard.
    – DawnSong
    Commented Mar 8, 2022 at 14:21
  • @DawnSong run bindkey '^K' to reveal what function/widget ^k is bound too. By default it's bound to kill-line which I don't think syncs with pasteboard. Though if it's not kill-line then look up what it is and you probably have the answer. (you might be using some zsh configs that add. oh-my-zsh for example)
    – Chris
    Commented Mar 31, 2022 at 21:31
1

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.

9
  • 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. Commented Sep 13, 2018 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 :) Commented Sep 13, 2018 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. Commented Sep 13, 2018 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! Commented Sep 13, 2018 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. Commented Sep 13, 2018 at 15:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .