10

Very similar to this question, particularly the second part to this answer:

From the diff view of a specific commit, is there a way to jump to the current+editable version of a modified file? If I press RET on the file, Magit takes me to a read-only view of that file from that commit; is there a simple way to instead jump to the current version of that file so I can edit it?

(My current workflow is to use find-file-in-project to load the file but it'd be so much faster if I could directly navigate to it via Magit.)

4
  • 1
    The HEAD version of a modified file is the file as it was at the most recent commit. That's not the same thing as the current/editable file (which will contain the modifications). Which do you want?
    – phils
    Jan 19, 2019 at 10:02
  • Good point; I want current/editable file. (Corrected question's working to make more clear.) Jan 21, 2019 at 18:55
  • 1
    I'm actually still slightly confused by the question. If you're looking at a commit in Magit then just type C-RET instead of RET on one of the hunks for that file, and Magit will take you to the current file (at maybe the desired position, but interim changes might mess with that). I originally thought you meant you were viewing a revision of a single entire file as it was at the point in time of a given commit (in which case I couldn't see an existing key binding to jump to the current file).
    – phils
    Jan 22, 2019 at 20:28
  • @phils Sorry I wasn't more clear in my use case: I talked about pressing RET because that I what I knew, but I was looking for the behavior of C-RET. Thanks for helping me clarify my question. Jan 23, 2019 at 19:04

3 Answers 3

20

To visit the current version of the file in the working tree press C-RET instead of RET. By the way, the latter takes you to the blob that added the current line when point is on a line beginning with + and to the version that last had that line on a line beginning with -.

1
  • I did not know that about RET -- that it jumps to the correct blob based on whether point is at + or -. Thank you for including that note! Jan 23, 2019 at 19:06
0

You can achieve the wanted behaviour by typing

^
C-e
M-x
ffap

Now you just need to replace each shortcut with the corresponding function:

(defun magit-open-current ()
   "Open the current version of a modified file."
   (interactive)
   (magit-section-up)
   (move-end-of-line nil)
   (ffap))

If you want to open the file immediately without confirmation, replace the last line with

   (find-file (ffap-file-at-point)))
0

I was looking for the same thing. While C-RET appears to be working for many people, it does not in my set up. It took me some time to figure out what the key binding is. I'm sharing how I did it in case other people gets into the same situation:

  1. In the magit-diff buffer, go to a line you want to visit.
  2. Run M-x magit-diff-visit-worktree-file (this the function we want to call).
  3. When this command is run, it will
    • Bring you to the corresponding current/editable version of file (I reckon the magit term of it is worktree file)
    • Tell you in the mini buffer what the key binding is. For me, it is C-j (Magit 20230530.1403 [>= 3.3.0.50-git], Transient 0.4.0, Git 2.34.1, Emacs 27.1, gnu/linux)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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