tmux if-shell run-shell different outputs

2019-08-18 09:24发布

问题:

The is_vim command below works with the tmux if-shell command to properly detect if vim is open in the current pane, and if so then sends the key command below.

But, it is not working with run-shell, and I'm not sure why. With run-shell, the if statement always seems to evaluate to false and it always called the tmux select-pane command below.

# is_vim is directly from the setup guide for https://github.com/christoomey/vim-tmux-navigator
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
    | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'" 

# Comment out one of the below to test
bind -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"
bind -n C-h run-shell "if [ $is_vim ]; then tmux send-keys C-l; else tmux select-pane -R; fi"

回答1:

[ is a command, not part of if's syntax. After expansion, you have

if [ ps -o ... | grep ... ]; then

which is wrong; you just want

if ps -o ... | grep ...; then

so drop the brackets:

bind -n C-h run-shell "if $is_vim ; then tmux send-keys C-l; else tmux select-pane -R; fi"

However, you should be able to do something simpler (not tested):

bind -n C-l if-shell "[ #{pane_current_command} = vim ]" ...
bind -n C-h run-shell "if [ #{pane_current_command} = vim ]; then ..."

#{pane_current_command} is expanded by tmux before the shell sees the command.



标签: shell tmux