運行跨平臺 shell(例如 Bash 或 zsh)的最大優勢在於你能在多平臺上使用同樣的語法和腳本。在 Windows 上設置(替換)shell 挺麻煩的,但所獲得的回報遠遠超出這小小的付出。

zsh shell inside Emacs on Windows

MSYS2 子系統允許你在 Windows 上運行 Bash 或 zsh 之類的 shell。使用 MSYS2 很重要的一點在於確保搜索路徑都指向 MSYS2 子系統本身:存在太多依賴關係了。

MSYS2 安裝後默認的 shell 就是 Bash;zsh 則可以通過包管理器進行安裝:

pacman -Sy zsh

通過修改 /etc/passwd 文件可以設置 zsh 作為默認 shell,例如:

mkpasswd -c | sed -e s/bash/zsh/ | tee -a /etc/passwd

這會將默認 shell 從 bash 改成 zsh。

要在 Windows 上的 Emacs 中運行 zsh ,需要修改 shell-file-name 變數,將它指向 MSYS2 子系統中的 zsh 二進位文件。該二進位 shell 文件在 Emacs exec-path 變數中的某個地方。

(setq shell-file-name (executable-find "zsh.exe"))

不要忘了修改 Emacs 的 PATH 環境變數,因為 MSYS2 路徑應該先於 Windows 路徑。接上一個例子,假設 MSYS2 安裝在 c:programsmsys2 中,那麼執行:

(setenv "PATH" "C:\programs\msys2\mingw64\bin;C:\programs\msys2\usr\local\bin;C:\programs\msys2\usr\bin;C:\Windows\System32;C:\Windows")

在 Emacs 配置文件中設置好這兩個變數後,在 Emacs 中運行:

M-x shell

應該就能看到熟悉的 zsh 提示符了。

Emacs 的終端設置(eterm)與 MSYS2 的標準終端設置(xterm-256color)不一樣。這意味著某些插件和主題(提示符)可能不能正常工作 - 尤其在使用 oh-my-zsh 時。

檢測 zsh 否則在 Emacs 中運行很簡單,使用變數 $INSIDE_EMACS

下面這段代碼片段取自 .zshrc(當以互動式 shell 模式啟動時會被載入),它會在 zsh 在 Emacs 中運行時啟動 git 插件並更改主題:

# Disable some plugins while running in Emacs
if [[ -n "$INSIDE_EMACS" ]]; then
plugins=(git)
ZSH_THEME="simple"
else
ZSH_THEME="compact-grey"
fi

通過在本地 ~/.ssh/config 文件中將 INSIDE_EMACS 變數設置為 SendEnv 變數……

Host myhost
SendEnv INSIDE_EMACS

……同時在 ssh 伺服器的 /etc/ssh/sshd_config 中設置為 AcceptEnv 變數……

AcceptEnv LANG LC_* INSIDE_EMACS

……這使得在 Emacs shell 會話中通過 ssh 登錄另一個運行著 zsh 的 ssh 伺服器也能工作的很好。當在 Windows 下的 Emacs 中的 zsh 上通過 ssh 遠程登錄時,記得使用參數 -t-t 參數會強制分配偽終端(之所以需要這樣,時因為 Windows 下的 Emacs 並沒有真正的 tty)。

跨平臺,開源真是個好東西……


via: onwebsecurity.com/confi

作者:Peter Mosmans 選題:lujun9972 譯者:lujun9972 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出


推薦閱讀:
相關文章