Run a shell command from a specific directory in emacs

How to execute a shell command (e.g. git gui) from a specific directory? I would prefer a cross-platform approach that does not depend on shell operators such as &&or ;because I need it to work on Windows and unix. (For example, a call cd /path/to/dir && git guiwill not work on Windows because it is &&invalid.)

I tried:

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

This crashes because for some reason the shell thinks it ; git guiis part of the path and it reports:The system cannot find the path specified.

So, I would prefer not to deal with shells, and I hope that there will be an elisp function that sets the directory for shell-commandor async-shell-command. I tried:

 (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")))
+4
3

(shell-command) default-directory, , cd.

, git gui , .git/.

+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"
+5

Try the following:

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

Source: https://habr.com/ru/post/1537151/


All Articles