Zsh function keys binding for commands
Get a little faster every day. Today I got sick of autocompleting the node npm commands I sometimes use hundreds of times a day. No idea why I put up with that for so long.
So I wanted to use the function keys like in an IDE like eclipse so I can build and run the code I modified as quickly as possible i.e. not having to key in a few characters > tab > key some more characters > enter. What a waste of time!
Note that if you’re using a different shell this will vary. I initially came across the solution for BASH which is ‘bind’. I use Zsh with the Oh-my-zsh plugin manager so I had to use ‘bindkey’
I found the answer here: https://unix.stackexchange.com/questions/79897/how-can-i-use-bindkey-to-run-a-script
The -s switch lets you bind a string to a key. So to bind F8 to my ‘npm run webpack’ command:
bindkey -s ‘^[[19~’ ‘npm run webpack\n’
To bind F5 to my ‘npm start’ command:
bindkey -s ‘^[[15~’ ‘npm start\n’
The \n at the end is a newline which runs the command immediately. Put these in your .zshrc to save between sessions. Yes, I realize I could trigger my ‘run webpack’ command with the ‘npm start’ script. I like to keep the build separate from the run though, in case I just want to test if webpack worked.
Originally published at Raleigh Illgen.