tmux tabs with name of file open in vim

狂风中的少年 提交于 2019-11-28 04:09:24
chepner

Here's a partial answer. It can be improved, but I don't have time to work it out right now.

Put the following in your .vimrc:

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

There are other events (see :help autocmd-events in Vim) that may be used to handle as well. One thing I haven't figured out is how to change the window name if you have an instance of vim open in each of two panes, and you switch from one pane to the other. vim is unaware of the activity in tmux, so no vim events are triggered.

It is possible! I wanted to share this answer because I've been looking for it for quite some time. Finally got the time to implement it myself. Put the following in your .vimrc:

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

It will set the terminal title to only the document title currently in focus (%t stands for the document title without the path). Thanks to the event BufEnter, the terminal title changes each time you switch focus to another document. Also when leaving Vim, it is changed back to the original state. Put (or replace) the following in your .tmux.conf:

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

It is not necessary to copy it exactly, but it looks like so:

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

The I stands for the window number. The W stands for the current application being run and the T stand for the terminal title. The later we use to show the current file open in vim. The terminal title is always set (my bash terminal always shows the hostname which i don't need to see in my status bar descriptions), so to only show it when vim runs add the following to your .bashrc:

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

This is true for bash, the shell I use. PROMPT_COMMAND is evaluated just before your prompt is shown in the terminal. The echo command sets the terminal title to nothing. This action thus happens each time you get your prompt back from applications who might have changed the title. Other shells might need to be configured differently...

I wouldn't use tmux rename-window as it sets the title for as long as the windows exists. You would need to call it for each application launch. The presented approach keeps things dynamic, as it works with multiple panes in a window and multiple split screens/files open within vim.

Thanks for great input guys it saves me a lot of typing :-)

I combined the two previous answers into one, that I like.

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

The first and sedond line are for tmux and the third and 4th are for normal terminal use. You don't have to restart tmux since it is vim that updates tmux explicitly.

And to restore automatic window title on Vim exit:

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

I would also suggest to check if we are running under tmux:

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

Great answers here, but I still couldn't get it to work the way I wanted it, which is: 1) Change the TMUX window name on opening vim 2) On quit. return it to the previous name when finished I achieved it with the following 3 vimrc lines:

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))
Von

As an alternative to other answers which set the tmux window name, you can have vim set the tmux pane title instead. This lets you keep a static window name that you define with tmux new-window -n <window-name> while having vim change the pane title. You can use #T in set-titles-string to display the pane title, e.g. set-option -g set-titles-string "#W: #T" will display tmux window name and pane title.

Example vim configuration to change pane title:

" 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

Kudos to vim.wikia.com for the t_ts and t_fs guidance and phluks for the autocommand.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!