之前,本人寫了一篇文章 黑科技搶先嘗 - Windows全新終端初體驗(附無需編譯就能安裝的Preview版本及代碼Build全過程,介紹了玩轉Windows terminal的兩種方式。
今天這篇文章,主要介紹如何美化 Windows terminal 中 WSL 的 Linux 終端,依然是以本人最熟悉的Ubuntu
為例,其他版本的 Linux 請參考本文的流程換用對應的命令。假定你已按我的上一篇文章已安裝好 Windows terminal預編譯版本,同時安裝好了 Ubuntu,並設置好了 WSL
的權限,然后就可以按本文繼續操作了。
修改默認源,為apt-get安裝提速
由於 Ubuntu
官方的源實在是太慢了,建議大家緩存阿里雲鏡像的 Ubuntu 源,其具體操作步驟如下:
首先將原配置文件備份
sudo cp /etc/apt/sources.list /etc/apt/sources.list.20190516
然后 vim 打開sources.list
,
sudo vim /etc/apt/sources.list
i
(插入)模式下,在末尾輸入依次輸入每一行進行替換:
:%s/security.ubuntu/mirrors.aliyun/g
:%s/archive.ubuntu/mirrors.aliyun/g
每輸入一行,回車一次,然后就替換好了。
更新源:
sudo apt update; sudo apt upgrade
安裝python 和 python pip
依次輸入如下命令:
sudo apt-get install python3
sudo apt-get install build-essential
sudo apt-get install python-pip
pip install --upgrade pip
安裝 zsh
sudo apt-get install zsh
如果哪天不想用了,需要卸載:
sudo apt-get remove zsh
安裝powerline-font中的特定字體
從github下載字體"DejaVu Sans Mono for Powerline",並安裝。安裝字體時,選擇對應的哪一個ttf后綴的安裝即可。
手動安裝字體"Fira code" 或 "DejaVu Sans Mono for Powerline"
然后在命令行應用之,具體操作是在profiles.json中把字體設置成它,即可生效。
powerline/fonts: Patched fonts for Powerline users.
https://github.com/powerline/fonts
安裝powerline-shell
cd ~
mkdir src
cd src
git clone https://github.com/b-ryan/powerline-shell
cd powerline-shell
sudo python setup.py install
修改~目錄下的配置文件 .bashrc 和 .zshrc
首先,插入一點背景知識:
ps1格式簡介:
shell有四種不同的命令提示,稱為PS1,PS2,PS3和PS4。 PS代表Prompt String。Windows/Linux/Ubuntu都用得比較多的是PS1腳本。
"$PS1", "Prompt String 1" 指定命令行界面中的環境變量。
先編輯文件 .zshrc
vim ~/.zshrc
在末尾加入內容:
function powerline_precmd() {
PS1="(powerline-shell --shell zsh ?)"
}
function install_powerline_precmd() {
for s in "${precmd_functions[@]}"; do
if [ "$s" = "powerline_precmd" ]; then
return
fi
done
precmd_functions+=(powerline_precmd)
}
if [ "$TERM" != "linux" ]; then
install_powerline_precmd
fi
編輯完后更新文件 .zshrc
source ~/.zshrc
再編輯文件.bashrc
vim ~/.bashrc
在末尾加入內容:
function _update_ps1() {
PS1="(powerline-shell ?)"
}
if [ "$TERM" != "linux" ]; then
PROMPT_COMMAND="_update_ps1; $PROMPT_COMMAND"
fi
編輯完后更新文件 .bashrc
source ~/.bashrc
然后,我隨便輸入幾個命令:
sudo su
ls
此時Windows terminal中的效果如下:
我直接就使用這個默認主題了。
那么,如果要在命令提示符中插入emoji表情,怎么弄呢?就在下圖相應的地方(對應文件 `.bashrc`)加~
此時的 terminal 為:
更有意思的玩法可以參考:
https://loige.co/random-emoji-in-your-prompt-how-and-why/
zsh主題定制
此外,如果想更深入地定制zsh
主題,推薦安裝oh_my_zsh
,下面詳細介紹。
安裝 oh_my_zsh
Linux下安裝oh-my-zsh
,需要在終端輸入命令:
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
如果上述url無法訪問,請將地址換為鏡像
sh -c "$(wget https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh -O -)"
如果 遇到證書類似問題
apt-get install ca-certificates
解決權限問題
如果啟動時會出現zsh-syntax-highlighting
權限問題,運行下面的命令即可:
compaudit | xargs chmod g-w,o-w
卸載oh_my_zsh
的方法
uninstall_oh_my_zsh
從 Windows 10 的 Bash 中運行 WSL
對於windows 10中 WSL
獨立的默認Bash窗口,只能選擇字體"DejaVu Sans Mono for Powerline",可以在界面上選擇后使用。
開始菜單中搜索 bash 並打開,
右鍵 屬性-> 字體,選字體"DejaVu Sans Mono for Powerline"。
最后的運行結果是:
Scott Hanselman:
請注意我在 prompt 中也使用了Powerline 。 我正在使用Fira Code ,它含有我需要的glyphs,但你當然也可以使用改進過的Powerline字體或使用像Nerd Fonts和它的字體補丁程序那樣的工具制作自己的字體 。 此字體補丁程序通常用於獲取你最喜歡的等寬字體,並為其添加Powerline glyphs。
注意:如果您看到字形有任何奇怪的間距問題,您可以嘗試使用--use-single-width-glyphs
來解決它。 通過發布,我認為所有這些小問題都將得到解決。 在我的終端環境下使用Fira Code
沒有任何問題,您的環境可能有所不同。
參考:
修改 WSL Ubuntu 18.04 默認源為阿里雲鏡像 - Windows - 大象筆記
https://www.sunzhongwei.com/modify-the-wsl-ubuntu-1804-default-source-for-ali-cloud-images
b-ryan/powerline-shell: A beautiful and useful prompt for your shell
https://github.com/b-ryan/powerline-shell
How to Install Powerline Fonts, Symbols for Bash on Windows 10 - The Customize Windows
https://thecustomizewindows.com/2018/01/install-powerline-fonts-symbols-bash-windows-10/
敲黑板:更多干貨可關注 公號「大白技術控」,持續輸出優質的技術文章~
歡迎在留言區留下你的觀點,一起討論提高。如果今天的文章讓你有新的啟發,學習能力的提升上有新的認識,歡迎轉發分享給更多人。
歡迎各位讀者加入 .NET技術交流群,在公眾號后台回復“加群”或者“學習”即可。
文末彩蛋
微信后台回復“asp”,給你:一份全網最強的ASP.NET學習路線圖。
回復“cs”,給你:一整套 C# 和 WPF 學習資源!
回復“core”,給你:2019年dotConf大會上發布的.NET core 3.0學習視頻!