Emacs multi keyboard shortcut - emacs

I'm trying to create a combination keybinding.
Here's an example:
(define-key my-minor-mode-map (kbd "x f") "\C-x\C-f")
(edit: Thanks you Stefan for pointing out the space between \C-x and \C-f.)
This however takes me to a random file and describe-key says this:
Macro: C-x C-f
Keyboard macro.
So I'm not really sure what that means.
It seems that trying to bind s to C-s doesn't work either (As well as other interactive commands like C-r and M-x).
This does work:
(define-key my-minor-mode-map (kbd "x f") "\M-f")
So basically I want to be able to run C-x C-f (find-file) without having to type 'find-file as a function itself.
In other words; I don't want this:
(define-key my-minor-mode-map (kbd "x f") 'find-file)
I hope someone could help me out with this. My emacs knowledge is very limited.
Thanks in advance.
Complete code:
(defvar my-minor-mode-map (make-keymap) "my-minor-mode keymap")
(define-key my-minor-mode-map (kbd "x f") "\C-x\C-f")
(define-minor-mode my-minor-mode
"My minor-mode"
t "My minor mode" 'my-minor-mode-map)
(defun my-minibuffer-setup-hook ()
(my-minor-mode 0))
"My minor-mode"
Edit:
What would even be better is if I could do this:
(define-key my-minor-mode-map (kbd "x") "\C-x")
(define-key my-minor-mode-map (kbd "f") "\C-f")
And then if I would type "x f" that it would exectue "\C-x C-f" aka find file.
That way I wouldn't have to write out every possible key combination.

"\C-x \C-f" has 3 elements: C-x, SPC, and C-f. You probably did not mean for that space to be there.

I'm not entirely certain what you believe should be happening here, but I suspect what you actually want is:
(define-key my-minor-mode-map (kbd "x f") (key-binding (kbd "C-x C-f")))
which is the same thing as the code you said you didn't want to use:
(define-key my-minor-mode-map (kbd "x f") 'find-file)
except that it obtains the function dynamically, based on the key binding.
p.s. It's also slightly odd that you're using a mixture of kbd and non-kbd syntax in the same form.

Related

Defining key binding with arguments

I want to map C-f C-b as moving forward and backward by a fixed amount of lines in a file.
I did this:
(global-set-key (kbd "C-f") 'next-line)
(global-set-key (kbd "C-b") 'previous-line)
but I don't know how to specify an argument before the next-line command. I guess I should use digit-argument but I am unable to write the command in a correct way.
You've changed your question to be about how to bind directly to key sequences
This binds C-c l to C-u 5 C-n
(global-set-key (kbd "C-c l") (kbd "C-u 5 C-n"))
One of the possible alternatives would be define a new function:
(defun my-next-line ()
(interactive)
(next-line 5))
(global-set-key (kbd "C-f") 'my-next-line)
Otherwise, if it is just something you can accomplish with the keyboard you might want to use
M-x name-last-kbd-macro
and save it in your .emacs file
M-x insert-kbd-macro
and have emacs implement the function for you.
It will just get the name you gave in your call to name-last-kbd-macro

How to bind text insertion in isearch

I'd like to have M-u to insert an underscore when I am in isearch (isearch-regexp and also the reverse variants).
Neither
(define-key isearch-mode-map (kbd "M-u") 'insert-underscore)
nor
(add-hook 'isearch-mode-hook
(lambda ()
(local-set-key (kbd "M-u") 'insert-underscore)
))
insert-underscore is my function that simply inserts "_". It works in the main frame and also in minibuffer, but I can't get it working in isearch...
Thank you!
Isearch doesn't use regular commands. (kbd "_") along with every other
printable character is bound to a special command in isearch-mode-map. It's
not obvious, but a lot of things happen in "isearch-mode" when you press a
key. Display is refreshed with new results, wrapping is a possibility, etc, etc,
You'd have to manipulate raw keyboard events to get this to work.
(defun underscore ()
(interactive)
(isearch-unread-key-sequence (list ?_)))
(define-key isearch-mode-map (kbd "M-u") 'underscore)
Note that this code is not robust; for example, numeric prefix does not work.
EDIT: After letting percolate in my mind for a while, it occured to me that this is the exact use-case for translation keymaps
(define-key key-translation-map (kbd "M-u") (kbd "_"))
Ain't Emacs grand?

having two functions executed with one keyboard combo

I'm trying to have C-<return> map to move-end-of-line then newline-and-indent
In my .emacs I have been playing around with the following:
(global-set-key (kbd "C-<return>") '(progn (move-end-of-line) (newline-and-indent)))
And
(defun c-ret()
'(move-end-of-line newline-and-indent))
(global-set-key (kbd "C-<return>") 'c-ret)
but neither work.
Pointers?
You are quoting the commands.
That implies that they won't be executed. You also need the (interactive) to signify to emacs that it's callable from the keyboard.
Then, you need to have your parameters to your functions correct.
Further, I think your nomenclature for return is wrong.
Your basic misunderstanding here is knowing how eLisp works. That's okay, it's an arcane programming language.
' a.k.a QUOTE is pretty much a Lisp-specific special instruction. It says, "Don't evaluate what comes after me", and returns the unevaluated parameter.
So '(foo bar) is desugared into (QUOTE (FOO BAR)), which returns (FOO BAR).
Try this:
(defun c-ret()
(interactive)
(move-end-of-line nil)
(newline-and-indent))
(global-set-key (kbd "C-RET") 'c-ret)
You can do this without writing any code yourself. See http://www.emacswiki.org/emacs/KeyboardMacrosTricks for instructions on capturing a sequence of commands as a keyboard macro, naming it, and saving it in your .emacs. You can then give the new command a key binding of your choice with e.g. (global-set-key (kbd "C-c i") 'new-command-name).
If you want a one-line solution, this will work too:
(global-set-key (kbd "C-<return>") (lambda () (interactive) (move-end-of-line nil) (newline-and-indent)))

Setting shortcuts keys in specific modes in Emacs (e.g. ido)

I have two problems which are somewhat related I believe:
1) In IDO I'd like to change ido-restrict-to-matches to samething else than C-SPC or C-#. Unfortunately I do not know how to tell emacs that I want a different shortcut (say C-0).
2) I'd like to protect my C-; but whenever flyspell-mode is running it overtakes C-;. My definition is in .emacs as:
(global-set-key (kbd "C-;") 'mark-paragraph)
but apparently flyspell overwrites this... (although even then, if I look in the help M-h k C-; it does say mark-paragraph)
Could somebody please tell me how to bind/unbind keys in these conditions? It has to work without modifying ido.el and flyspell.el and re-building, right?
Thanks very much!
Flyspell provides a customization for the C-; binding, so you can either M-x customize RET flyspell-auto-correct-binding RET or put something like this in your ~/.emacs:
(setq flyspell-auto-correct-binding (kbd "C-~")) ; or a binding of your choice
As for ido, your question is slightly confusing, because it implies there are times when you're using ido outside the minibuffer...
The documentation in ido.el contains the following advice:
;; To modify the keybindings, use the ido-setup-hook. For example:
;;(add-hook 'ido-setup-hook 'ido-my-keys)
;;
;;(defun ido-my-keys ()
;; "Add my keybindings for ido."
;; (define-key ido-completion-map " " 'ido-next-match)
;; )
Using that knowledge, you can change the key bindings like this in your own "ido-my-keys" function:
(define-key ido-completion-map (kbd "C-SPC") nil)
(define-key ido-completion-map (kbd "C-#") nil)
(define-key ido-completion-map (kbd "C-0") 'ido-restrict-to-matches)
There's an additional ido hook specifically for the minibuffer, too, but it's not clear why you would need that: ido-minibuffer-setup-hook.

Useful keyboard shortcuts and tips for ESS/R

I would like to ask regular ESS/R users what key bindings do they use frequently and tips on using ESS/R.
I have set several shortcuts in my .emacs file. The most useful are:
C-tab to switch between the R command line and the file (similar to josh answer, but much faster):
(global-set-key [C-tab] 'other-window)
Control and up/down arrow keys to search history with matching what you've already typed:
(define-key comint-mode-map [C-up] 'comint-previous-matching-input-from-input)
(define-key comint-mode-map [C-down] 'comint-next-matching-input-from-input)
Comment-uncomment a selected region with C-d or C-maj-d
(defun uncomment-region (beg end)
"Like `comment-region' invoked with a C-u prefix arg."
(interactive "r")
(comment-region beg end -1))
(define-key ess-mode-map (kbd "C-d") 'comment-region)
(define-key ess-mode-map (kbd "C-S-d") 'uncomment-region)
Also I've also enabled CUA mode (from options menu) and reconfigured quite a lot of shortcuts to require only two keystrokes (instead of four in standard mode):
;; Delete selection when pressing [delete] key
(delete-selection-mode t)
;; ESS Mode (.R file)
(define-key ess-mode-map "\C-l" 'ess-eval-line-and-step)
(define-key ess-mode-map "\C-p" 'ess-eval-function-or-paragraph-and-step)
(define-key ess-mode-map "\C-r" 'ess-eval-region)
;; iESS Mode (R console)
(define-key inferior-ess-mode-map "\C-u" 'comint-kill-input)
(define-key inferior-ess-mode-map "\C-w" 'backward-kill-word)
(define-key inferior-ess-mode-map "\C-a" 'comint-bol)
(define-key inferior-ess-mode-map [home] 'comint-bol)
;; Comint Mode (R console as well)
(define-key comint-mode-map "\C-e" 'comint-show-maximum-output)
(define-key comint-mode-map "\C-r" 'comint-show-output)
(define-key comint-mode-map "\C-o" 'comint-kill-output)
;; Search with C-f / C-F (control-maj-F for backware search)
(global-set-key "\C-f" 'isearch-forward)
(global-set-key (kbd "C-S-f") 'isearch-backward)
(define-key isearch-mode-map "\C-f" 'isearch-repeat-forward)
(define-key isearch-mode-map (kbd "C-S-f") 'isearch-repeat-backward)
;; Save with C-s / C-S
(global-set-key (kbd "C-s") 'save-buffer)
(global-set-key (kbd "C-S-s") 'write-file)
;; need to redefine them for isearch mode (don't know why)
(define-key isearch-mode-map (kbd "C-s") 'save-buffer)
(define-key isearch-mode-map (kbd "C-S-s") 'write-file)
;; Pause = dedicate window.
(defun toggle-current-window-dedication ()
(interactive)
(let* ((window (selected-window))
(dedicated (window-dedicated-p window)))
(set-window-dedicated-p window (not dedicated))
(message "Window %sdedicated to %s"
(if dedicated "no longer " "")
(buffer-name))))
(global-set-key [pause] 'toggle-current-window-dedication)
;; delete = delete
(global-set-key [delete] 'delete-char)
;; C-b = list buffers
(global-set-key (kbd "C-b") 'bs-show)
You will find many more useful shortcuts in ESS documentation.
C-c C-z ess-switch-to-end-of-ESS
is nice to jump from your source file that you are editing foo.R to the R console
I found this link to be extremely helpful. It provides elisp code to make Shift+Enter do many common tasks in a context dependent fashion.
http://kieranhealy.org/blog/archives/2009/10/12/make-shift-enter-do-a-lot-in-ess/
Great stuff, have been using it for ages. Unfortunately as of 15-11-2013 the uncomment key binding may not work due to EMACS changes (I think, at least it was working before I loaded the latest version). This is because the default uncomment function has 3 arguments but the one defined above has 2. The best way to fix this is to simply delete the uncomment function from the code and retain the keybinding, so it uses the default uncomment function. Or in other words just use this:
(define-key ess-mode-map (kbd "C-d") 'comment-region)
(define-key ess-mode-map (kbd "C-S-d") 'uncomment-region)
M-n and M-p in the ESS R console for next/previous command.