How to simulate return in emacs minibuffer - emacs

What I want to do is, when I find myself using a function that prompts me for a value, and this value is ALWAYS the right one, to be able to bind the function to a key and auto-accept the answer, simulating a RET in the minibuffer.
Take for instance "ido-switch-buffer". When entered, it prompts you for a buffer name, and proposes the last visited one.
(It can do a lot more, that's why I dont want to re-define it, I just want a wrapper around it)
Say I want to switch between the last visited two buffers, how would I say that in my .emacs ?

You can do this with:
(switch-to-buffer (other-buffer))

You may bind any keyboard macro, which is essentially a sequence of keys, to a key. For example,
(global-set-key (kbd "C-c b") (kbd "C-x b <return>"))

Related

How to override the keybindings for Emacs Org mode org-property-next-allowed-values and org-property-previous-allowed-value

S-RIGHT is the default keybinding for (org-property-next-allowed-values) and
S-LEFT for (org-property-previous-allowed-value) according to https://orgmode.org/manual/Property-Syntax.html. These keybindings are used to cycle through the TODO keywords as well as schedule dates.
I want to change the keybinding to C-RIGHT and C-LEFT, respectively so that they don't interfere with my normal editing. (I use S-LEFT, S-RIGHT for text selection).
I tried to add the following lines to my init.el:
(define-key org-mode-map (kbd "<S-left>") nil)
(define-key org-mode-map (kbd "<S-right>") nil)
The above 2 lines successfully disabled the 2 keybindings.
Then I added the following 2 lines to remap the keybindings:
(define-key org-mode-map (kbd "<C-left>") 'org-property-previous-allowed-value)
(define-key org-mode-map (kbd "<C-right>") 'org-property-next-allowed-values)
However, emacs (I use AquaEmacs on Mac) doesn't recognize the "org-property-previous-allowed-value" and "org-property-next-allowed-values".
I wonder what did I do wrong here?
Thanks
<S-right> is bound to org-shiftright by default, which does different things depending on context. It only does org-property-next-allowed-value if you cursor is on a property.
I don't know why you are having problems with the rebinding, hence my comment asking for clarification. But I thing that you probably don't want to redefine keys at all. Try adding
(setq org-support-shift-select 'always)
to your init file instead. The doc string for org-support-shift-select says (among other things - you should do C-h v org-support-shift-select RET and read the whole thing if you decide to go this way):
In Emacs 23, when ‘shift-select-mode’ is on, shifted cursor keys
start selecting a region, or enlarge regions started in this way.
In Org mode, in special contexts, these same keys are used for
other purposes, important enough to compete with shift selection.
Org tries to balance these needs by supporting ‘shift-select-mode’
outside these special contexts, under control of this variable.
...
If you set this variable to the symbol ‘always’, then the keys
will not be special in headlines, property lines, item lines, and
table cells, to make shift selection work there as well. If this is
what you want, you can use the following alternative commands:
‘C-c C-t’ and ‘C-c ,’ to change TODO state and priority,
‘C-u C-u C-c C-t’ can be used to switch TODO sets,
‘C-c -’ to cycle item bullet types,
and properties can be edited by hand or in column view.
However, when the cursor is on a timestamp, shift-cursor commands
will still edit the time stamp - this is just too good to give up.

In Spacemacs (or Emacs), is it possible to bind keys to other keys?

Here is one thing that has been hard to find out. In Spacemacs (or Emacs), is it possible to have a given key combination set to execute whatever another key combination is supposed to do?
In other words, is it possible, for example, to have the F5 key always execute whatever the combination C-c C-c is supposed to do? Something like:
(global-set-key [remap (kbd "F5")] (kbd "C-c C-c"))
Let me give a concrete example for clarity. The key combination C-c C-c is often used to send the current buffer's entire code to interpreter/console, be it to send the buffer's code to the Python interpreter in case the code resides in a .pyfile, or to send it to a R console with package ESS in a .R file.
Now, suppose that one wants to have the F5 key set to do that for these two languages and maybe even more. One obvious solution would be to simply implement key bindings that are different for each package. However, it would be much easier if it was possible to simply say: whenever F5 is pressed, it should result in whatever command associated to `C-c C-c being called.
Is it possible to have such a thing? I am particularly interested in doing that for Spacemacs, but of course a more generic answer for Emacs is acceptable.
Try
(define-key key-translation-map [f5] (kbd "C-c C-c"))
It should solve your problem.

Emacs: Use a function key (F19) as meta

I’m on a Mac and would like to use the function key F19 as meta.
(There’s a good reason, although it's a bit of a hack: My built (the otherwise excellent port by Yamamoto Mitsuharu) doesn’t support using only the left alt key as meta while preserving the native behavior (inserting special characters) of the right alt key.. So I remapped the left alt key to an unused key - F19 - on the system level with PCKeyboardHack (xmodmap is sorely missed) and would like to tell Emacs to use that as meta.)
So, how do I: use a function key (F19 in my case) as meta key in Emacs?
(I’m fairly new to Emacs and, after some googling, tried out something like
(define-key global-map [f19] \M)
but that, of course, doesn’t do the trick (Symbol’s value as variable is void: M)
Well, with
(setq x-alt-keysym symbol)
you can tell emacs what key is to be understood as meta, but afaik it only accepts 'meta, 'alt, 'super and 'hyper as symbol. Maybe try it with f19.
An alternative option that will work but which will require adaption to a new way of working would be to use F19 as a prefix key (like you use C-h or F1 to invoke help commands):
(define-prefix-command 'f19-map)
(global-set-key (kbd "<f19>") 'f19-map)
(global-set-key (kbd "<f19> x") 'execute-extended-command)
(global-set-key (kbd "<f19> u") 'upcase-word)
...
Writing the configuration shouldn't be too hard. Just press C-h b to get all existing keybindings and edit the help buffer. Get rid of all lines that don't start with M-, then use rectangles to replace all occurrences of ^M- by (global-set-key (kbd ", and so on.
Yet another option would be to bind the key to Esc instead of F19, as long as that's supported by your system, and use the esc prefix instead of the f19 prefix. That way you don't have to change the emacs configuration at all.

Cannot get to bind Enter to 'newline-and-indent in Emacs !!! Very annoying

Cannot get to bind Enter to newline-and-indent in Emacs !!! Very annoying.
I already tried everything on the following thread by changing 'mode' to ruby and still nothing:
How do I make Emacs auto-indent my C code?
I know that the problem is the RETURN key, since if I bind to something else, works fine.
I tried [enter], (kbd "enter"), (read-kbd-macro "enter"), (kbd "RET")
Follow-up 1.
This is what I get from C-hkRET
RET runs the command newline, which is an interactive compiled Lisp
function.
It is bound to RET.
(newline &optional ARG)
Insert a newline, and move to left margin of the new line if it's blank.
If use-hard-newlines' is non-nil, the newline is marked with the
text-propertyhard'.
With ARG, insert that many newlines.
Call auto-fill-function' if the current column number is greater
than the value offill-column' and ARG is nil.
I dont know what to make of it or how to figure out if it's a global
or local binding that gets in the way. trying to remap C-j
also doesnt work.
As a previous comment says, use C-h k (describe-key) to see what the key is bound to at the point when it's not doing what you want. The (kbd "foo") syntax will be correct for whichever foo describe-key refers to it as.
Chances are that you are simply not defining that key in the appropriate keymap.
Note that major and minor mode keymaps take precedence over the global keymap, so you shouldn't necessarily be surprised if a global binding is overridden.
edit:
Myself, I have a hook function for common behaviours for all the programming modes I use, and it includes the sort of remapping you're after. The relevant part looks like this:
(defun my-coding-config ()
(local-set-key (kbd "RET") (key-binding (kbd "M-j")))
(local-set-key (kbd "<S-return>") 'newline)
)
(mapc
(lambda (language-mode-hook)
(add-hook language-mode-hook 'my-coding-config))
'(cperl-mode-hook
css-mode-hook
emacs-lisp-mode-hook
;; etc...
))
See Daimrod's answer for the explanation of why I'm re-binding RET to the current binding of M-j -- although I'm using comment-indent-new-line (or similar) instead of newline-and-indent (or similar), which does what I want in both comments and non-comments.
In Emacs 24, programming modes seem to derive from prog-mode, so you could probably (un-tested) reduce that list to prog-mode-hook plus any exceptions for third-party modes which don't yet do that.
As said earlier, use C-hkC-j because
C-j is the standard key to do newline-and-indent.
If you open a new file, activate ruby-mode and try the previous
command you will see why it doesn't work. Because ruby-mode doesn't
have newline-and-indent but rather
reindent-then-newline-and-indent. Yes that's stupid but you can either ask
to the maintener to change it, or accept it.
However I suggest you to use C-j to do it because
ruby-mode is not the only mode to do so, like paredit-mode which
uses paredit-newline.

Assign multiple Emacs keybindings to a single command?

I'm giving ErgoEmacs mode a try to see if I can use Emacs more comfortably. Some of its keybindings are fairly intuitive, but in many cases I don't want to outright replace the defaults.
For example, in the context of ErgoEmacs' navigation shortcut structure, M-h makes sense as a replacement for C-a--but I want to be able to use both, not just M-h. I tried simply duplicating the commands:
;; Move to beginning/ending of line
(defconst ergoemacs-move-beginning-of-line-key (kbd "C-a")) ; original
(defconst ergoemacs-move-end-of-line-key (kbd "C-e")) ; original
(defconst ergoemacs-move-beginning-of-line-key (kbd "M-h")) ; ergoemacs
(defconst ergoemacs-move-end-of-line-key (kbd "M-H")) ; ergoemacs
But Emacs simply overwrites the first keybinding with the second. What's the best way to address this?
To re-post reply from ergo-emacs mailing list:
Xah Lee said:
that's very easy.
in the
ergoemacs-mode.el file, there's this
line (load "ergoemacs-unbind") just
comment it out. That should be all
you need to do. However, note that
ErgoEmacs keybinding defines those
common shortcuts such as Open, Close,
New, Save... with keys Ctrl+o,
Ctrl+w, Ctrl+n, Ctrl+s etc. About 7 of
them or so. So, i think some of these
will hit on emacs traditional
bindings with Ctrl. if you are new to
ErgoEmacs and trying to explore it,
you might just try starting with few
keys. this page might have some
useful info:
http://code.google.com/p/ergoemacs/wiki/adoption
thanks for checking out ErgoEmacs!
Xah ∑ http://xahlee.org/
As it turns out, ErgoEmacs uses two files to define the keybinding. One is the main ergoemacs-mode.el file, and the other is the specific keyboard layout you select (e.g. ergoemacs-layout-us.el). The latter document creates a constant, which the former uses to create the keybinding. So while I thought I was duplicating the keybinding, I was actually changing the constant which was subsequently used for that purpose.
Solution:
In ergomacs-mode.el:
;; Move to beginning/ending of line
(define-key ergoemacs-keymap ergoemacs-move-beginning-of-line-key 'move-beginning-of-line)
(define-key ergoemacs-keymap ergoemacs-move-end-of-line-key 'move-end-of-line)
(define-key ergoemacs-keymap ergoemacs-move-beginning-of-line-key2 'move-beginning-of-line) ; new
(define-key ergoemacs-keymap ergoemacs-move-end-of-line-key2 'move-end-of-line) ; new
In ergoemacs-layout-us.el:
;; Move to beginning/ending of line
(defconst ergoemacs-move-beginning-of-line-key (kbd "M-h"))
(defconst ergoemacs-move-end-of-line-key (kbd "M-H"))
(defconst ergoemacs-move-beginning-of-line-key2 (kbd "C-a")) ; new
(defconst ergoemacs-move-end-of-line-key2 (kbd "C-e")) ; new
Huh? Is having one and only one way for every function some golden principle of ErgoEmacs? Because normal keybinding works exactly the opposite way: you name one key at a time and specify what it should do. If a mode defines a global variable to mean "the key that end-of-line is bound to", then of course there can be only one value, but with the normal binding commands you can bind the same function to as many combinations as you like. In fact, every keybinding I have ever seen used looked either like this
(global-set-key [(meta space)] 'just-one-space)
or like this
(add-hook 'c-mode-hook 'my-c-mode-hook)
(defun my-c-mode-hook ()
(define-key c-mode-map [(control c) b] 'c-insert-block))
if it's only for a specific mode.