Emacs lisp key binding in shell mode not working - emacs

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.

Related

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)
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.

emacs cider clear REPL buffer

I simply want to clear the repl buffer so that a single prompt eg (user>) is left on the first line.
I have a keybinding:
(put 'erase-buffer 'disabled nil)
(global-set-key (kbd "C-x C-<backspace>") 'erase-buffer)
But this gives the message :
text is read only
There is the option C-c C-o but this only clears the last return value.
When using python, and run-python the following command C-x M-o which i believe is comint-clear-buffer
cider-repl.el provides a function cider-repl-clear-buffer which by default is bound to:
M-x c-r--bu RET
as C-c M-b is not used by cider-repl as far as I am aware:
(add-hook 'cider-repl-mode-hook
'(lambda () (define-key cider-repl-mode-map (kbd "C-c M-b")
'cider-repl-clear-buffer)))
cider-repl.el also provides cider-repl-handle-shortcut which is bound to ,.
Which will prompt you to many commands, such as clear (which you want), ns (to change namespace), refresh, reload and many others
I find pressing , followd by enter (to choose clear, faster/more convenient than the other answer.)
Note: you need to type , into the repl while the line is empty, it works for both evil and normal emacs keybinds

Emacs can't reset Ctrl-d key behaviour

I wanted to change the behaviour of Ctrl-d key. So it will delete a word backward. I created a function:
(defun backward-delete-word (arg)
"Delete characters backward until encountering the beginning of a word.
With argument ARG, do this that many times."
(interactive "p")
(delete-region (point) (progn (backward-word arg) (point))))
Then inserted this into emacs.d:
(global-set-key (kbd "\C-d") 'backward-delete-word)
It works in fundamental-mode, but in php-mode it just removes the next character. When I click
Ctrl-h k Ctrl-d
Emacs gives this:
C-d runs the command c-electric-delete-forward, which is an
interactive compiled Lisp function in `cc-cmds.el'.
It is bound to C-d.
(c-electric-delete-forward ARG)
Somehow, it was reset to another function. How to find out, where it was reset and make it work with my function instead?
I don't have php-mode so I can't say for sure, but the binding is likely overriden in php-mode-map (which, as a major mode map, has higher precedence than the global map).
You can check by using C-h b to list all available key bindings and look for C-d or c-electric-delete-forward in the output buffer to see in which keymap the binding is defined.
Assuming php-mode-map overrides the C-d binding, you can disable it using
(define-key php-mode-map (kbd "C-d") nil)

Insert the output of shell command into emacs buffer

I want to set a key binding to insert the date into the buffer. I have written the following lisp in my .emacs file. Using date as an example:
;;put the date
(global-set-key
(kbd "C-c C-d")
(shell-command "date" (current-buffer))
)
The key binding works okay when I use other commands like 'next-line, but shell-command will put it into the *scratch* buffer when the .emacs is read and leaves it at that.
Maybe I need to use shell-command-on-region.
For the general case of inserting any output of a shell command to the current buffer, you can use the in-built keyboard chords:
C-u M-! <shell-command>
which runs the same shell-command function, and also inserts the output back at the point in the current buffer.
The entire key-stroke itself can be saved as a macro (and perhaps assigned to a shortcut) for easier invocation of common shell commands.
A friend of mine at work helped me.
(defun put-the-date ()
(interactive)
(insert (shell-command-to-string "date")))
(global-set-key
(kbd "C-c C-d")
'put-the-date
)

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)