繁体   English   中英

如何使用 shell 代码启动新的 tmux session?

[英]how start new tmux session with shell code?

我想模糊查找一个文件,然后在新的 tmux window 中打开它。

不幸的是,tmux session 立即退出,并返回 0 退出代码。 如果我回显命令并从终端手动运行它,它工作正常。 同样,如果我复制 echo 的 output 并使其成为自己的命令,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

这里尝试了其他堆栈溢出帖子中的以下内容

问题在于您如何定义CMD 硬编码的tmux命令将是(使用-c选项来简化事情)

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

要存储命令及其 arguments,请使用数组。

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[@]}"
}

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

此文件映射 ctrl+p 以使用fd模糊查找文件,切换到包含目录,然后在编辑器中打开它。 If you're not in a tmux session, it does the same after opening a tmux session (in addition to changing to the project directory before starting the tmux session so it's the root of all future tmux-panes in that session).

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

未在此文件中定义: source_if_exists() { if [ -f $1 ]; then source $1; fi } 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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM