0

我试图给我的 tmux 窗格单独的标题。由于 tmux 中没有内置任何内容来分配标题,因此我使用的函数将接收窗格的各种属性,然后根据这些属性查找我想要的标题,然后将echo其取出。

但是,test函数内部没有按预期工作。即使传入了 session_name “portal”,它也不匹配字符串“portal”,即使输出始终完全是“portal”。

我已经从函数中删除了所有不相关的代码,以准确显示失败的匹配:

tmux_pane_title() {
  local session_name=$1
  # ...
  if [[ "$session_name" = "portal" ]] && echo ".${session_name}." || echo "-${session_name}-"
  # ...
}

tmux set pane-border-format "#P: `tmux_pane_title \"#{session_name}\" \"#{pane_current_command}\" \"#{pane_current_path}\"` "

总是回显“-portal-”,表明 $1 实际上是“portal”,但它与test.

我曾尝试使用 sed 删除换行符,但没有任何区别。

但是,如果我将“门户”硬编码到 tmux 格式中,pane-border-format它会突然起作用,这表明名称中隐藏了一些奇怪的控制字符,阻止它在我传入时工作session_name

tmux set pane-border-format "#P: `tmux_pane_title \"portal\" \"#{pane_current_command}\" \"#{pane_current_path}\"` "

如果是这样,我怎样才能找到并消除控制字符?(为什么它会在那里?我没有在我的tmuxinator.yml文件中输入任何奇怪的会话名称。)

我已经尝试过删除这样的控制字符:

local session_name=$(echo $1 | tr -d "[:cntrl:]")

如果不是这种情况,我怎样才能弄清楚是什么破坏了这个功能?

PS我在tmux 3.1b上。

4

1 回答 1

0

This is NOT an answer to the question, but it is a step in the direction of a solution to the problem that led me to ask the question. If you have the same problem, this may be helpful.

Although I'm still interested in solving the mystery related to the failing test, I found a way to set my pane titles more easily1.

There is a pane_title property that can be used in your pane-border-format:

tmux set pane-border-format "#{pane_title}"

If you set this format, then you can set the title with printf and escape sequences:

printf '\033]2;%s\033\\' 'your desired title'

(I had read about the printf technique elsewhere, both on and off SO, but it fails if you don't have the proper format including pane_title. No where else did I see these two things mentioned together. Without the combination, it fails. Assuming that the default format is set is not a safe assumption.)

A complete answer is still useful, so I could do things like this:

tmux set pane-border-format " #P: #{?pane_title,#{pane_title},`tmux_pane_title \"#{session_name}\" \"#{pane_current_command}\" \"#{pane_current_path}\"`} "

That would choose an explicit title if one were set, and default to the function to choose one. So please don't close this question as a duplicate of others that relate to setting tmux pane titles. IT IS DIFFERENT.


1In other words, I solved the problem but I didn't answer the question. Many people think SO is a problem-solving site, but it describes itself as a question & answer site. I doubt if many people have given this distinction much thought, but they are very different things.

于 2020-08-22T19:27:45.237 回答