TransWikia.com

Properly restore terminal title after suspending vim

Vi and Vim Asked by ravron on August 31, 2021

I’m using Vim 7.3 on OS X Terminal version 2.6.1 (OS X version 10.11.2). If it’s relevant, my version is compiled without X11.

When I open a file in Vim, the terminal title bar changes to show the file name, as expected. Quitting the file with :q clears the title bar, also expected. The problem comes when using CtrlZ to stop Vim temporarily. After doing so, the title bar reads “Thanks for flying Vim”. Restarting the stopped Vim instance with %% causes the title bar to once again display the file name. Finally, quitting Vim with :q leaves the title bar showing the file name, instead of blank as it should be.

Here’s an example to clarify. Each step lists the action, the title bar that is expected, and the title bar that actually results.

  1. New terminal window – “” – “”
  2. vim foo.txt – “foo.txt” – “foo.txt”
  3. :q – “” – “”
  4. vim foo.txt – “foo.txt” – “foo.txt”
  5. CtrlZ – “” – “Thanks for flying Vim”
  6. %% – “foo.txt” – “foo.txt”
  7. :q – “” – “foo.txt”

As you can see, there are discrepancies between what I expect and what actually happens at steps 5 and 7. 5 can be fixed, I’ve found, with :set titleold=, to clear the old title variable. However, step 7 continues to vex me, and is clearly related to stopping Vim using bash job control.

How do I get the title bar properly restored even after stopping and restarting Vim using bash job control?

2 Answers

The help for 'titlestring' says:

When Vim was compiled with HAVE_X11 defined, the original title will be restored if possible, see |X11|.

As you don't have X11 Vim won't help you.

In Bash you could use the environment variable PROMPT_COMMAND. The command defined in this variable is executed before the prompt is displayed (see man bash).

PROMPT_COMMAND='printf "33]0;$PWD07"'

This would set the title to display the current working directory. You can use any other text instead of $PWD.

Before changing PROMPT_COMMAND you should check if it is currently set and maybe extend the current value.

Answered by Ralf on August 31, 2021

https://tldp.org/HOWTO/Xterm-Title-3.html

documents how to change xterm title bars on the fly. You could make a little bash script to execute the code and run it within the terminal or as a vim key-mapping, as appropriate. Presumably other terminals have similar functionality.

Answered by user985675 on August 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP