Vim Tip: Setting File Name in the Title Bar (Not the Status Bar)

When I first started using Vim in Ubuntu, I noticed that while editing a file, title bar of the terminal window would display something like this:

sampson-chen@ubuntu: ~/src/reviewboard/reviewboard/reviews/ui

which falls just short of being helpful because it omits the most important part: which file exactly is being edited. It is not realistic (and indeed wasteful of mental resources) to keep track of this information especially when often several files are open simultaneously across 5-6 terminal sessions. The problem is yet more pronounced when these files happen to reside in the same directory.

After a bit of digging, I found a solution. Add the following to your ~/.vimrc:

let &titlestring = expand("%:p")
if &term == "screen"
  set t_ts=^[k
  set t_fs=^[
endif
if &term == "screen" || &term == "xterm"
  set title
endif

(Modified from the example at http://vim.wikia.com/wiki/Automatically_set_screen_title – To create ^[, which is escape, you need to enter CTRL+V <Esc>)

This gives you the full path in the title bar:

/home/sampson-chen/src/reviewboard/reviewboard/reviews/ui

which worked well enough for about a week, until I came across a simpler (and better) solution. Add the following to your ~/.vimrc instead:

set title

And it will give you the file name first, then the prefixing path, and then “VIM”:

base.py (~/src/reviewboard/reviewboard/reviews/ui) - VIM

TL;DR – add “set title” to your ~/.vimrc

One thought on “Vim Tip: Setting File Name in the Title Bar (Not the Status Bar)

Leave a Reply

Your email address will not be published. Required fields are marked *