Free Cmd-W in Magit to Close the Buffer

Magit binds M-w to magit-copy-buffer-revision. On my Mac, I use the left Command () key as the Meta key, though, and wired M-x, M-c, M-v to cut/copy/paste, leaving M-w aka +W to close the current buffer.

Magit defines this key binding in a lot of modes, though. The status mode has its own key map, the log mode has its own key map – so I did the only sensible thing and removed this from all maps I could find.

Update 2022-06-10: Magit author Jonas Bernoulli pointed out I’m doing everything wrong, there’s just 1 key-map, all the others derive from it. So the actually sensible thing to do is just this:

(unbind-key "M-w" magit-mode-map)

In my defense I would like to complain that this fact isn’t very obvious from the built-in Emacs help!!1

Old approach before Jonas showed the true way

Here’s the previous code to unbind M-w from all Magit mode key maps:

(let ((all-magit-mode-maps
       '(magit-status-mode-map
         magit-mode-map
         magit-log-mode-map
         magit-blob-mode-map
         magit-diff-mode-map
         magit-refs-mode-map
         magit-blame-mode-map
         magit-stash-mode-map
         magit-cherry-mode-map
         magit-reflog-mode-map
         magit-process-mode-map
         magit-section-mode-map
         magit-stashes-mode-map
         magit-repolist-mode-map
         magit-revision-mode-map
         magit-log-select-mode-map
         magit-todos-list-mode-map
         magit-merge-preview-mode-map
         magit-submodule-list-mode-map
         magit-blame-read-only-mode-map)))
    (dolist (mode-map all-magit-mode-maps)
      (unbind-key "M-w" mode-map)))

To get there, I filtered all bound variables for “magit -mode-map”. That produced a list of 20 results.

Thanks to Embark, I ran C-, S ((embark-collect)) to produce a more permanent buffer from the minibuffer’s resuls. There, I copied the variable names with a rectangular selection and concatenated the 20 lines into one via xah-reformat-lines that ships with my modal input of choice, Xah Fly Keys mode. (I could’ve probably also have used fill/unfill shortcuts, I just realize.)

Collected results in a dedicated buffer to edit

Wrap that in a quoted list and loop over it to call unbind-key for each map.

It’s likely that some of these key maps didn’t even bind M-w, but that’s not a problem, it’s just a no-op.