when i type C-u F9 I want anything.el to pop up a buffer of choices as if I had typed
M-x anything -shell* manually.
In other words, I often invoke anything and look for all my shell buffers and so I would like to simplify this process.
1. You can define a simple function calling anything with a pre-filled -shell* input, and bind it to a key (for example F9):
(defun my/anything-shell ()
(interactive)
(anything :input "-shell*"))
(global-set-key (kbd "<f9>") 'my/anything-shell)
2. If your F9 key is already bound to usual-f9-command and you want anything-shell to be called only when you specify a prefix argument (with C-u F9), then your key binding must be a bit more complex:
(global-set-key (kbd "<f9>") (lambda (&optional arg)
(interactive "P")
(if arg
(my/anything-shell)
(usual-f9-command))))
Related
In Emacs, with the idea of binding a keychord to another keychord, why is this elisp code not working?
(global-set-key (kbd "C-f") (kbd "C-s"))
(The idea being, in this example, to get "C-f" to do the same as "C-s".)
I don't know why it not work either, but the usual way to achieve your purpose is just binding a key to a interactive command, instead of a string or keyboard macro, in this case (kbd "C-s"):
(global-set-key (kbd "C-f") #'isearch-forward)
or (the above is better)
(global-set-key (kbd "C-f") (key-binding (kbd "C-s")))
According to the documentation, I was expecting the following to be true: What you are attempting doesn't work, because you're binding the result of (kbd "C-s") to a key, which is an internal Emacs key representation and not the function that is (globally) set to the key. However, as xuchunyang pointed out, that's not entirely correct. global-set-key calls define-key internally which has in its documentation the following part:
(define-key KEYMAP KEY DEF)
In KEYMAP, define key sequence KEY as DEF. KEYMAP is a keymap.
KEY is a string or a vector of symbols and characters, representing a
sequence of keystrokes and events. [...]
DEF is anything that can be a key's definition:
nil (means key is undefined in this keymap),
a command (a Lisp function suitable for interactive calling),
a string (treated as a keyboard macro), [...]
And indeed, xunchunyang's example (global-set-key [?l] (kbd "C-f") works as you expect. (global-set-key [?l] (kbd "C-s") doesn't work, because isearch-forward expects an argument (the search regexp) which interactive will ask for. Only if you provide one, this particular keyboard macro will work, i.e. (global-set-key [?l] (concat (kbd "C-s") "foo")) will bind a search for "foo" to the key "l".
xuchunyang's answer is absolutely right: the best way is to name the function explicitly via (global-set-key (kbd "C-f") #'isearch-forward).
You can easily figure out the function that is bound to a key by typing C-h k and then the key which you want to "copy": e.g. C-h k C-s) will show you that C-s is (usually, cf. below) bound to isearch-forward.
The approach using key-binding could, in the general case, lead to strange results: depending on where and how you execute this (i.e. interactively in some buffer or in your .emacs), results may differ, because key-binding honours current keymaps, which might assign different functions to keys.
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).
I'm trying to map c-u m-x indent-pp-sexp to a single key, like F5, so that working with Emacs doesnt erode my fingerprints.
I use (global-set-key (kbd "C-u M-x indent-pp-sexp") "<f5>") but i'm getting the following error:
global-set-key: Key sequence C-u M-x i n d e n t - p p - s e x p starts with non-prefix key C-u
EDIT
With this lambda function (global-set-key (kbd "<f5>") (lambda (interactive) (universal-argument) (indent-pp-sexp t)))
Getting error:
recursive-edit: Wrong type argument: commandp, (lambda (interactive) (universal-argument) (indent-pp-sexp t))
Weird, because univeral-argument takes no parameters, and indent-pp-sexp takes boolean
You have the arguments the wrong way around, and you bind keys to functions, not to other key sequences. Perhaps you are really looking for a named macro; or you can write some actual Lisp and bind that to F5:
(global-set-key (kbd "<f5>")
(function (lambda () (interactive) (indent-pp-sexp t) )) )
The presence of an argument in the call form appears to be sufficient to select the prefix argument functionality.
You're missing the argument list to the lambda. Additionally I think passing t to indent-pp-sexp negates the need to call universal-argument.
(global-set-key (kbd "<f5>") #'(lambda ()
(interactive)
(indent-pp-sexp t)))
I'm a noob like you, but I already happened to figure basic things like making macros. I don't really know what's wrong with your code, but here's walkthrough of how I do things at home. What you need to do first, is press F3. Then type your keystrokes, and when finished, press F4. Congratulations, you have defined an anonymous macro. You can replay it as many times you wish by pressing F4 again. When you have played enough, enter M-x name-last-keybord-macro, and name it eg. foobar. Go to your ~/.emacs.d/macros/ directory (make it if you don't have one) and visit a file that you will name foobar.el. In its buffer, M-x insert-kbd-macro. When asked about name, say foobar. You will see that emacs has entered the contents of your just recorded macro into the file. Save it. Open your .emacs file, and add lines:
(load (expand-file-name "~/.emacs.d/macros/foobar.el"))
(global-set-key (kbd "M-<f5>") 'foobar)
And things start working for me after restart, with M-F5 as the binding for foobar.el macro.
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)
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)