如何用shell代码启动新的tmux会话?

问题描述 投票:0回答:1

我想模糊地找到一个文件,然后在一个新的tmux窗口中打开它。

不幸的是,tmux会话立即退出,并返回一个0的退出代码。如果我回传命令,并从终端手动运行它,它工作正常。同样的,如果我复制echo的输出,并使其成为自己的命令,yy,我可以执行它,它也能正常工作。

另一个(额外)问题似乎发生在使用ZLE时,因为它似乎没有设置终端。xx会返回一个0的退出代码(立即),而ctrl-x会从tmux返回 "不是终端 "的警告。

xx () {
  P=$(zsh -c $FZF_DEFAULT_COMMAND | fzf +m)                                     
  CMD="'cd $(dirname $P) && ${EDITOR:-vim} $(basename $P)'"                     
  echo tmux new-session $CMD                                                    
  tmux new-session $CMD
}

yy () {                                                                         
  tmux new-session 'cd /data/repos/notes && nvim engineering_a_compiler.md'                                                                           
}

# create zsh widgets
zle -N xx xx
# keybind zsh widgets
bindkey ^x xx

尝试了其他堆栈溢出帖子中的以下内容。此处

zsh tmux fzf
1个回答
0
投票

问题在于你是如何定义 CMD. 硬编码 tmux 命令将是(使用 -c 简化选项)

tmux new-session -c "$(dirname "$P")" "${EDITOR:-vim} "$(basename "$P")"

为了存储命令和它的参数,使用一个数组。

xx () {
  P=$(zsh -c "$FZF_DEFAULT_COMMAND" | fzf +m)
  CMD=("${EDITOR:-vim}" "$(basename "$P")")
  echo "tmux new-session ${CMD[*]}"
  tmux new-session -c "$(dirname "$P")" "${CMD[@]}"
}

0
投票

对于有兴趣的人,这里是我的.zshrc_fzf配置的相关部分;tl;dr是我不使用widgets,而是直接用文本绑定键调用函数,因为似乎ZLE不会将tty传递给在widgets中执行的函数。

这个文件将ctrl+p映射到模糊查找文件上,使用的是 fd,改为包含目录,并在编辑器中打开它。如果你不在tmux会话中,那么在打开tmux会话后,它也会做同样的事情(除了在开始tmux会话前改变到项目目录,使它成为该会话中所有未来tmux-panes的根)。

它用ctrl+f做同样的事情,但使用ripgrep (rg)来代替按文件内容搜索文件。

本文件中没有定义。source_if_exists() { if [ -f $1 ]; then source $1; fi }

#!/usr/bin/zsh
# SOURCE: https://github.com/junegunn/fzf/wiki/examples
source_if_exists /usr/share/fzf/key-bindings.zsh
source_if_exists /usr/share/fzf/completion.zsh

FZF_PREVIEW="'head -100 {}'"
FZF_VIM_PLUGIN_PREVIEW=~/.local/share/nvim/plugged/fzf.vim/bin/preview.sh
if [[ -f $FZF_VIM_PLUGIN_PREVIEW ]]; then
  FZF_PREVIEW=$FZF_VIM_PLUGIN_PREVIEW
fi
FZF_DEFAULT_OPTS="--layout=reverse --height='40%' --preview='"$FZF_PREVIEW" {}'"
export FZF_RIPGREP_OPTS="--column --line-number --no-heading --hidden --ignore-file $HOME/.gitignore_global"
export FZF_DEFAULT_COMMAND="fd . $SEARCH_DIRS --hidden --ignore-file ~/.gitignore_global"
FZF_CTRL_T_COMMAND=$FZF_DEFAULT_COMMAND

edit () {
  P=$1
  # file
  if [[ -f $P ]]; then
    if [[ $TERM != tmux* ]]; then  # not in tmux session
      # change to project directory, becomes root of new tmux-panes
      cd $(dirname $P)
      if [[ $(git rev-parse --is-inside-work-tree 2>/dev/null) == "true" ]]; then
        cd $(git rev-parse --show-toplevel)
      fi
      tmux new-session "cd $(dirname $P) && ${EDITOR:-vim} $(basename $P); zsh"
      clear
    else  # within existing tmux session
      cd $(dirname $P) && ${EDITOR:-vim} $(basename $P)
    fi
  # directory
  elif [[ -d $P ]]; then
    cd $P
  fi
}

# find path, change to directory, if path is a file, open in $EDITOR
fd_find () {
  edit $(zsh -c $FZF_DEFAULT_COMMAND | fzf +m)
}

rg_find () {
  CMD="rg $FZF_RIPGREP_OPTS ${1:-'.*'} $SEARCH_DIRS"
  edit $(zsh -c $CMD | fzf +m | cut -d: -f1)
}

# zsh shortcuts
bindkey -s ^p 'fd_find\n'
bindkey -s ^f 'rg_find\n'
© www.soinside.com 2019 - 2024. All rights reserved.