12

When committing changes in Magit, it is useful to have the diff view open while adding details about the commit.

In Magit, we see the diff in the *magit ..* buffer. When you hit cc while the cursor is on a Staged file, the Magit commit buffer .. COMMIT_EDITMSG pops up. By default, this new buffer replaces the *magit ..* buffer that was showing the commit diffs.

QUESTION - How do I make the .. COMMIT_EDITMSG buffer open in a new window (reuse a different window or create a new window if the frame had just 1 window to begin with) so that I can see both the *magit ..* and .. COMMIT_EDITMSG buffers?

17

It is possible to configure how a buffer opens (in the same frame/window or a different frame/window) using display-buffer-alist. Refer to the references below to learn more about this variable.

Solution

We need to tell emacs to always open buffers ending with COMMIT_EDITMSG in a window but not in the same (*magit ..*) window.

Add the following snippet to the init.el after (require 'magit) to specify this action.

(add-to-list 'display-buffer-alist
                 '(".*COMMIT_EDITMSG". ((display-buffer-pop-up-window) .
                                        ((inhibit-same-window . t)))))

Breakdown of the solution

display-buffer-alist is an alist that calls a FUNCTION (or a list of functions) with ALIST parameters when a CONDITION is true. The display-buffer-alist can be represented as,

'(CONDITION . (FUNCTION . ALIST))
  • Here CONDITION is that the buffer name ends with COMMIT_EDITMSG and that is represented by ".*COMMIT_EDITMSG".
  • The FUNCTION to call is display-buffer-pop-up-window as we want to create a window for this buffer. Instead of specifying the function by itself it's put in as a list of a single function (display-buffer-pop-up-window)so that more functions can be easily added to the list if needed in future.
  • The ALIST contains (inhibit-same-window . t) as we don't want this buffer to open in the same window.

References

2

Starting with v2.1.0 Magit by default shows the changes that are being committed alongside the buffer used to write the commit message.

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.