It is possible to bind Command Key from mac os x in zsh? I can't find any information about that.
2 Answers
If you use iTerm2, you can. First, configure hotkeys to send your custom codes:

And then you can bind them by putting the following in your .zshrc:
bindkey '^[begin' beginning-of-line
bindkey '^[end' end-of-line
- 1,524
- 1
- 12
- 16
-
See also: http://stackoverflow.com/questions/6205157/iterm2-how-to-get-jump-to-beginning-end-of-line-in-bash-shell – mik01aj Nov 06 '14 at 13:00
-
Because the solution is split between your iTerm settings and your .zshrc, couldn't this cause problems if you use a keystroke in anything other than zsh? (It would be nice if iTerm allowed some settings to only be active in certain contexts—for instance if you're using a specified shell.) – iconoclast May 05 '16 at 17:21
-
Whatever I use, `zsh` is always started first, so the mapping is there (just checked: I had `nano` inside `sh` inside my `zsh` and the keys works fine). – mik01aj May 06 '16 at 09:00
-
1Careful, when defining an escape sequence, mind that `^[b` or `^[e` or `^[{{whatever}}` might already be an escape sequence that is used by something standard. Worth looking up existing escape codes and avoiding any of them as the front of your custom sequence, or you might find that stuff misbehaves in totally unexpected and non-obvious ways in some random edge case months later. – mtraceur Mar 13 '23 at 19:43
Yes, but it's not as straightforward as you might hope.
First of all, the Mac OS doesn't pass a control sequence to applications when command-key combinations are pressed. Applications instead offer functions to the OS which can be executed via Apple Events. The binding of key combos to the specific apple event occurs at the OS level.
The best solution I can think of would be along the following lines:
Write an AppleScript which does whatever you wanted to do; use the
do shell scriptcommand to execute shell commands – or write a shell script if that's preferable. Either way, this approach will open a new terminal window for each command run, however.Alternatively, figure out a sequence of keystrokes which will generate the desired behavior (e.g. you should be able to type "top" to start up top in the current shell window).
Use Automator to create a service which either runs the AppleScript (or shell script or whatever), or types the keystrokes. Assign this service to Terminal, and assign it a keyboard shortcut. This guide walks you through that process: http://www.makeuseof.com/tag/how-to-create-your-own-services-menus-mac/
Approach 1 is imperfect because you can't run it in the current window shell session. Approach 2 is imperfect because it will give undesired results when the active shell session is running anything other than zsh.
- 688
- 6
- 14