1

What I'm trying to do is have C-a and C-e take me to the beginning or ending, respectively, of the logical line, not the visual line, in org-mode (with visual-line-mode active). I see that C-a is mapped to org-beginning-of-line, which, like a lot of org-mode things, seems to be context-dependent.

Here's what I'm trying.

        (define-key org-mode-map (kbd "C-a") 'move-beginning-of-line)
        (define-key org-mode-map (kbd "C-a") 'move-end-of-line)
        

This seems to do nothing. C-h k followed by C-a still shows it's still mapped to org-beginning-of-line. I also tried using the angle bracket form of calling kbd: (kbd "<C-a>"), which made no difference.

If it helps, here's what I get when I do C-h m:

Enabled minor modes: Auto-Composition Auto-Compression Auto-Encryption
Blink-Cursor Electric-Indent File-Name-Shadow Font-Lock Global-Eldoc
Global-Font-Lock Global-Visual-Line Line-Number Mac-Mouse-Wheel
Menu-Bar Show-Paren Tool-Bar Tooltip Transient-Mark Visual-Line

UPDATE: both @Tyler and @NickD solved the problem with their answers. Also it turns out the visual-line-mode-map had a remapping as well, which also needed to be unmapped. Here's specifically what I added to my .emacs file to solve the entire thing:

(add-hook 'visual-line-mode-hook
      (lambda ()
        (define-key visual-line-mode-map [remap move-beginning-of-line] nil)
        (define-key visual-line-mode-map (kbd "C-a") 'move-beginning-of-line)            
        (define-key visual-line-mode-map [remap move-end-of-line] nil)
        (define-key visual-line-mode-map (kbd "C-e") 'move-end-of-line)))

(add-hook 'org-mode-hook
      (lambda ()
        (define-key org-mode-map [remap move-beginning-of-line] nil)
        (define-key org-mode-map (kbd "C-a") 'move-beginning-of-line)            
        (define-key org-mode-map [remap move-end-of-line] nil)
        (define-key org-mode-map (kbd "C-e") 'move-end-of-line)))
  • 2
    Does this answer your question? [How to remap control up in org-mode](https://emacs.stackexchange.com/questions/3998/how-to-remap-control-up-in-org-mode) It's not the same issue exactly, but the source of the problem in both cases is the way org-mode remaps commands. – Tyler May 24 '21 at 02:50
  • For the record, and the benefit of future visitors, note that Org mode since recently handles the case of `visual-line-mode` remappings (see https://code.orgmode.org/bzg/org-mode/commit/ccd513a3c5b732561ea1b1d6020f5a05c1565205). – gusbrs May 25 '21 at 11:59

2 Answers2

1

Org-mode remaps move-beginning-of-line to org-beginning-of-line. Consequently, any keybinding you attach to move-beginning-of-line will call org-beginning-of-line. To undo this, you need to undo the remapping:

(define-key org-mode-map [remap move-beginning-of-line] nil)
(define-key org-mode-map (kbd "C-a") 'move-beginning-of-line)
Tyler
  • 21,719
  • 1
  • 52
  • 92
0

Org mode remaps move-beginning-of-line to org-beginning-of-line using this code (in org-keys.el):

(org-remap org-mode-map
       ...
       'move-beginning-of-line 'org-beginning-of-line
       'move-end-of-line       'org-end-of-line
       ...
)

(where I have elided a bunch of other remappings).

I don't understand the mechanism well enough to provide a decent explanation (which is also a warning to take the following suggestion with a large grain of salt), but remapping them afterwards to themselves seems to work (no guarantees, if it breaks you own it, etc.):

(with-eval-after-load "org-keys"
   (org-remap org-mode-map
        'move-beginning-of-line 'move-beginning-of-line
        'move-end-of-line       'move-end-of-line))
NickD
  • 27,023
  • 3
  • 23
  • 42