1

I'm trying to make a setup that will use emacs as a command line tool to switch git branches.

The idea is to run some command in terminal, something like emacsclient -t -e "(magit-checkout)". This will run emacs window with (magit-checkout) command executed in my terminal. I can then select a branch from the list, press enter, emacs will then checkout this branch, window will close and I'm back to my terminal.

I can't make it work though, running emacsclient -t -e "(magit-checkout)" just flashes emacs window in my terminal and closes it immediately. It works almost as I want it to work with (magit-commit) command (the only thing that doesn't work is that emacs window won't close after the operation is done). But it doesn't work with (magit-checkout). Is there any way to make it work the way I described?

  • 1
    magit-checkout needs one argument, you can try emacsclient -t -e "(call-interactively #'magit-checkout)" instead. Personally, I use only magit-status (something like emacsclient -t -e '(magit-status)') and start various git operation from the status buffer. – xuchunyang Aug 22 '18 at 12:48
  • the comment from @xuchunyang might solve the problem for you. However, Emacs can be a bit awkward when used as a command-line tool. The usual magit workflow is to do everything from Emacs, so you don't need to switch back and forth between Emacs and your terminal. Magit provides all your git features, and you can compile, browse directories, and even run terminals (via M-x shell or M-x eshell) from Emacs as well. What you're doing is fine of course, there are no rules! But it might be worth investigating how to do the stuff you're doing in your terminal in Emacs instead. – Tyler Aug 22 '18 at 14:08
  • Why do you want to use Emacs to switch git branches? Wouldn't it be far easier to use git check out a different branch? Is it for the branch list and selection? – zck Aug 22 '18 at 18:40
  • Thank you @xuchunyang, this works great! Can I ask if may be you also know how to achieve this "close the emacs window after the branch is selected" behaviour? It's not a big deal, but it's nice to have. – Dmitry Ryadnenko Aug 23 '18 at 7:50
  • 1
    @DmitryRyadnenko You can type C-x 5 0 or use emacsclient -t -e "(call-interactively #'magit-checkout)" -e "(delete-frame)". However, I think you cam simply use emacsclient -e "(magit-status)" then checkout/commit/diff/pull/etc from here. I prefer this way since I am not familiar with the git command line interface. – xuchunyang Aug 23 '18 at 8:17
2

As outlined in the comments by others you can use something like:

 emacsclient -t -e "(call-interactively #'magit-checkout)" -e "(delete-frame)"

Of course you would have to create a script or alias for that.

But like the users who have commented on your questions, I too would recommend using either plain old git checkout or going all in with Magit, doing everything from there. If you stick to the former, then you should look into teaching Zsh's completion mechanism about fuzzy matching - I would assume that that is possible somehow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.