I'm new to emacs, I have the following keybinding which invokes my custom keybinding.
(eval-after-load 'evil-ex
'(evil-ex-define-cmd "mf" 'someFun))
It calls someFun correctly, but what if I want it to have default switches like "-ignore-case" to be directly associated with this key binding. How can I do that?
Related
In Emacs' tide-mode (typescript development) I would like to use M-q, which is normally bound to fill-paragraph, to rather run tide-format. I have a mode hook like
(defun setup-tide-mode ()
...
(local-set-key [M-q] 'tide-format)
(describe-key [M-q]))
(add-hook 'typescript-mode-hook #'setup-tide-mode)
When I open a typescript file I do see the *Help* buffer which indeed shows
<M-q> runs the command tide-format ...
Yet when I then run C-h k M-q to describe the key binding of M-q, I get
M-q runs the command fill-paragraph
There is this suspicious difference in the printout between <M-q> and M-q. This is probably telling me something, but I don't know what.
What would be the correct way to locally overwrite M-q to run a different command?
You want to use [?\M-q] instead of [M-q] because ?\M-q is the event generated when you press the Alt/Meta modifier along with the Q key.
I have written a fancy function, which I would like to bind to TAB. The functionality is only meaningful in any non-read-only text buffer. Currently, I bind it either like that:
(global-set-key (kbd "<tab>") 'my-indent-region)
or
(define-key global-map (kbd "<tab>") 'my-indent-region)
The problem with this binding is that now tab-completion does no longer work in the minibuffer, which is an essential feature (e.g. for buffer/file names, or M-x).
Is it possible to bind TAB only for regular modes? I know that I can use define-key some-major-mode-map, but since I want it in all modes except for the minibuffer, this would be annoying to maintain. Thus, I'm probably looking for something like a define-key any-mode-except-minibuffer ....
If such a functionality does not exist: Is there a workaround to get the tab-completion working in the minibuffer again? Maybe I can re-set the original minibuffer tab binding after changing the global binding? I couldn't figure out though which function I actually have to bind to make it work.
After some more research I found a workaround/solution to the problem in this answer.
Apparently, my problem was that I was binding to (kbd "<tab>"). If I understand it correctly, my problem was in fact not that I overwrote the actual keymap of the minibuffer -- I guess they are correctly loaded when entering the minibuffer minor modes. However, there seems to be a precedence of a binding to (kbd "<tab>") over a binding to just "\t". According to the above answer, the minibuffer bindings just use "\t", so binding to (kbd "<tab>") shadows them. I'm now using the following binding instead:
(global-set-key "\t" 'my-indent-region)
Now everything seems to be working fine.
Do you see this behavior when you start Emacs without your init file (emacs -Q)? I doubt it. If not, then recursively bisect your init file to find out what is causing the problem.
The minibuffer uses its own keymaps, which are local and which therefore take precedence over global keymap bindings.
However, any minor-mode keymaps take precedence over local keymaps. So if, for example, you have a (global) minor mode turned on that binds <tab> then that will override any binding for that key in the minibuffer keymaps.
Another thing you can do is simply bind whatever command you want to <tab> in the minibuffer keymaps. But again, you should not need to do that, if you want the usual <tab> behavior for the minibuffer.
[Another possible confusion: Some things, such as Isearch, which you might think use the minibuffer do not use it. Isearch uses its own keymap, isearch-mode-map.]
UPDATE after your comment:
Assigning a key in the global map, as you have done, should not affect what that key does in the minibuffer, provided it has a different binding in the minibuffer keymaps. TAB is typically bound in all of the minibuffer completion keymaps (but not in the non-completion minibuffer keymaps).
See the Elisp manual, nodes Completion Commands and Text from Minibuffer for information about the minibuffer keymaps.
To see what the current bindings are for a keymap that is associated with a variable (such as minibuffer-local-completion-map), load library help-fns+.el and use C-h M-k followed by the keymap variable's name. (See Help+ for more information about the library.)
If you do not want TAB to use your global command binding in the non-completion minibuffer maps (minibuffer-local-map, minibuffer-local-ns-map), then just bind it in those maps to whatever command you like. But for the completion maps you should not need to do anything - TAB should already be bound there.
Did you try emacs -Q, to see if something in your init file is interfering? If not, do that first.
Emacs's Delete-selection-mode, modern as it feels, makes it too easy to inadvertently delete code. Thus, I would like it disabled by default. Therefore, I added
(delete-selection-mode f)
to my init.el file but that had no effect. By which I mean, if I highlight a region and type something, the contents of the selection are replaced with the new typing. I also tried toggling the mode with M-x delete-selection-mode command, but that had no effect. I use Emacs 24.1.1 on Windows7, Mac and Linux. What am I missing, is there some other mode or variable that I should be enabling instead?
delete-selection-mode is set by default when CUA mode is enabled, but it can be disabled via M-x customize. CUA settings are a subgroup of the "Editing Basics" subgroup of the top-level "Editing" group. To disable delete-selection mode, use the Value Menu of the "Cua Delete Selection" item and change the setting to Disabled. This will place the following line among the custom-set-variables defined in your .emacs or init.el file: (cua-delete-selection nil). After this has been set, actions such as cut with C-w or pressing the delete key will continue to operate normally on regions.
Specifically, I'd like to change the key bindings of Emacs-Helm. When I run helm-find-files, if I hit C-z on a directory, you can jump into the selected directory. I'd like to change this behavior to Tab. I know the action bound to C-z is helm-execute-persistanet-action. I can achieve this by doing (global-set-key (kbd "<tab>") 'helm-execute-persistanet-action) but then that will capture all other tab actions. I only want tab to run helm-execute-persistanet-action when I'm in helm-find-files
I think what you are looking for is define-key. The expression should look like:
(define-key helm-mode-map [tab] 'a-command)
You could try to advice around the helm-find-files function to declare a variable in-helm-find-files then bind the tab key in the helm keymap using define-key. If the in-helm-find is set then you can call the function you want otherwise use keymap look up to call the function in the global map.
Advicing
http://www.gnu.org/software/emacs/manual/html_node/elisp/Around_002dAdvice.html#Around_002dAdvice
Helm Keymap
https://github.com/emacs-helm/helm/blob/master/helm.el#L101
Keymap lookup
Given an emacs command name, how would you find key-bindings ? (and vice versa)
First of all, find out what major mode is active in the buffer in which you want to change the key binding. You can do that with C-h v major-mode, or look in your mode-line.
Then, use local-set-key to create the binding for that major mode only by putting some code in the mode hook. I'm not familiar with helm, but let's say the major mode is called helm-mode, and it has a hook helm-mode-hook and the command you want to bind is called helm-do-something:
(add-hook 'helm-mode-hook
(lambda () (local-set-key [tab] 'helm-do-something)))
Is there a command to globally override a keybinding such that it overrides even the local settings of major modes? global-set-key is overridden by major mode bindings, as stated here: http://www.gnu.org/software/emacs/manual/html_node/emacs/Rebinding.html
No, there is no (built-in) way to set up a key binding that overrides all others. Look at how Emacs searches the keymap by reading "Searching the Active Keymaps".
You could set overriding-terminal-local-map or overriding-local-map to a keymap containing the binding you want, but that'd prevent your buffer from having any buffer/overlay/minor-mode keymaps, pretty much disabling the majority of Emacs.
The next area Emacs looks for a binding is in the character property at the current point - which probably isn't used all over the place, but it's one way your binding would be overridden (unless you muck with character properties to define your key everywhere, really icky).
The next place Emacs looks is in the variable emulation-mode-map-alists, which is probably your best bet. It was set up for packages to use in cases where there are multiple minor-mode keymaps it wants to juggle.
Make a global minor mode (see Defining Minor Modes), put your key binding in there, add your minor mode and keymap into the emulation-mode-map-alists, and turn on your minor mode.
Your key binding will now have precedence over all others, except those earlier in the emulation-mode-map-alist list, or found in character properties, or in the overriding-local-map...
I believe that's the best you can do, w/out hacking Emacs source.
In the case of minor mode keybindings overriding my personal global bindings i have had luck using add-hook + local-unset-key
(add-hook 'undo-tree-mode
(lambda ()
(local-unset-key "C-/")))