Search code examples
emacskey-bindingscua-mode

Emacs: How to redefine Ctrl-Enter when CUA-mode is enabled?


If cua-mode is enabled, redefining Ctrl-Enter does not works as expected and always runs cua-set-rectangle-mark function. In the code below you can see that I also defined Alt-Enter to my function, just for testing, and it runs fine. But I wish to left Alt-Enter to cua-set-rectangle-mark because I prefer to use Ctrl-Enter to call my function that creates a line below the current line. What is wrong?

(cua-mode t)
(defun vscode-insert-line-below()
  (interactive)
  (move-end-of-line 1)
  (newline-and-indent))
(global-set-key (kbd "C-<return>") 'vscode-insert-line-below)
(global-set-key (kbd "M-<return>") 'vscode-insert-line-below)

Solution

  • This is probably what you want:

    (cua-mode t)
    (defun vscode-insert-line-below()
      (interactive)
      (move-end-of-line 1)
      (newline-and-indent))
    (define-key cua-global-keymap (kbd "<C-return>") 'vscode-insert-line-below)
    

    (You can use either (kbd "<C-return>") or (kbd "C-<return>"), but I like to use the form that C-h k shows me.)

    When you are in cua-mode the local keymap is cua-global-keymap, and its bindings override the same global bindings.

    I found that map by doing C-h k C-RET in cua-mode. It told me:

    <C-return> runs the command cua-set-rectangle-mark (found in cua-global-keymap), which is an interactive autoloaded Lisp function in cua-rect.el.

    It is bound to <C-return>.

    [Arg list not available until function definition is loaded.]

    Start rectangle at mouse click position.