32

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

4

7 回答 7

42

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

将以下内容放入您的.vimrc:

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

还有其他事件(参见:help autocmd-eventsVim)也可以用来处理。我还没有弄清楚的一件事是,如果您vim在两个窗格中的每个窗格中都有一个 open 实例,并且您从一个窗格切换到另一个窗格,那么如何更改窗口名称。vim不知道 中的活动tmux,因此不会vim触发任何事件。

于 2013-02-27T23:37:26.927 回答
18

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

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

它将终端标题设置为仅当前焦点%t的文档标题(代表没有路径的文档标题)。由于 event 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 中打开的多个分屏/文件。

于 2013-04-05T19:33:52.753 回答
9

感谢伟大的输入家伙它节省了我很多打字:-)

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

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。

于 2015-04-17T07:42:07.270 回答
7

并在 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
于 2016-06-17T08:56:10.403 回答
2

这里有很好的答案,但我仍然无法让它按照我想要的方式工作,即: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))
于 2015-07-10T19:00:52.523 回答
2

作为设置 tmux 窗口名称的其他答案的替代方法,您可以让 vim 设置 tmux 窗格标题。tmux new-window -n <window-name>这使您可以在 vim 更改窗格标题时保留您定义的静态窗口名称。您可以使用#Tinset-titles-string显示窗格标题,例如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.com的自动命令t_tst_fs指导和phluk

于 2016-05-10T01:05:52.940 回答
1

感谢这里所有的好答案!

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

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

~/.tmux.conf

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

要保留索引号,请将 window-status-format 和 window-status-current-format 行更改为“#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

于 2021-06-07T21:40:46.587 回答