在vim中打开文件名的tmux选项卡

时间:2013-02-27 22:11:59

标签: vim tmux

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

7 个答案:

答案 0 :(得分:35)

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

将以下内容放入.vimrc

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

可能会有其他事件(请参阅Vim中的:help autocmd-events) 同样。我还没想到的一件事是如果你改变窗口名称 在两个窗格的每个窗格中打开一个vim实例,然后从一个窗格切换 到另一个。 vim不知道tmux中的活动,因此不会触发vim个事件。

答案 1 :(得分:17)

有可能!我想分享这个答案,因为我一直在寻找它。终于有时间自己实施了。将以下内容放入.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"'

对于bash,我使用的shell也是如此。 PROMPT_COMMAND在终端中显示提示之前进行评估。 echo命令将终端标题设置为空。因此,每当您从可能已更改标题的应用程序返回提示时,就会发生此操作。其他shell可能需要以不同方式配置......

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

答案 2 :(得分:5)

感谢很棒的输入员,它为我节省了很多打字: - )

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

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

第一个和sedond线用于tmux,第三个和第四个用于正常终端使用。您不必重新启动tmux,因为vim是明确更新tmux的。

答案 3 :(得分:3)

并在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

答案 4 :(得分:1)

这里有很好的答案,但我仍然无法按照我想要的方式工作,这是: 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))

答案 5 :(得分:1)

作为设置tmux窗口名称的其他答案的替代方法,您可以让vim设置tmux窗格标题。这使您可以保留使用tmux new-window -n <window-name>定义的静态窗口名称,同时让vim更改窗格标题。您可以使用#T中的set-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

感谢t_tst_fs vim.wikia.com以及phluks自动命令。

答案 6 :(得分: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

我在 tmux 中使用 bash 而不是自动重新格式化选项,以便窗口标题将重命名为活动窗格(如果适用)。我还在此处将标题重命名为退出 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

相关问题