【问题标题】:tmux tabs with name of file open in vim带有文件名的 tmux 选项卡在 vim 中打开
【发布时间】:2013-02-27 22:11:58
【问题描述】:

我是 tmux 的新手,我正在尝试弄清楚如何编辑配置,以便打开 vim 的窗口在任务栏中显示的不是 #:vim,而是无论在 vim 中打开的文件的名称是什么
(ie "#:filename.php")。似乎这应该是一件常见的事情,但我的搜索 - foo 失败了。

【问题讨论】:

  • 我在 vim 中经常使用 split 。如果我在缓冲区中打开 10 个文件并将 4 个文件拆分为 4 个窗口,你告诉我,你想在 tmux 窗口标签中显示什么?
  • @Kent 假设在一个窗口中只打开了一个文件。这个可以吗?
  • Kent:你可以在 Vim 中定义适当的自动命令来调用 tmux rename-window 并使用活动缓冲区中的文件名。

标签: vim tmux


【解决方案1】:

这是部分答案。它可以改进,但我现在没有时间解决它。

将以下内容放入您的.vimrc

autocmd BufReadPost,FileReadPost,BufNewFile * call system("tmux rename-window " . expand("%"))

还有其他事件(见 Vim 中的:help autocmd-events)可以用来处理 也是。我还没有弄清楚的一件事是如何更改窗口名称,如果你 在两个窗格中的每个窗格中打开一个 vim 实例,然后从一个窗格切换 给对方。 vim 不知道 tmux 中的活动,因此不会触发 vim 事件。

