Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run a shell command from a specific directory in emacs

Tags:

emacs

elisp

How do I execute a shell command (eg, git gui) from a specific directory? I'd prefer a cross-platform approach that doesn't depend shell operators like && or ; because I need this to run on Windows and unix. (For example, calling cd /path/to/dir && git gui won't work on Windows because && is not valid.)

I tried:

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

That fails because for whatever reason the shell thinks ; git gui is part of the path, and it reports: The system cannot find the path specified.

So, I'd rather not deal with shell quirks and I'm hoping there's an elisp function that sets the directory for shell-command or async-shell-command. I tried:

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

That had no effect: git gui always opens in my home directory. (Also, shell-process-pushd/popd are not documented.)

This also doesn't work:

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

Neither does this:

(let '(bufdir (file-name-directory (buffer-file-name))) 
       (with-temp-buffer
         (print bufdir)
         (cd bufdir)
         (shell-command "git gui")))
like image 286
Justin M. Keyes Avatar asked Apr 18 '14 23:04

Justin M. Keyes


People also ask

How do I run a shell command in Emacs?

You can execute an external shell command from within Emacs using ` M-! ' ( 'shell-command' ). The output from the shell command is displayed in the minibuffer or in a separate buffer, depending on the output size.

How do I access Emacs shell?

To enter Emacs, type emacs at the shell prompt. When you want to leave Emacs for a short time, type a C-z and Emacs will be suspended. To get back into Emacs, type %emacs at the shell prompt.

How do I change directory in Emacs?

You can type the 'cdcdThe cd command, also known as chdir (change directory), is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.https://en.wikipedia.org › wiki › Cd_(command)Cd (command) - Wikipedia' emacs command. ( M-x cd ) to change the default folder as a one off. Save this answer.

How do I run a script in Emacs?

If the script is executable, you can execute it with C-c C-x ( executable-interpret ). You'll be given the opportunity to pass arguments. (This command works no matter what language the program is written in, by the way, as long as the file you're editing is executable.)


1 Answers

Are you sure you're using the trailing slash to indicate directory name in 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"
like image 69
event_jr Avatar answered Oct 14 '22 13:10

event_jr