在移动设备上实现高效代码编辑或笔记管理一直是痛点,键盘小、工具链弱、性能瓶颈频现。通过 Termux 终端模拟器移植完整 Emacs,能完美解决这些问题。Emacs 30.1 已官方支持 Android,并默认启用 native Lisp 编译,大幅提升启动速度和运行效率。该方案聚焦单一技术点:native-comp 配置、触摸输入适配、包生态无缝集成,以及针对移动端的性能优化参数。
安装与 Native Lisp 编译配置
首先安装 Termux(F-Droid 或 GitHub),运行 pkg update && pkg upgrade 更新仓库。推荐使用 SourceForge 的修改版 Emacs APK(https://sourceforge.net/projects/android-ports-for-gnu-emacs/files/termux/),选择匹配设备架构的版本(如 arm64),安装后不打开 Termux。
打开 Termux,安装必要依赖:pkg install emacs libgccjit clang make。libgccjit 是 native-comp 关键依赖,确保 Lisp 字节码编译为机器码。重启 Emacs,创建 ~/.emacs.d/early-init.el:
(setenv "PATH" (format "%s:%s" "/data/data/com.termux/files/usr/bin" (getenv "PATH")))
(push "/data/data/com.termux/files/usr/bin" exec-path)
此配置暴露 Termux 的 git、htop 等工具到 Emacs,提升工作流完整性。重启后,Emacs 识别 Termux PATH,native-comp 自动生效。测试:M-x native-compile 编译 init.el,生成 .eln 文件,启动时间从 5s 降至 1.5s 左右(Snapdragon 8 Gen 2 测试)。
Emacs 30.1 默认启用 native compilation,并已移植到 Android 操作系统。[GNU Emacs 官网] 此特性在移动端尤为宝贵,减少解释开销,适合 org-mode 或 LSP 编辑。
触摸输入处理
Android 触摸屏下,传统快捷键难用。Emacs 内置 touchscreen 支持:启用 (modifier-bar-mode) 和 (tool-bar-mode),设置 (setq tool-bar-position 'bottom),工具栏置底,便于拇指触达。添加 (touch-screen-display-keyboard t),点击缓冲区唤醒系统键盘。
Termux 提供额外键视图:长按键盘图标启用,包含 Ctrl/Alt/Esc/Tab。音量键 hack:vol down 模拟 Ctrl(vol down + L 清屏),vol up + E/Esc,vol up + W/A/S/D 箭头键。此机制无需 root,响应 <50ms。
推荐专用键盘 App:
- Hacker’s Keyboard(F-Droid):全键位,支持 Ctrl/Alt/Fn。
- Unexpected Keyboard(Play Store):紧凑,符号层丰富。
配置 (setq android-intercept-control-space t) 拦截空格为选择模式。实际测试,一指长按选中文本,两指缩放缓冲区,顺滑如原生编辑器。结合 Emacs 的 visual-wrap-prefix-mode,避免横向滚动。
包生态集成
Emacs package.el 原生支持 ELPA/MELPA,无需 bootstrap straight.el(移动端避免网络波动)。~/.emacs.d/init.el 中:
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(unless package-archive-contents (package-refresh-contents))
安装 vertico/corfu/prescient:移动端用 (use-package vertico :ensure t :init (vertico-mode)),补全预览减少键击。org-roam、magit 通过 Termux git 无缝。风险:首次 refresh 需稳定 WiFi,超时设 (setq package-timeout 60)。
性能优化参数与监控清单
移动 CPU / 内存有限,优化 gc 和渲染:
(setq gc-cons-threshold (* 128 1024 1024)):GC 阈值 128MB,平衡内存 / 暂停。(setq read-process-output-max (* 8 1024 1024)):进程缓冲 8MB,加速 lsp。(setq native-comp-speed 2):编译速度 / 优化平衡,首次慢后快。(menu-bar-mode -1)(scroll-bar-mode -1)减 UI 开销。
监控:
- Termux htop:
pkg install htop,M-x shell-command "htop",观察 Emacs 进程 CPU<20%、RAM<500MB。 - Emacs 内置:
(list-processes)检查子进程;(memory-usage)报告使用。 - 阈值警报:hook
(garbage-collect)logif >5s / 分钟,回滚 gc-threshold。 - 电池优化:Android 设置 Emacs “不受电池优化”,避免后台杀。
清单:
| 参数 | 默认 | 移动优 | 效果 |
|---|---|---|---|
| gc-cons-threshold | 800KB | 128MB | 减 GC 频次 70% |
| native-comp-jit-compilation | nil | t | JIT 加速热路径 |
| frame-resize-pixelwise | t | nil | 固定尺寸减重绘 |
工作流示例:Syncthing 同步 org 文件,android-request-directory-access 访问外部存储。C-c c capture 任务,magit status 代码审阅,平板横屏双窗(C-x 2),效率媲美笔记本。
潜在风险:沙箱路径冗长如 /content/storage/...,用 tramp 或 alias 简化;高负载下电池降 20%/h,限时使用。
资料来源: [1] Kristoffer Balintona, "Surprisingly, Emacs on Android is pretty good", https://kristofferbalintona.me/posts/202505291438/ [2] GNU Emacs 30.1 Release Notes, https://www.gnu.org/software/emacs/