Lan*_*opp 3 zsh zsh-completion oh-my-zsh
我最近编写了一个方便的Zsh函数,它创建了一个没有参数的新tmux会话.如果提供了参数并且会话已经存在,则会附加它.否则,将使用提供的名称创建新会话.
# If the session is in the list of current tmux sessions, it is attached. Otherwise, a new session
# is created and attached with the argument as its name.
ta() {
# create the session if it doesn't already exist
tmux has-session -t $1 2>/dev/null
if [[ $? != 0 ]]; then
tmux new-session -d -s $1
fi
# if a tmux session is already attached, switch to the new session; otherwise, attach the new
# session
if { [ "$TERM" = "screen" ] && [ -n "$TMUX" ]; } then
tmux switch -t $1
else
tmux attach -t $1
fi
}
Run Code Online (Sandbox Code Playgroud)
这很好用,但我想为它添加自动完成功能,所以当我点击Tab键时,它会为我列出当前的会话.这是我到目前为止所拥有的:
# List all the the sessions' names.
tln() {
tmux list-sessions | perl -n -e'/^([^:]+)/ && print $1 . "\n"'
}
compctl -K tln ta
Run Code Online (Sandbox Code Playgroud)
当我点击标签时,会列出会话名称,但它不允许我在它们之间切换.我错过了什么?
ZyX*_*ZyX 10
您没有仔细阅读文档:
调用给定的函数以获得完成.除非名称以下划线开头,否则该函数将传递两个参数:要尝试完成的单词的前缀和后缀,换句话说,在光标位置之前的那些字符,以及从光标位置开始的那些字符.可以使用read builtin的-c和-l标志访问整个命令行.该函数应将变量设置为
reply包含完成的数组(每个元素一个完成) ; 请注意,不应将回复置于函数的本地.从这样的函数中,可以使用-c和-l标志访问命令行以读取内置函数.例如,
.您不能从完成功能向stdout输出任何内容.您必须改为设置数组变量reply:
reply=( $(tmux list-sessions | cut -d: -f1) )
Run Code Online (Sandbox Code Playgroud)
.注意,你没有理由在这里调用perl,cut更合适.我没有^([^:]+)在tmux输出中看到任何不匹配的行.
| 归档时间: |
|
| 查看次数: |
1956 次 |
| 最近记录: |