I recently got to know that useful info can be shown in title of bash terminal when editing files using vim. Adding below displays file name, path of the file being edited.

set title

But on exiting vim, the title is set to 'Thanks for flying vim'. I have different title set for each bash terminal and would like it to be restored once I close the vim?

Any way to change this behaviour of vim?

Before:
enter image description here

While editing a file in vim:
enter image description here

After:
enter image description here

Animation: see the title text Animation

  • 1
    This is between Vim and your terminal, it has nothing to do with bash (on a side note: there is no such thing as a "bash terminal"). What is the output of :echo has('X11')? – lcd047 Jun 23 '16 at 16:55
  • I dont think this is possible with vim. May be possible with Shell scripting or commands. I have asked a similar question earlier. 'Once the tile is changed to Thanks for flying vim , it can't be reset again. This is said to be for security reasons. Refer :help title . – SibiCoder Jun 23 '16 at 18:34
  • @lcd047 output is 0. – mtk Jun 23 '16 at 19:45
  • @SibiCoder carpetSmokers answer works. – mtk Jun 23 '16 at 19:46
  • Well, this looks like Ubuntu, which is known to ship a crippled Vim. But, if you say the solution posted by @Carpetsmoker, I suppose everybody's happy. – lcd047 Jun 23 '16 at 20:04
up vote 8 down vote accepted

Use the titleold setting:

" Update term title but restore old title after leaving Vim
set title
set titleold=

From :help 'titleold':

This option will be used for the window title when exiting Vim if the original title cannot be restored. Only happens if 'title' is on or 'titlestring' is not empty.

  • Nope, the original title should be restored regardless of the value of titleold. If it doesn't it's because Vim couldn't figure out the original title. That could happen if Vim is compiled without +X11, or if fs or ts entries in termcap (or the equivalent in terminfo) are messed up. – lcd047 Jun 23 '16 at 17:04
  • As far as I know, original title can never be get again through vim. Can I tell more about it? – SibiCoder Jun 23 '16 at 18:35
  • @SibiCoder Sure it can, it works fine here on X11. But it needs a Vim version compiled with certain (combinations of) features. – lcd047 Jun 23 '16 at 20:06

The actual problem is that Vim does not know how to read the old title, because termcap/info is missing the control sequence to do that. But we can supply it ourselves for terminal types we know it for. Xterm and screen use the same sequence, so this will fix both:

if has ("title")

    " if there is no &t_ts sequence, is the terminal type known?
    if &t_ts == "" && ( &term == "screen" || &term == "xterm" )
        " add the missing control sequence for xterm or screen 
        let &t_ts = "\e]2;"
    endif

    " enable setting the title only if vim can read the old one
    if &t_ts != ""
        set title
    endif

endif

To supress vim changing the window title just disable the function:

"disable title modification
set notitle

To set your bash terminal title to your short hostname when exiting vim, add this to your .vimrc:

execute "set titleold=".hostname()

At least on Arch, bash's default is the short hostname, so unless you have something else changing it sometimes, this will emulate restoring it.

All credit goes to a google search which found: https://gist.github.com/charlesthomas/9915977

New contributor
user1902689 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

As of 8.1.0253, Vim will use the terminal's title stack to push/pop the title. This is more robust than trying to query the title from X, but not all terminals may support this functionality.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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