【问题标题】:run a shell command from a specific directory in emacs从 emacs 中的特定目录运行 shell 命令
【发布时间】:2014-04-21 22:19:25
【问题描述】:

如何从特定目录执行 shell 命令(例如,git gui)?我更喜欢一种不依赖于&&; 等shell 运算符的跨平台方法,因为我需要它在Windows 和unix 上运行。 (例如,调用 cd /path/to/dir && git gui 在 Windows 上不起作用,因为 && 无效。)

我试过了:

(async-shell-command (concat "cd \""
  (replace-regexp-in-string 
    "/" "\\\\" (file-name-directory (buffer-file-name))) "\" ; git gui"))

这失败了,因为无论出于何种原因,shell 都认为 ; git gui 是路径的一部分,并报告:The system cannot find the path specified.

所以,我宁愿不处理 shell 怪癖,我希望有一个 elisp 函数可以为 shell-commandasync-shell-command 设置目录。我试过了:

 (shell-process-pushd (file-name-directory (buffer-file-name)))
 (shell-command "git gui")
 (shell-process-popd nil)

这没有效果:git gui 总是在我的主目录中打开。 (另外,shell-process-pushd/popd 没有记录。)

这也行不通:

(start-process "gitgui" nil "git" "gui")

这也不行:

(let '(bufdir (file-name-directory (buffer-file-name))) 
       (with-temp-buffer
         (print bufdir)
         (cd bufdir)
         (shell-command "git gui")))

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    您确定在 emacs-lisp 中使用尾部斜杠来指示目录名称吗?

    (let ((default-directory "~/.emacs.d"))
      (shell-command-to-string "echo $PWD"))
            ;;; ⇒ "/Users/me"
    
    (let ((default-directory "~/.emacs.d/"))
      (shell-command-to-string "echo $PWD"))
            ;;; ⇒ "/Users/me/.emacs.d"
    

    【讨论】:

      【解决方案2】:

      (shell-command) 确实使用缓冲区的default-directory,这意味着没有理由将cd 到缓冲区的目录。

      我的问题是我错误地在没有关联 .git/ 目录的缓冲区上运行 git gui

      【讨论】:

        【解决方案3】:

        试试这个:

        (let ((default-directory "~/.emacs.d/")) (shell-command "ls"))
        

        【讨论】:

        • 谢谢,但default-directory已经设置为所需的目录。它对shell-command 根本没有任何影响。顺便说一句,我没有附加到shell-command,如果还有其他我不知道的功能,我会很高兴听到它。
        • default-directory 可以设置为 localglobal 并且它只是被覆盖。使用default-directory 而不是let 和只设置它而不绑定到命令或缓冲区是有区别的。 (let ((default-directory "~/.emacs.d/")) (call-process "ls" nil 't)) 也适合我。
        • 好吧,我猜你不在 Windows 上,或者我正在使用的 Windows emacs build 存在错误。在 Mac OS X 自制 emacs 构建中,即使只是 (shell-command "git gui") 也适用于我,而无需 (let ...)。您使用的是什么 emacs 版本和操作系统?
        • 我在 Windows(22 版本)中的旧 emacs 中尝试过:(let ((default-directory "t:/")) (shell-command "dir")) 工作正常。
        • 原来我在一个没有.git/ 目录的文件上运行git gui。是的,我觉得自己很愚蠢。
        猜你喜欢
        • 2012-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-31
        • 2011-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多