0

My go to method for changing a key in a major mode is to do this:

(eval-after-load "org"
  '(progn
     (define-key org-mode-map (kbd "C-k") nil)))

But for C-k which is bound to org-backward-heading-same-level - this doesn't do anything.

When i check C-h k and press C-k, I get the following:

C-k runs the command org-backward-heading-same-level (found in org-mode-map), which is an interactive byte-compiled Lisp function in ‘org.el’.

I also tried:

  • with-eval-after-load, and
  • define key with [remap ...]

They also didn't work.

My goal is to make C-k do nothing.

Vinn
  • 263
  • 2
  • 7

1 Answers1

2

C-k is bound to org-kill-line by default (try with emacs -Q), not to org-backward-heading-same-level (which is bound to C-c C-b by default).

One possibility is that somewhere in your init file, you rebind C-k to org-backward-heading-same-level but that has nothing to do with loading org, so the eval-after-load may trigger early, but you rebind it afterwards. You should check your init file(s) for such a setting and just expunge it.


[Feel free to stop reading here and try to fix your problem. Afterwards and if you are curious, read the following, although I don't think it has anything to do with your immediate problem; but it may be useful in various obscure situations in the future.]

There is an additional wrinkle with C-k in particular however, that might be worth knowing (although it's firmly in the advanced category). C-k is normally bound to kill-line in most modes. Org mode redefines it but not by setting C-k in the keymap, but by remapping the kill-line command to org-kill-line - see org-keys.el:

(org-remap org-mode-map
       'self-insert-command    'org-self-insert-command
       'delete-char            'org-delete-char
       'delete-backward-char   'org-delete-backward-char
       'kill-line              'org-kill-line
       ...

You can undefine such remappings, but you have to use something like the following:


(define-key org-mode-map [remap kill-line] nil)

although you should think through the implications.

And even after you do this, C-k is still bound in the global map to kill-line, so if you really want to make it do nothing, you will need to undefine it there as well. That is something I would NOT recommend however.

As I mentioned above, this is probably NOT the problem you are having; it is something to keep in the back of your mind for future reference. The moral of the story is that keymap handling can be a complicated subject in Emacs.

NickD
  • 27,023
  • 3
  • 23
  • 42
  • THank you for the detail here. I looked into it and when i run `emacs -Q`, you are correct the command is different. However, I have no place where i assign this function to a key. Looking deeper, `C-h k` says the following: `It is bound to C-c C-b, C-k, g k, C-k, g k, .` – Vinn Mar 31 '23 at 18:12
  • This function seems to have many keybinding that I did not set. But when I look at they `org-mode-map`, i can see its only assigned to one key. – Vinn Mar 31 '23 at 18:13
  • If `emacs -Q` does not show the bindings, then your init file(s) (or maybe the init files that the distro adds - what exactly are you running both in terms of Emacs and in terms of an OS?) are the culprit: there is no other way for the keybindings to happen. So bisect it (them) to figure out what's doing this. See [How do I troubleshoot Emacs problems?](https://emacs.stackexchange.com/questions/28429/how-do-i-troubleshoot-emacs-problems) for some methods. – NickD Mar 31 '23 at 19:42
  • In my case (standard Emacs on Fedara 36), `C-h w org-backward-heading-same-level` gives me two bindings: `C-c C-b` and the menu binding ` `. I found https://lists.gnu.org/archive/html/emacs-orgmode/2017-04/msg00082.html where `gk` is an Evil keybinding that's added. No `C-k` though. – NickD Mar 31 '23 at 20:10