UP | HOME

▼ 本文更新于 [2025-07-20 周日 21:49]

emacs-在tramp的remote-buffer中执行本地shell

使用Emacs的tramp连接到远程buffer中时,如果你恰好有一些定时执行的函数,且他们包含 shell-command 这个函数,那么将会在tramp的远程shell中执行这些命令。

根据 StackOverFlow 的这个回答,问题的关键在于 default-directory 这个local-variable:

The point is default-directory . If it is local, your command runs locally. So do something like

(let ((default-directory "/"))
(do your stuff))

我们可以自定义一个函数 my/local-shell-command

(defun my/local-shell-command (command)
  "在本地计算机上执行一个 shell 命令,无论当前 TRAMP 是否激活。"
  (let ((default-directory temporary-file-directory)) ; 关键:强制进程在本地运行
    (shell-command command)))

然后在需要本机电脑执行的情况下,用 my/local-shell-command 替换掉 shell-command

© Published by Emacs 31.0.50 (Org mode 9.8-pre) | RSS 评论