Skip to content

Instantly share code, notes, and snippets.

@bignimbus
Last active October 12, 2022 11:19
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bignimbus/1da46a18416da4119778 to your computer and use it in GitHub Desktop.
Save bignimbus/1da46a18416da4119778 to your computer and use it in GitHub Desktop.
Set iTerm2 title to current buffer in vim
" 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()
@bignimbus
Copy link
Author

Please leave feedback if there's room for improvement - I'm still new at vimscript.

@VictorTaelin
Copy link

Feedback: you're awesome.

Room for improvement: create more of you.

@WilliamLu99
Copy link

You are amazing. I have been looking for this for hours.

@m147
Copy link

m147 commented Dec 29, 2017

awesome!
anyway to display full path?
and also maybe ssh info. such as ssh user@host:path/to/file
don't know if this would be too much to ask :D
either way, great work
thanks.

@m147
Copy link

m147 commented Dec 30, 2017

Ok, I figured it out, how to do what I want as per my above comment.
Here it is, I changed the 3rd & 5th lines:
(compare to the code above)

" Set the title of the Terminal to the currently open file
function! SetTerminalTitle()
    let titleString = expand('$USER@$HOSTNAME: ') . expand('%:~')
    if len(titleString) > 0
        let &titlestring = expand('$USER@$HOSTNAME: ') . expand('%:~')
        " 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

this will display something like this: USER@HOSTNAME: ~/path/to/working/directory
also with ssh the USER@HOSTNAME will be changed to that of the ssh session.

@nicholsonjf
Copy link

Worked with iTerm2 build 3.1.5. Thank you!

@jbyman
Copy link

jbyman commented Jun 22, 2018

This is awesome! Thank you for this.

One thought – it doesn't look like this plays well with vim-airline Super small issue, but it looks like there's a highlighted box in the top left:

screen shot 2018-06-22 at 2 14 37 pm

Any idea how to get around this?

[Edit]

Looks like adding :edit at the bottom of the function "refreshes" the buffer and fixes things, but it feels hacky

@SeaOfOcean
Copy link

thanks, it works really well!

@unimatrixZxero
Copy link

👍 Thank you, works like a charm.

@diegoe
Copy link

diegoe commented Mar 30, 2020

The problem with this solution is that it triggers redraws whenever you switch to a new buffer (specially visible when you are switching between panels in a window). The actual fix for this is to correctly set the escape sequence for title setting in iTerm2:

From my vimrc:

""" Fix titles for iTerm2 tabs
" iTerm2 uses an special escape sequence to set titles. The default
" 'title start' and 'title finish' sequences in vim can only set the
" iTerm2 title for single-tab windows. As soon as you open a new window,
" your title will be obscured by your iTerm2's default title.
"
" The solution is to set the iTerm2 magic sequences instead of the
" generic default ones in vim. Note that these are *escape sequences*
" and not just literal characters (control+v <esc>, and control+v control+g)
"
" See the following:
"   https://linux.die.net/HOWTO/Xterm-Title-3.html
"   https://www.iterm2.com/faq.html
"   https://vim.fandom.com/wiki/Automatically_set_screen_title
set t_ts=^[];
set t_fs=^G

IMPORTANT: Please note that those are not literal ^G or ^[ -- those are escape sequences, you need to use (control+v , and control+v control+g) in INSERT mode, respectively

@rparker
Copy link

rparker commented Aug 28, 2020

This works, but adds ~1,200 ms to my vim startup time.
I measured with "vim --startuptime vim.log somefile.txt"

@PKostovic
Copy link

Thank you, this saved me a bunch of time tinkering with it myself and made life a little bit easier!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment