Search code examples
emacselispdired

Launch magit directly from ido in Emacs


I want to launch magit directly from ido. I.e., I want to launch ido with ido-find-file, navigate to the directory where I want to run magit, press some key combination like C-x g, and have ido quit and and magit open in that directory.

I currently just press C-d to open dired in the directory, and then press C-x g to open magit, but I would like to avoid that extra C-d.

I managed to add a new key-combination to ido-completion-map:

(add-hook 'ido-minibuffer-setup-hook
          (lambda () (interactive)
            (define-key ido-completion-map (kbd "C-x g") 'arvid-ido-enter-magit-status)
            ))

But the following function does not seem to work, just opening dired with instead of magit:

(defun arvid-ido-enter-magit-status ()
  "Drop into `dired' from file switching."
  (interactive)
  (setq ido-exit 'dired)
  (magit-status default-directory)
  (exit-minibuffer))

Solution

  • Magit already comes with such a function:

    (defun ido-enter-magit-status ()
      "Drop into `magit-status' from file switching.
    
    To make this command available use something like:
    
      (add-hook 'ido-setup-hook
                (lambda ()
                  (define-key ido-completion-map
                    (kbd \"C-x g\") 'ido-enter-magit-status)))
    
    Starting with Emacs 25.1 the Ido keymaps are defined just once
    instead of every time Ido is invoked, so now you can modify it
    like pretty much every other keymap:
    
      (define-key ido-common-completion-map
        (kbd \"C-x g\") 'ido-enter-magit-status)"
      (interactive)
      (with-no-warnings ; FIXME these are internal variables
        (setq ido-exit 'fallback fallback 'magit-status))
      (exit-minibuffer))