2017-07-18 59 views
2

我想在我的.vimrc中設置&titlestring以包含簡短主機名。當我有這個,它工作正常:vim:如何在標題串中使用變量

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 

但後來我得到| ~/.vimrcecho編輯在標題欄中的輸入線和Thanks for flying Vim。我如何獲得標題欄中的簡短主機名?

+1

@Ingo Karkat給出了正確的答案。關於你的問題,我懷疑這是一個典型的'system()後面的換行問題。 [Chomp it](https://stackoverflow.com/questions/3208405/how-do-i-write-a-vim-function-to-output-the-result-of-a-system-command) –

回答

4

我不會爲此啓動外部命令; .vimrc中的system()呼叫可能會解釋奇怪的症狀。

爲什麼不通過substitute()提取短主機名(第一部分,最大爲.)?

let &titlestring = $USER . "@" . substitute(hostname(), '\..*$', '', '') . " | " . expand("%:~") 
相關問題