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

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)

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.

Related

Emacs lisp key binding in shell mode not working

I have my emacs editor setup to run a shell when I press Ctrl-z. When inside of a shell buffer, I use Ctrl-z to clear the buffer by running the erase-buffer function. The code is being evaluated (when I Ctrl-h v and describe the shell-mode-map I can see that C-z is bound to clear-shell-buffer in shell mode. When I run the clear-shell-buffer with M-x the message says:
You can run the command clear-shell-buffer with <C-z>
However, when I type Ctrl-z in the shell it does not run the function or give any messages at all. Any idea why?
(defun clear-shell-buffer ()
"Clear the contents of the current buffer"
(interactive)
(erase-buffer)
;; (insert "/usr/games/fortune -a")
(comint-send-input)
)
(put 'erase-buffer 'disabled nil)
(eval-after-load 'shell
'(define-key shell-mode-map [(\C-z)] 'clear-shell-buffer))
This is happening because of the key binding being incorrect. You can verify this by doing C-h k C-z when in shell mode.
Instead of [(\C-z)], use one of these options:
[(?\C-z)]
[(control ?z)]
(kbd "C-z")
which will correctly set the key binding, and let you call the correct function with C-z
You can see a little bit of what's happening if you evaluate just the those statements. Here's the output I get for each
(define-key shell-mode-map [(\C-z)] 'clear-shell-buffer)
;;Output: (define-key shell-mode-map [(C-z)] (quote clear-shell-buffer))
(define-key shell-mode-map [(?\C-z)] 'clear-shell-buffer)
;;Output: (define-key shell-mode-map [(26)] (quote clear-shell-buffer))
You can see that the types differ for the key binding. Right now, you're passing a symbol, when you want to be passing a character code.

emacs: assign single key-binding to multiple commands, using the universal argument

Here is my attempt:
(global-set-key [M-left] (key-binding (kbd "C-u C-#")))
After I evaluate the above expression, invoking alt + left gives me the message <M-left> is undefined. The following, however, works:
(global-set-key [M-left] (key-binding (kbd "C-u")))
But this is only the universal argument part of my command. How do I combine these two commands into one Emacs key-binding?
There are two ways to do this: define a Keyboard Macro interactively or write a function:
(define-key global-map [M-left]
(lambda ()
(interactive)
(set-mark-command t)))
sds has provided the solutions, but for clarification, if you evaluate (key-binding (kbd "C-u C-#")) you'll see that it returns nil -- because that is not a bound key sequence.
In fact C-u runs the command universal-argument, which takes care of reading a subsequent key sequence from the user (C-# in your case).

emacs, flyspell, deactivate "C-." key binding

I have this little problem, I have some key bindings like so C-. C-x or C-. C-m. After I activate the flyspell-mode, I cannot use these commands. In my .emacs file I have the next 2 lines before
(global-unset-key (kbd "C-."))
(define-key (current-global-map) (kbd "C-.") nil)
(global-set-key (kbd "C-. C-l") 'global-linum-mode)
Then, my C-. C-l works, but it does not when the flyspell-mode is activated. The command bound to C-. is flyspell-auto-correct-word. I tried to deactivate it as follows:
;; first try
(defun flyspell-auto-correct-word-disable() (define-key (current-local-map) (kbd "C-.") nil))
(add-hook 'flyspell-mode-hook 'flyspell-auto-correct-word-disable)
;; second try
(define-key (current-global-map) [remap flyspell-auto-correct-word] nil)
None of the tries work, what can I do? I tried in Emacs 23 and 24 and I have the same issue.
What about:
(eval-after-load "flyspell"
'(define-key flyspell-mode-map (kbd "C-.") nil))
Your first solution is almost correct, but you have to remember that the current local map is set up by the major mode, not minor modes. The best option you have it to directly access flyspell-mode-map and modify it (another option would be to find it in minor-mode-map-alist but I think it would be needlessly complicated).
Also, I prefer putting such mode-specific settings within eval-after-load (which means they will be evaluated once) rather than in a hook (in which case the settings are evaluated multiple times: each time one buffer activates flyspell-mode). But this is a matter of preference and either way is fine.

emacs - set shortcut key only in major mode?

I would like to override C-l and use it to do M-x erase-buffer followed by simulating hitting RET, only when I am in m-shell-mode. C-l should be its default, recenter-top-bottom, otherwise. How do I do so?
Not sure what m-shell-mode is, but if it's a well-defined major mode, then the following should do the trick:
(require 'm-shell-mode)
(define-key m-shell-mode-map (kbd "C-l") 'erase-buffer)
Might I suggest an alternative binding, which has the same visual effect, but keeps the buffer contents around (which can be handy).
(defun shell-clear-command (&optional a)
"\"clear\" the screen"
(interactive "P")
(recenter (or a 0)))
(define-key m-shell-mode-map (kbd "C-l") 'shell-clear-command)
If m-shell-mode is based on comint-mode, which is true of many modes that provide a shell to interact with another process, then you can pass the return keypress to matlab with the function comint-send-input. In that case the following code should do what you want:
(defun clear-and-return ()
"Erases the buffer, and then passes a return to the buffer process.
Assumes the buffer is attached to a comint process."
(interactive)
(erase-buffer)
(comint-send-input))
(defun my-m-shell-mode-hook ()
(local-set-key (kbd "C-l") 'clear-and-return))
(add-hook 'm-shell-mode-hook 'my-m-shell-mode-hook)
The first defun makes a function that does what you want. The second is a hook function that will bind C-l to that function for the buffer that is active when the function is called. The add-hook tells emacs to run the second function whenever you start m-shell-mode. You can add further m-shell-mode customizations inside the body of my-m-shell-mode, and Emacs will run all of them each time you start the mode.
If m-shell-mode is not based on comint-mode, you need to find out what happens when you press return. From a buffer that is running the mode, type C-h k RET to find the function bound to the return key. Use that function instead of comint-send-input in the code above.
You can add to your m-shell-mode hook the following code:
(local-set-key (kbd "C-l") 'erase-buffer)

How can I bind compile using comint to a key combination

I currently bind compile to C-x c. I know I can run compile in comint mode by using C-u C-x c but I'd prefer to just bind it to C-x c directly. I can't fathom how to do this without copying the whole of the compile function from compile.el, tweaking it and binding that. Is there a better way?
Edit: To clarify my sloppy language, I don't wish to bind C-x c whilst in comint mode. I wish to cause C-x c to run 'compile using comint mode. I currently have C-x bound to 'compile. I can do what I want by typing C-u C-x c but I'd prefer to just be able to type C-x c to do that.
I think this works...
(defun c-w-c ()
(interactive)
(call-interactively 'compile t (vector 21 (this-command-keys-vector))))
(global-set-key (kbd "C-x c") 'c-w-c)
the '21' prepended into the vector is the ctrl-u prefix key, and it seems to fool the compile function into thinking it was called with C-u C-x c.
Edit:
It didn't work, but this does:
(defun c-w-c ()
(interactive)
(setq current-prefix-arg '(4))
(call-interactively 'compile))
You can do something like this :
(global-set-key [(C-f5)] 'compile)
(global-set-key [(f5)] 'recompile)
It binds compile to C-f5 and each time you want to recompile with the same command as you've given in compile, just type f5. It works whatever the major mode you're currently in.
For your case, do like this :
(global-set-key [?\C-x ?c] 'compile)
Are you asking for this?
(define-key comint-mode-map (kbd "C-x c") 'compile)
This works too:
(define-key comint-mode-map (kbd "C-x c")
(lambda (command)
(interactive
(list
(let ((command (eval compile-command)))
(if (or compilation-read-command current-prefix-arg)
(compilation-read-command command)
command))))
(compile command t)))
It's ugly because it duplicates the "interactive" spec from the compile command.