8

In the magit-status buffer, pressing TAB shows or hides a given section, which is handy when looking at unstaged content and see what hunks can be staged.

However, when tabbing on a section, the buffer goes down, so the section I am trying to expand goes to the bottom of the buffer, thus hiding the expanded part of the section. I then have to cycle using recenter-top-bottom to bring the section back to the top of the buffer.

Is there a way to automatically bring the section / entry to the top of the buffer when expanding it?

5

If you're using Emacs v24.3.x, put his in your .emacs

(defadvice magit-toggle-section (after magit-section-hidden activate)
  (recenter-top-bottom 0))

If you're using Emacs v24.4.x, here is what I have come up with using an advice – although I am sure it is not perfect:

(defun magit-toggle-scroll-to-top () (recenter-top-bottom 0))
(advice-add 'magit-toggle-section :after #'magit-toggle-scroll-to-top)
  • The advice-add is void in Emacs v24.3.1, it is new advice facility introduced in Emacs v24.4 – CodyChan Nov 18 '14 at 4:09
  • @CodyChan That's right: for older versions, defadvice should be used. – Sébastien Le Callonnec Nov 18 '14 at 9:31
  • Thank you. Your solution for Emacs 24.4 worked perfectly for me. I've been going mad, thinking that any of the packages which I have is conflicting with Magit. – zeroDivisible Jun 20 '15 at 21:09
4

On the next branch that's already supported. (Which shouldn't keep anybody from giving another answer here, explaining how to get the same effect with master).

  • I misread you question. A similar feature exists when moving between sections, but didn't in the case you actually asked about. But now I have implemented that for toggling sections too, and so this answer is correct now :-) – tarsius Nov 11 '14 at 21:20
  • Awesome, I'll give the next branch a spin. – Sébastien Le Callonnec Nov 11 '14 at 21:44
  • Not that be default it only does this for hunks, replace magit-hunk-set-window-start with magit-section-set-window-start in magit-section-movement-hook and magit-section-show-hook if you would like this behavior for all sections. Now that I think of it doing so for magit-section-show-hook would probably lead to problems. – tarsius Nov 11 '14 at 22:41
  • Turns out this isn't as easy as I though and I had to revert this change for now. – tarsius Nov 12 '14 at 0:27
0

These work on the latest version of magit from Melpa (from master branch) as tested today.

While in the Magit Status window,

  • C-u j u - Jump to the unstaged section, expand it and bring that to the top of the buffer
  • C-u j s - Jump to the staged section, expand it and bring that to the top of the buffer

Similar action will apply to other C-u j .. bindings in the magit-section-jump-map too.

From magit.el in master branch:

(defvar magit-section-jump-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd "z") 'magit-jump-to-stashes)
    (define-key map (kbd "n") 'magit-jump-to-untracked)
    (define-key map (kbd "u") 'magit-jump-to-unstaged)
    (define-key map (kbd "s") 'magit-jump-to-staged)
    (define-key map (kbd "f") 'magit-jump-to-unpulled)
    (define-key map (kbd "p") 'magit-jump-to-unpushed)
    (define-key map (kbd "r") 'magit-jump-to-pending)
    map)
  "Submap for jumping to sections in `magit-status-mode'.")

Answer

To answer your specific application to review the hunks in unstaged section, do C-u j u in the Magit Status buffer.

Applies to the magit master branch code; haven't tried out the next branch.

An example workflow using these bindings

  1. M-x magit-status
  2. Stage the files you want using s
  3. Expand the staged files showing the hunk diffs, while also positioning the top of the staged section to the top of the buffer: C-u j s
  4. Commit c c, opens COMMIT buffer in a split window
  5. Write log while reviewing staged diffs in other window. If diff exceeds the window height, I use the 'scroll other window commands'.
  6. Finish commit C-c C-c.
  • Although these are indeed useful shortcuts, this is not exactly what I was after: I want the current file, or hunk, or section to scroll to the top when I expand it so I can see most of the diff in that current change. – Sébastien Le Callonnec Apr 28 '15 at 19:43

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.