Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically set iTerm2 tab title to the filename opened in vim?

Tags:

vim

tabs

iterm

I can get the vim title to display on my window by doing this:

let &titlestring = expand("%:t") . " @ " . hostname()
if &term == "screen"
  set t_ts=^[k
  set t_fs=^[\
endif
if &term == "screen" || &term == "xterm"
  set title
endif

But the tabs will say "Default".

From the commandline I can do this:

echo -ne "\e]1;hello world\a"

And that'll show "Hello World" in my tabs.

Is there a way to have vim write this stuff to my tab instead of title instead?

like image 437
dd. Avatar asked Jan 20 '10 23:01

dd.


2 Answers

This works for me:

" Set the title of the Terminal to the currently open file
function! SetTerminalTitle()
    let titleString = expand('%:t')
    if len(titleString) > 0
        let &titlestring = expand('%:t')
        " this is the format iTerm2 expects when setting the window title
        let args = "\033];".&titlestring."\007"
        let cmd = 'silent !echo -e "'.args.'"'
        execute cmd
        redraw!
    endif
endfunction

autocmd BufEnter * call SetTerminalTitle()

Source: https://gist.github.com/bignimbus/1da46a18416da4119778

like image 62
mgaw Avatar answered Oct 02 '22 03:10

mgaw


I don't have iTerm, so I can't test this, but try adding this to your .vimrc:

set t_ts=^[]1;
set t_fs=^G

Type CTRL-V Escape for ^[ and CTRL-V CTRL-G for ^G.

like image 22
Laurence Gonsalves Avatar answered Oct 02 '22 03:10

Laurence Gonsalves