14

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.

  • 1
    can this help : stackoverflow.com/a/15124717/2558252 ? – nobe4 Jul 8 '15 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 – Mayukh Sarkar Jul 8 '15 at 19:36
  • as the question was answered (and IMHO correctly) could you accept it ? – nobe4 Aug 19 '15 at 9:56
19

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
    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 '15 at 4:02
3

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

autocmd VimLeave * call system("tmux setw automatic-rename")
  • 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 at 19:06
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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