Last active
February 14, 2025 14:18
-
-
Save bignimbus/1da46a18416da4119778 to your computer and use it in GitHub Desktop.
Set iTerm2 title to current buffer in vim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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() |
thanks, it works really well!
👍 Thank you, works like a charm.
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
This works, but adds ~1,200 ms to my vim startup time.
I measured with "vim --startuptime vim.log somefile.txt"
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
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: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