copy paste – Launching a terminal command with a shortcut (cmd + [LETTER])

I'm still not quite clear on what you're trying to accomplish, but perhaps this will help:

1. You didn't post a Python script, so I made one up:

Apologies, I'm not a PyGuy! I saved it as ~/scripts/ All it does is grab the contents of the pasteboard, and print it to stdio. You can augment it, or put the relevant code in your script:

import sys, subprocess

def get_clipboard_data():
    p = subprocess.Popen(["pbpaste"], stdout=subprocess.PIPE)
    retcode = p.wait()
    data =
    return data

new_data = get_clipboard_data()

print('clipboard data: ',new_data)

2. Make the script executable:

chmod 755 ~/scripts/

Note: You can run this script from the command line to test it before proceeding; make sure you copy a line of text using commandc before running the script – otherwise the pasteboard may be empty!:


3. Enter this line in your zsh config file – ~/.zshrc:

bindkey -s 'eg' '~/scripts/^M' 

4. source your ~/.zshrc file as shown below to load the revised config file into your shell:

. ~/.zshrc

# alternatively, use the command 'source ~/.zshrc'

5. Run bindkey to verify it's been added:

bindkey | grep mypyapp

# which should yield one line:

"^[g" "~/scripts/^M"

*Note that the keyboard shortcut is not what we entered in Step 3. 'eg' was expected to mean optiong. But it seems that macos has overruled that choice, replacing it with ^[g; which is translated as follows:

control[ + g

The sequence is a bit tricky; you must press both control[, then release both, and then finally g.

That's it

I didn't test this thoroughly, so make sure you do before committing it to to a serious application. Also know that adding some zle (zsh line editor) code gives you many more options, but this may be a reasonable place to start. Let us know if you hit any snags, or have further questions.

Author: Subham

Leave a Reply

Your email address will not be published. Required fields are marked *