Journal

2024-04-08 Even more Magit tips

Almost five years ago I wrote a short post with some Magit tips. Well, why not write some more? Magit is slowly but constantly evolving, and recently I discovered something very useful I didn’t even know existed.

Magit defines the variable magit-define-global-key-bindings, with a default value being, well, the symbol default. This value means that Magit defines a few keybindings accessible from anywhere in Emacs. These are:

  • C-x g for magit-status. As far as I remember, this has always been the recommended choice. I am pretty sure that early Magit versions did not bind this by default, since I had this binding in my init.el, but now this Just Works™ (unless you explicitly set magit-define-global-key-bindings to nil, of course, or bind C-x g to something else – Magit does not rebind keys you have already bound).
  • C-x M-g for magit-dispatch. The command has a rather short docstring, but from what I understand after trying it out and reading about it in the manual, it is roughly equivalent to magit-status but without actually displaying the status. What I mean by this is that, for example, C-x g followed by l l shows the Git log of the current branch, and C-x M-g l l does exactly the same, but without showing the Magit status buffer, and pressing q in the log buffer then gets us back to the buffer we were in beforehand. That’s nice, but not indispensable.
  • C-c M-g for magit-file-dispatch. This is definitely the most interesting and non-obvious command of these three. It shows a transient menu for Magit commands operating on the current file. For instance, unlike magit-status and magit-dispatch (where the concept of blaming doesn’t make sense, since you can’t run git blame on the whole repository), you can run magit-blame from that menu. Even better, l runs magit-log-file-buffer, which shows Git log restricted to the current file. You can also stage a file with it and do a bunch of other, perhaps less often useful (but not less useful!) stuff. For instance, poking around I learned about the magit-edit-line-commit command. It is esoteric enough to be disabled by default, but the concept is really cool – it starts an interactive rebase where you can edit the commit which added the line you are in. This way, if you spot some simple mistake like a typo (on a yet unpushed branch), you can easily fix it “in place”, that is, in the commit it was made.

The only drawback I can see is that the C-x M-g and C-c M-g bindings are not very easy to remember. This can be remedied with setting magit-define-global-key-bindings to recommended. In that case, magit-dispatch gets bound to C-c g (I assume that here, g stands for “global”) and magit-file-dispatch to C-c f (supposedly, f standing for file). These bindings could not be made default because keys like C-c <letter> are reserved for the user, but it’s of course fine to use them if the user explicitly asks for that.

That’s it for today, I really hope this is useful for some of you out there!

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryGit

Comments on this page

More...