I have the number of my xterm (i.e., the value of /dev/tty??) saved as an environment variable, TTY e.g., echo $TTY returns 11. I'd like to include this number in the title, along with the name of the file when I open vi.

Currently I have

set titlestring=%-25.55F\ %a%r%m titlelen=17

I've tried the obvious:

set titlestring=$TTY\ %-25.55F\ %a%r%m titlelen=17

But this doesn't expand $TTY, e.g., and the title of my xterm window is

$TTY myFilename

instead of

11 myFilename

Could somebody please advise how to get the desired result? Thanks!

2 Answers 2


Variables are not expanded in the context of a set command. So use let:

let &titlestring = $TTY . ' %-25.55F %a%r%m'
set titlelen=17
  • But this will expand the value at the time of the definition of the value, not when displaying it. One also needs to be careful about expanding the value, if it can contain whitespaces I think (not sure about that) Commented May 18, 2020 at 9:43
  • @ChristianBrabandt From the name "TTY", I would not expect it to change during runtime. As we use let here, no escaping of whitespaces is needed. Would be different with exe "set titlestring=" . $TTY . "..."
    – Ralf
    Commented May 18, 2020 at 9:50
  • I see, thanks for clarification Commented May 18, 2020 at 11:38
  • Fabulous, thanks very much, @Ralf and @ChristianBrabandt. One followup question: most of the time I call vi with an alias that first sets the TTY variable. But not always. Is there a way to condition the titlestring line on the existence of $TTY? That is, in csh, I'd write if ( ($?TTY) ) then let ...?
    – Leo Simon
    Commented May 19, 2020 at 7:32
  • @LeoSimon Then you'll need if $TTY != '' | let &titlestr=... | else | let &titlestr=... | endif (replace '|' with line breaks).
    – Ralf
    Commented May 19, 2020 at 9:25

For this particular case where $TTY is constant and not likely to change, @Ralf's answer of using let &titlestring to compose the setting string value is recommended.

Another possible approach is to use a %{...} group inside the setting value, which will evaluate it every time the setting is used.

In this case, this would work:

set titlestring=%{$TTY}\ %-25.55F\ %a%r%m titlelen=17

The 'statusline' option also uses similar %-sequences and using a %{...} with a valid Vimscript expression is also an option there.

Your Answer

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

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