5

diff-mode allows to move (by C-c C-c) or to apply hunk and to move (by C-c C-a) to position of original source file where hunk is placed.

How can I do that in Magit Rev mode? On RET it opens buffer unrelated to file with corresponding content for file version.

1 Answer 1

7

Note that RET may or may not visit the current file.

C-hkRET tells us:

<return> runs the command magit-diff-visit-file ...

[...]

If the diff shows changes in the worktree, the index, or HEAD, then visit the actual file. Otherwise, when the diff is about an older commit or a range, then visit the appropriate blob.

The optional FORCE-WORKTREE means to force visiting the worktree version of the file. To do this interactively use the command magit-diff-visit-file-worktree instead.

You can use C-RET to call magit-diff-visit-file-worktree:

From a diff, visit the corresponding file at the appropriate position.

[...]

The actual file in the worktree is visited. The positions in the hunk headers get less useful the "older" the changes are, and as a result, jumping to the appropriate position gets less reliable.

Jumping to the current file is, more often than not, what I want to do, and so I use the following to swap the meaning of RET and C-RET.

(with-eval-after-load "magit-diff"
  ;; Swap the meanings of RET and C-RET on diff hunks.
  ;; Note that the default RET bindings are [remap magit-visit-thing]
  ;; in the original keymaps, but I am only concerned with RET here.
  ;; Note also that in a terminal, C-RET sends C-j.
  ;; Using the same key formats here as magit-diff.el
  (define-key magit-file-section-map [return] 'magit-diff-visit-file-worktree)
  (define-key magit-file-section-map [C-return] 'magit-diff-visit-file)
  (define-key magit-file-section-map (kbd "C-j") 'magit-diff-visit-file)
  (define-key magit-hunk-section-map [return] 'magit-diff-visit-file-worktree)
  (define-key magit-hunk-section-map [C-return] 'magit-diff-visit-file)
  (define-key magit-hunk-section-map (kbd "C-j") 'magit-diff-visit-file))

Your Answer

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

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