15

Whenever I open a tmux session in terminal, all of the tabs containing vim sessions are labelled vim.

My question is: How to configure it to label tmux tabs with the name of the edited file or something like vim | test.cpp used as the title of the tabs in question.

3
  • 1
    can this help : stackoverflow.com/a/15124717/2558252 ?
    – nobe4
    Jul 8, 2015 at 19:34
  • thanks for this but can you please elaborate the process in a bit detail? Moreover the answer in not complete. The guy just gave an hint Jul 8, 2015 at 19:36
  • as the question was answered (and IMHO correctly) could you accept it ?
    – nobe4
    Aug 19, 2015 at 9:56

4 Answers 4

23

There you go :

autocmd BufReadPost,FileReadPost,BufNewFile,BufEnter * call system("tmux rename-window 'vim | " . expand("%:t") . "'")

Decomposing :

autocmd BufReadPost,FileReadPost,BufNewFile,BufEnter * call

On buffer read, file read or buffer new file event (see :help autocmd-events) execute the next command :

call system()

Call a system function and pass it the text :

"tmux rename-window 'vim | "  

Rename the current window with a string starting with vim |

. expand("%:t") 

Add to the string the filename (see this article for the formatting and this post for the expand insert)

. "'"

Add the final ' to close the command.

After expanding it will look like (while editing the .vimrc file):

system("tmux rename-window 'vim | .vimrc'")

As @erthalion said in the comment, you can clean up when leaving vim with :

autocmd VimLeave * call system("tmux rename-window 'tmux'")
1
  • 1
    I think it will be nice to include BufEnter event into this list (to change label even when we switch between existing buffers), and something like this autocmd VimLeave * call system("tmux rename-window 'tmux'") for cleanup.
    – erthalion
    Jul 16, 2015 at 4:02
4

To restore the default tmux naming scheme when quitting vim you can also do:

autocmd VimLeave * call system("tmux setw automatic-rename")
1
  • This is used in unison with the other answer provides the best solution. I have seen the tmux rename-window snippet all over, but this is the first time someone pointed out the automatic-rename tmux window option. Thank you.
    – f3xy
    Mar 1, 2019 at 19:06
3

This is what I put in my vimrc thanks to the answer by @raphael

I also install the tmux-plugins/vim-tmux-focus-events plugin for the FocusGained and FocusLost events.

augroup tmux
  autocmd!
  if exists('$TMUX')
    autocmd BufReadPost,FileReadPost,BufNewFile,FocusGained * call system("tmux rename-window " . expand("%:t"))
    autocmd VimLeave,FocusLost * call system("tmux set-window-option automatic-rename")
  endif
augroup END
0

To add something more to other's answers, I've added some vimscript to restore the previous tmux window name instead of the automatic rename:

if exists('$TMUX')
  let windowName = system("tmux display-message -p '#W'")
  autocmd BufReadPost,FileReadPost,BufNewFile,BufEnter * call system("tmux rename-window 'vim(" . expand("%:t") . ")'")
  autocmd VimLeave * call system("tmux rename-window " . windowName)
endif
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.