I've been setting up my terminal and I have zsh-vi-mode which gives me modal editing at the command line. I live in tmux, and I recently had the idea of replacing the tmux prefix with my vim leader key (space) when in normal mode in zsh.
Essentially, what I'd like to be able to do is alias space to ctrl+b (the tmux prefix) only when in normal mode.
I've been able to somewhat accomplish this with the following in my .zshrc
:
function vim_to_tmux_leader() {
osascript -e 'tell application "System Events" to keystroke "b" using {control down}'
}
# The plugin will auto execute this zvm_after_lazy_keybindings function
function zvm_after_lazy_keybindings() {
zvm_define_widget vim_to_tmux_leader
# In normal mode, press Ctrl-B to access tmux
zvm_bindkey vicmd ' ' vim_to_tmux_leader
}
This is clearly an ugly hack as I use apple script to send the ctrl b keystrokes to my terminal. Although it works, there's significant latency such that I have to wait ~0.5s after pressing space before I can press any tmux hotkey for them to register correctly.
I feel like there has to be a better way to do this. Is there a way to (1) send key combinations back to the terminal from zsh natively or (2) somehow buffer any subsequent keypresses after space to wait until the osascript
command has finished executing?
Well... a simple solution is to use tmux
from the command line and directly create the commands I want:
function tmux_vsplit() {
tmux split-window -v
}
function tmux_hsplit() {
tmux split-window -h
}
function zvm_after_lazy_keybindings() {
zvm_define_widget tmux_vsplit
zvm_define_widget tmux_hsplit
zvm_bindkey vicmd ' v' tmux_vsplit
zvm_bindkey vicmd ' h' tmux_hsplit
}
This doesn't give the freedom of being able to properly enter the prefix combo, but it gets the job done!