vim:如何在标题字符串中使用变量

vim:如何在标题字符串中使用变量,vim,Vim,我想将我的.vimrc中的&标题字符串设置为包含短主机名。当我有这个时,它工作得很好: let &titlestring = $USER . "@" . hostname() . " | " . expand("%:~") if &term == "screen" set t_ts=^[k set t_fs=^[\ endif if &term == "screen" || &term =~ "xterm" set title endif 但它会打印

我想将我的
.vimrc
中的
&标题字符串设置为包含短主机名。当我有这个时,它工作得很好:

let &titlestring = $USER . "@" . hostname() . " | " . expand("%:~")
if &term == "screen"
  set t_ts=^[k
  set t_fs=^[\
endif
if &term == "screen" || &term =~ "xterm"
  set title
endif
但它会打印完整的主机名。为了获得较短的主机名,我尝试了以下方法:

let hostname=system('hostname -s')
let &titlestring = hostname . " | " . expand("%:~")
if &term == "screen"
  set t_ts=^[k
  set t_fs=^[\
endif
if &term == "screen" || &term =~ "xterm"
  set title
endif

但是我得到了
| ~/.vimrc
echo
ed到输入行,并且
感谢标题栏中的飞行Vim
。如何在标题栏中获取短主机名?

我不会为此启动外部命令;
system()
调用
.vimrc
可以解释奇怪的症状

为什么不通过
substitute()
?提取短主机名(第一部分,最多


@英戈·卡尔卡特给出了正确的答案。关于您的问题,我怀疑这是一个典型的带有'system()的尾随换行符问题。
let &titlestring = $USER . "@" . substitute(hostname(), '\..*$', '', '') . " | " . expand("%:~")