Windows下Emacs中的zsh Shell

系统 Windows Linux
运行跨平台 shell(例如 Bash 或 zsh)的最大优势在于你能在多平台上使用同样的语法和脚本。在 Windows 上设置(替换)shell 挺麻烦的,但所获得的回报远远超出这小小的付出。

[[259323]]

运行跨平台 shell(例如 Bash 或 zsh)的***优势在于你能在多平台上使用同样的语法和脚本。在 Windows 上设置(替换)shell 挺麻烦的,但所获得的回报远远超出这小小的付出。

zsh shell inside Emacs on Windows

zsh shell inside Emacs on Windows

MSYS2 子系统允许你在 Windows 上运行 Bash 或 zsh 之类的 shell。使用 MSYS2 很重要的一点在于确保搜索路径都指向 MSYS2 子系统本身:存在太多依赖关系了。

MSYS2 安装后默认的 shell 就是 Bash;zsh 则可以通过包管理器进行安装:

  1. pacman -Sy zsh

通过修改 etc/passwd 文件可以设置 zsh 作为默认 shell,例如:

  1. 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 变量中的某个地方。

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

不要忘了修改 Emacs 的 PATH 环境变量,因为 MSYS2 路径应该先于 Windows 路径。接上一个例子,假设 MSYS2 安装在 c:\programs\msys2 中,那么执行:

  1. (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 中运行:

  1. M-x shell

应该就能看到熟悉的 zsh 提示符了。

Emacs 的终端设置(eterm)与 MSYS2 的标准终端设置(xterm-256color)不一样。这意味着某些插件和主题(提示符)可能不能正常工作 - 尤其在使用 oh-my-zsh 时。

检测 zsh 否则在 Emacs 中运行很简单,使用变量 $INSIDE_EMACS

下面这段代码片段取自 .zshrc(当以交互式 shell 模式启动时会被加载),它会在 zsh 在 Emacs 中运行时启动 git 插件并更改主题:

  1. # Disable some plugins while running in Emacs
  2. if [[ -n "$INSIDE_EMACS" ]]; then
  3. plugins=(git)
  4. ZSH_THEME="simple"
  5. else
  6. ZSH_THEME="compact-grey"
  7. fi

通过在本地 ~/.ssh/config 文件中将 INSIDE_EMACS 变量设置为 SendEnv 变量……

  1. Host myhost
  2. SendEnv INSIDE_EMACS

……同时在 ssh 服务器的 /etc/ssh/sshd_config 中设置为 AcceptEnv 变量……

  1. AcceptEnv LANG LC_* INSIDE_EMACS

……这使得在 Emacs shell 会话中通过 ssh 登录另一个运行着 zsh 的 ssh 服务器也能工作的很好。当在 Windows 下的 Emacs 中的 zsh 上通过 ssh 远程登录时,记得使用参数 -t-t 参数会强制分配伪终端(之所以需要这样,时因为 Windows 下的 Emacs 并没有真正的 tty)。

跨平台,开源真是个好东西…… 

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2011-07-20 10:18:41

EmacsShell

2023-07-27 17:41:02

BashZshFish

2010-01-16 20:22:47

Windows 7shell

2019-06-17 08:50:49

Emacs shell命令Windows

2011-07-19 10:16:55

2020-03-09 12:31:08

WindowsWindows 10GNU Emacs

2019-06-13 10:24:34

工具性能数据

2009-03-04 12:03:30

LinuxZsh技巧

2021-03-09 08:58:51

windowsemacswsl2

2020-04-26 20:00:14

EmacsLinux

2012-01-18 10:46:33

ibmdw

2019-02-27 12:00:09

开源Org模式Emacs

2020-12-15 09:08:40

LinuxShell

2014-07-31 11:24:21

Linuxshell命令

2021-01-19 13:10:29

ZshLinuxUbuntu

2010-04-08 15:37:33

2023-06-03 20:36:35

ShellLinuxBash

2012-05-08 10:51:10

Linuxshell脚本

2009-07-20 17:17:54

Shell函数ASP.NET环境

2010-11-02 21:17:17

点赞
收藏

51CTO技术栈公众号