6

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

5
  • 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, 2016 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, 2016 at 18:34
  • @lcd047 output is 0.
    – mtk
    Jun 23, 2016 at 19:45
  • @SibiCoder carpetSmokers answer works.
    – mtk
    Jun 23, 2016 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, 2016 at 20:04

6 Answers 6

16

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.

3
  • 1
    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, 2016 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, 2016 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, 2016 at 20:06
3

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
3

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

2

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

"disable title modification
set notitle
2

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.

0

Add this to your .vimrc:

let &titleold="Terminal"

This line will change the title of the window after exiting vim. In my case, the title of the shell is just "Terminal". You can read more here: vim.fandom.com

1
  • Welcome to Vi and Vim! Good answers have more than just a link and a line of code; they explain how the answer solves the problem.
    – D. Ben Knoble
    Aug 13, 2020 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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