【讨论】:

  • 我不得不调整它以使用 expand() 来获取百分号以扩展到窗口名称,否则我只是将“%”作为我的窗口名称。这是使用 vim 7.3。即:autocmd BufReadPost,FileReadPost,BufNewFile * call system("tmux rename-window " .expand("%"))
  • 我发现在使用多个窗格或窗口时,或者逃犯我需要将 BufEnter 添加到事件列表中,以便在缓冲区之间移动时保持标题。
  • expand("%:t") 将放置文件名,而 expand("%") 显示完整路径。
  • 可能缺失:您需要设置t_ts。这适用于 tmux/screen:set t_ts=^[k。 (根据需要,使用 ctrl-v、ESC 输入 ^[ 字符作为实际转义符。)
  • 这似乎在您离开 vim 后重新命名了窗口。为了解决这个问题,我添加了@Phluks 回答中所述的autocmd VimLeave * call system("tmux rename-window bash")
【解决方案2】:

有可能!我想分享这个答案,因为我一直在寻找它很长一段时间。终于有时间自己实现了。将以下内容放入您的.vimrc

autocmd BufEnter * let &titlestring = ' ' . expand("%:t")
set title

它将终端标题设置为仅当前焦点的文档标题(%t 代表没有路径的文档标题)。感谢事件BufEnter,每次您将焦点切换到另一个文档时,终端标题都会发生变化。同样在离开 Vim 时,它会变回原来的状态。在您的.tmux.conf 中放入(或替换)以下内容:

set-window-option -g window-status-current-format "[#I #W#T]"
set-window-option -g window-status-format "[#I #W#T]"

没有必要完全复制,但看起来是这样的:

[1 vim .tmux.conf][2 bash]...

I 代表窗口编号。 W 代表正在运行的当前应用程序,T 代表终端标题。后面我们用来显示当前在 vim 中打开的文件。始终设置终端标题(我的 bash 终端始终显示我不需要在状态栏描述中看到的主机名),因此仅在 vim 运行时显示它,将以下内容添加到您的 .bashrc:

PROMPT_COMMAND='echo -ne "\033]0;\007"'

这对于我使用的 shell bash 来说是正确的。 PROMPT_COMMAND 在终端中显示您的提示之前进行评估。 echo 命令将终端标题设置为空。因此,每次您从可能已更改标题的应用程序中收到提示时,都会发生此操作。其他 shell 可能需要进行不同的配置...

我不会使用tmux rename-window,因为只要窗口存在,它就会设置标题。您需要在每次应用程序启动时调用它。所提出的方法使事情保持动态,因为它适用于一个窗口中的多个窗格以及在 vim 中打开的多个分屏/文件。

【讨论】:

  • 我试过这个没有任何运气。有什么调试技巧吗?我确实重启了 tmux。
  • 这还不足以继续下去,但最好的猜测是您没有使用 bash 作为默认终端。通过将“set-option -g default-shell /bin/bash”放入“.tmux.conf”中,可以强制 TMUX 使用 bash。也许您没有看到可以通过添加“set-option -g status on”来修复的状态栏。运气好吗?
  • autocmd BufEnter * let &titlestring = ' ' . expand("%:t") 在 tmux 中似乎对我不起作用。正如@Phluks 建议的那样,我在 tmux 中使用了tmux rename-window 方法。
  • 我刚刚弄清楚你所说的 terminal title 是我认为 tmux 称之为 pane title 的东西,你得到的答案与我刚刚在下面发布的相同。你缺少的是设置t_tst_fs,至少这对我有用
【解决方案3】:

感谢各位大神们的大力投入,这为我节省了很多打字时间:-)

我将之前的两个答案合二为一,我喜欢。

autocmd BufEnter * call system("tmux rename-window " . expand("%:t"))
autocmd VimLeave * call system("tmux rename-window bash")
autocmd BufEnter * let &titlestring = ' ' . expand("%:t")                                                                 
set title

第一行和第二行用于 tmux,第三和第四行用于正常终端使用。您不必重新启动 tmux,因为它是显式更新 tmux 的 vim。

【讨论】:

  • 前两个答案对我不起作用,只有这个。为什么?
【解决方案4】:

并在 Vim 退出时恢复自动窗口标题:

autocmd VimLeave * call system("tmux setw automatic-rename")

我还建议检查我们是否在 tmux 下运行:

if exists('$TMUX')
    autocmd BufEnter * call system("tmux rename-window '" . expand("%:t") . "'")
    autocmd VimLeave * call system("tmux setw automatic-rename")
endif

【讨论】:

    【解决方案5】:

    这里的答案很好,但我仍然无法让它按照我想要的方式工作,即: 1) 打开 vim 时更改 TMUX 窗口名称 2) 退出时。完成后将其恢复为以前的名称 我通过以下 3 行 vimrc 实现了它:

    autocmd BufReadPost,FileReadPost,BufNewFile * call system("tmux rename-window " . expand("%:t"))
    let tmuxtitle = system("tmux display-message -p '#W'")
    autocmd VimLeave * call system("tmux rename-window " . shellescape(tmuxtitle))
    

    【讨论】:

    • 效果很好,除了没有文件名的缓冲区。知道如何解决这些问题吗?
    【解决方案6】:

    作为设置 tmux 窗口名称的其他答案的替代方法,您可以让 vim 设置 tmux 窗格标题。这使您可以保留使用 tmux new-window -n <window-name> 定义的静态窗口名称,同时让 vim 更改窗格标题。您可以在set-titles-string 中使用#T 来显示窗格标题,例如set-option -g set-titles-string "#W: #T" 将显示 tmux 窗口名称和窗格标题。

    更改窗格标题的 vim 配置示例:

    " Teach vim the tmux escape sequences for changing pane title
    " Note the "^[" should be a literal escape code (use `^v<esc>` to enter it)
    set t_ts=^[]2;
    set t_fs=^[\\
    
    " Turn on setting the title.
    set title
    
    " The following causes vim to refresh the title each time we change buffers.
    " Otherwise it will only set the title once at startup.
    augroup RefreshTitle
      autocmd!
      " The concatenation of the colon is a hack to prevent vim from
      " interpreting the string as a modeline.
      autocmd BufEnter * let &titlestring = "vim" . ":" . expand("%:t")
    augroup END
    

    感谢 vim.wikia.comt_tst_fs 指导以及 phluks 的自动命令。

    【讨论】:

    • FWIW,对我来说,“set title”和“set titlestring=%t”在我切换缓冲区时保持窗格标题是最新的,我不需要 augroup/BufEnter 命令.
    【解决方案7】:

    感谢这里的所有精彩答案!

    这是这些方法加上一些 bash(在使用窗格时重命名窗口)的组合,我用来创建没有索引号或其他符号的标题名称,尽管它可以用于任何所需的格式。

    basePath/   -> at the prompt
    fileName    -> inside Vim
    

    ~/.tmux.conf

    允许通过下面的 .vimrc 和 .bashrc 配置文件重命名窗口标题,并将标题格式设置为仅显示名称。

    要保留索引号,请更改窗口状态格式 和窗口状态当前格式行到“#I:#W”。有关“格式”和“变量名称”下的更多选项,请参阅 tmux 手册页。

    set -g allow-rename on
    set-window-option -g window-status-format "#W"
    set-window-option -g window-status-current-format "#W"
    

    特定于没有索引号的配置,您可以将选项卡创建和移动绑定设置为更像浏览器和 Vim。

    # Create window -- Ctrl + t 
    # Navigate windows -- Ctrl+ h,l 
    bind -n C-t new-window
    bind -n C-h previous-window
    bind -n C-l next-window
    

    ~/.vimrc

    设置窗口标题为进入 Vim 并保存文件时的文件名。

    if exists('$TMUX')
        autocmd VimEnter,BufWrite * call system("tmux rename-window ' " . expand("%:t") . " '")
    endif
    
    

    ~/.bashrc

    我使用 bash 而不是 tmux 中的自动重新格式化选项,以便窗口标题将被重命名为活动窗格(如果适用)。我还将标题重命名为在此处退出 Vim 时的基本路径。

    # If Tmux running...
    tmux ls > /dev/null 2>&1
    TMUX_STATUS=$?
    if [ $TMUX_STATUS -eq 0 ]; then
    
        # Create function to get pwd, trim to "basepath/", 
        # and rename window
        basepathTitle () {
            getval=$(pwd)
            BASEPATH_TITLE=" ${getval##*/}/ "
            tmux rename-window "$BASEPATH_TITLE"
        }
    
        # Change cd functionality to rename window title to
        # pwd after every directory change
        cd () {
    
            builtin cd "$@"
            CD_STATUS=$?
    
            basepathTitle
    
            return "$CD_STATUS"
        }
    
        # Change vim functionality to change title 
        # back to basepath on close
        vim () {
            
            /usr/bin/vim "$@"
            VIM_STATUS=$?
            
            basepathTitle
    
            return "$VIM_STATUS"
        }
    
        # Set window title when tmux starts
        basepathTitle
    
    fi
    

    来源 tmux.conf
    tmux source-file ~/.tmux.conf
    

    来源.bashrc
    . .bashrc
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多