tmux if-shell run-shell different outputs

泄露秘密 提交于 2019-12-24 20:09:56

问题


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.



来源:https://stackoverflow.com/questions/56707794/tmux-if-shell-run-shell-different-outputs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!