(defun foo ()
message "hello")
I want to binding the function foo to key 'ctl + l' in the shell mode
can someone show me the code?
First, you need to fix your brackets and make the function interactive.
(defun foo ()
(interactive)
(message "hello"))
Then you need can bind this to a key while in shell-mode.
(local-set-key (kbd "C-i") 'foo)
To make it permanent, add a function to set this in the shell-mode-hook or (preferably) add it the keymap for the mode like so
(eval-after-load "shell"
'(define-key shell-mode-map (kbd "C-i") 'foo))
Related
I have been trying to make a few changes to ido-mode to make it more useful. One of the things which I have been trying to do is to remap some of the keys which I use in ido-find-file. The main one is that I want to use C-d to call the ido-enter-dired function instead of having to press C-f+C-d which does the same thing.
This is my ido setup so far:
(defun ali/ido ()
"My configuration for ido-mode"
(require 'ido)
(setq ido-create-new-buffer 'always)
;; Making sure that ido works in M-x
(global-set-key
"\M-x"
(lambda ()
(interactive)
(call-interactively
(intern
(ido-completing-read
"M-x "
(all-completions "" obarray 'commandp))))))
;; Ido keybindings
(defun ido-keybindings ()
(define-key ido-completion-map (kbd "C-d") 'ido-enter-dired))
(add-hook 'ido-setup-hook 'ido-keybindings)
(ido-everywhere t)
(ido-mode 1))
However whenever, I try to use C-d in ido-find-file I always get this error:
Debugger entered--Lisp error: (error "Command attempted to use minibuffer while in minibuffer")
When called with the minibuffer active, your command uses a recursive minibuffer to read input using ido-completing-read.
Use this as your command instead:
(lambda ()
(interactive)
(let ((enable-recursive-minibuffers t)) ; <=====================
(call-interactively
(intern
(ido-completing-read
"M-x "
(all-completions "" obarray 'commandp))))))
C-h v enable-recursive-minibuffers tells us:
enable-recursive-minibuffers is a variable defined in C source code.
Its value is nil
Documentation:
Non-nil means to allow minibuffer commands while in the minibuffer.
This variable makes a difference whenever the minibuffer window is active.
Also see minibuffer-depth-indicate-mode, which may be handy if this
variable is non-nil.
You can customize this variable.
I am using the following.
(global-set-key [f9] 'helm-do-grep-1)
But when I press f9, It complains wrong type argument. I just want it behavior like "C-u C-c h g" to grep recursively. But type so many keys is boring.
update:
I need to grep recursively. helm-do-grep run in non-recursive mode.
You can use
(global-set-key [f9]
(lambda ()
(interactive)
(let ((current-prefix-arg 't))
(call-interactively 'helm-do-grep))))
Upd. If you're interested: the version with kbd sequence
(global-set-key [f9]
(lambda ()
(interactive)
(let ((minibuffer-message-timeout 0))
(execute-kbd-macro (read-kbd-macro "C-u C-c h g C-x Q"))))
See the definition of C-x Q here https://stackoverflow.com/a/28435402/1937596
As the error message already points out, the function helm-do-grep-1 has one argument: https://github.com/emacs-helm/helm/blob/master/helm-grep.el#L810
Probably what you wanted is binding f9 to helm-do-grep which calls helm-do-grep-1 in return with the correct parameters (
https://github.com/emacs-helm/helm/blob/master/helm-grep.el#L1129)
(global-set-key [f9] 'helm-do-grep)
Update:
You can find several solutions to your question here: http://www.reddit.com/r/emacs/comments/2dxj69/how_do_make_helmdogrep_to_do_recursive_always/
To show another possibility you could also do the following:
(global-set-key [f5]
(lambda ()
(interactive)
(call-interactively (key-binding (kbd "C-c h g")))))
In that case, you call helm-do-grep using <f5> and the recursive approach with C-u <f5>. However, this approach will depend on your key bindings.
I have the following global keyboard shortcut in Emacs:
(global-set-key (kbd "C-<right>") 'forward-word)
For the org-mode I decided to redefine this shortcut. If the cursor stands on a link, then go to the link location. Otherwise - use forward-word function.
(defun is-link-p ()
(if (org-in-regexp org-bracket-link-regexp)
t))
(defun follow-link-or-next-word ()
(interactive)
(if (is-link-p)
(org-open-at-point)
(forward-word)))
(add-hook 'org-mode-hook (lambda ()
(define-key org-mode-map (kbd "C-<right>") 'follow-link-or-next-word)))
Is it possible to change org-mode shortcut in the following manner: instead of calling (forward-word), find what function is globally bound to "C-<right>" and call it instead.
Thus I won't need to change (forward-word) twice in case I decide to change the global shortcut.
I think you're looking for the function (lookup-key keymap key &optional accept-defaults)
This function returns the definition of key in keymap. All the other
functions described in this chapter that look up keys use lookup-key.
Here are examples:
(lookup-key (current-global-map) "\C-x\C-f")
⇒ find-file
(lookup-key (current-global-map) (kbd "C-x C-f"))
⇒ find-file
You could extend your functions:
(defun is-link-p ()
(if (org-in-regexp org-bracket-link-regexp)
t))
(defun follow-link-or-default-action()
(interactive)
(let ((global-default (lookup-key (current-global-map) (kbd "C-<right>"))))
(if (is-link-p)
(org-open-at-point)
(funcall global-default))))
(add-hook 'org-mode-hook (lambda ()
(define-key org-mode-map (kbd "C-<right>") 'follow-link-or-default-action)))
This binds an operation to Control-p
(global-set-key (kbd "C-p") (λ () (interactive) (previous-line 5)))
I would like to instead bind it to Meta-UpArrow.
Thanks!
(global-set-key (kbd "M-<up>") (λ () (interactive) (previous-line 5)))
I figured this out by typing "C-h k" and then pressing meta + uparrow.
If use global set key, that mapping will be shadowed by a mode-specific mapping. So, while your mapping works in markdown-mode where there is no mode specific mapping for "M-<up>", it won't work in org-mode where <M-up> maps to org-metaup.
So, if you really want to map "M-<up>" even if it may conflict with some modes that you use, you can do the following:
(global-set-key (kbd "M-<up>") (lambda () (interactive) (previous-line 5)))
(require 'org)
(define-key org-mode-map (kbd "M-<up>") (lambda () (interactive) (previous-line 5)))
However, you need to do this (define-key) for every mode that you use that already has a mapping for <M-up>.
I want to call a function from some elisp code as if I had called it interactively with a prefix argument. Specifically, I want to call grep with a prefix.
The closest I've gotten to making it work is using execute-extended-command, but that still requires that I type in the command I want to call with a prefix...
;; calls command with a prefix, but I have to type the command to be called...
(global-set-key (kbd "C-c m g")
(lambda () (interactive)
(execute-extended-command t)))
The documentation says that execute-extended-command uses command-execute to execute the command read from the minibuffer, but I haven't been able to make it work:
;; doesn't call with prefix...
(global-set-key (kbd "C-c m g")
(lambda () (interactive)
(command-execute 'grep t [t] t)))
Is there any way to call a function with a prefix yet non-interactively?
If I'm understanding you right, you're trying to make a keybinding that will act like you typed C-u M-x grep <ENTER>. Try this:
(global-set-key (kbd "C-c m g")
(lambda () (interactive)
(setq current-prefix-arg '(4)) ; C-u
(call-interactively 'grep)))
Although I would probably make a named function for this:
(defun grep-with-prefix-arg ()
(interactive)
(setq current-prefix-arg '(4)) ; C-u
(call-interactively 'grep))
(global-set-key (kbd "C-c m g") 'grep-with-prefix-arg)
Or you could just use a keyboard macro
(global-set-key (kbd "s-l") (kbd "C-u C-SPC"))
In this example, the key combination "s-l" (s ("super") is the "windows logo" key on a PC keyboard) will go up the mark ring, just like you if typed "C-u C-SPC".