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

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.

Related

Redefine M-z from zap-to-char

Clicking Ctrl-h k Alt-z gives this:
M-z runs the command zap-to-char, which is an interactive compiled
Lisp function in `simple.el'.
It is bound to M-z.
(zap-to-char ARG CHAR)
Kill up to and including ARGth occurrence of CHAR. Case is ignored if
`case-fold-search' is non-nil in the current buffer. Goes backward if
ARG is negative; error if CHAR not found.
I never use this function, but the keys are located in a very good position. So I wanted to redefine this sequence. But I couldn't. Even when I execute this command in .emacs:
(global-set-key (kbd "\M-z") 'backward-delete-word) M-z is still bound to zap-to-char.
Approach, suggested here, didn't help:
Emacs can't reset Ctrl-d key behaviour
You don't need a \ in 'kbd'. Also, I think you're looking for backward-kill-word:
(global-set-key (kbd "M-z") 'backward-kill-word)
EDIT: I didn't notice that the page you linked to defines backward-delete-word. If you have that function defined, you can use it instead of backward-kill-word here.

Is there an Emacs paredit hook available so I can redefine C-j?

I like using C-j to eval-last-sexp but paredit-mode (which I otherwise like) overrides this to paredit-newline. Looking in the paredit-mode docs I don't see anything like a paredit-mode-hook defined where I can add-hook to call local-set-key or a similar function.
Anyone have a suggestion?
Update
After trying out the two answers below and not having much success, I think the problem may be related to the fact that paredit is getting loaded in a few different contexts? To wit, I am opening both Common Lisp, Clojure and Emacs Lisp files, all of which can use paredit. Sadly, the various forms of eval-last-sexp have slightly different names in each mode, so I can't define the key once for everything. Rather, I need to bind the key based on the major mode that I am in also. Hopefully that adds another useful data point.
No need to use hooks, something like the following should work:
(eval-after-load "paredit"
#'(define-key paredit-mode-map (kbd "C-j") 'eval-last-sexp))
Alternatively, if for some reason that doesn't work, or you simply prefer the use of hooks, you can add the above define-key invocation to the hooks of the major modes for which paredit is activated.
Every mode defined by one of the define-*-mode macros automatically runs a corresponding MODE-hook.
I see that paredit-mode is a minor mode defined with (define-minor-mode paredit-mode ...), and therefore it will run paredit-mode-hook.
You can type M-x find-function RET define-minor-mode RET and search for run-hooks to see where this behaviour is defined.
Edit (based on addition to question):
Do they all use C-x C-e as a default binding for the mode-specific eval-last-sexp function, by any chance? That would seem like a consistent thing for them to do, and if so then you could use the following approach:
(local-set-key (kbd "C-j") (key-binding (kbd "C-x C-e")))
There is a paredit-mode-hook. You don't see it until you add something to it. Weird, but this is the way hooks behave.
However, in your case, the best approach may be to clear the paredit binding for C-j:
(eval-after-load 'paredit
#'(define-key paredit-mode-map (kbd "C-j") nil))
And then set your own via local-set-key in every major-mode hook.

Emacs macro works in .emacs file but not in a source file?

I have an emacs macro (global-set-key) that works perfectly fine in my .emacs file, but for whatever reason, it does not work in my .c file.
(global-set-key "\C-c\C-d" "\C-a\C- \C-e\M-w\C-j\C-y")
If I close and re-open my .emacs file and start messing around, this macro behaves as expected, copying a line to a line below. However, when I open a C file the same macro simply deletes a character (it only seems to pick up on C-d).
Any ideas?
The cc-mode defines C-c C-d in c-mode-base-map to be c-hungry-delete-forward, which is hiding your binding at the global level. So, the better way to solve this is to undefine the binding that cc-mode made, and you do that with the following:
(eval-after-load "cc-mode"
'(define-key c-mode-base-map (kbd "C-c C-d") nil))
You could also do it in a hook, but I prefer eval-after-load because it only gets executed once.
Note: I determined the existing binding by opening up a file in c-mode and typing C-h C-k C-c C-d (aka M-x describe-binding C-c C-d), and seeing:
C-c C-d runs the command c-hungry-delete-forward, which is an
interactive compiled Lisp function in `cc-cmds.el'.
This made it pretty clear that the binding was set up in (one of the) c-modes, so I just opened up (or greped) the source files for c-hungry-delete-forward whereupon I found:
(define-key c-mode-base-map "\C-c\C-d" 'c-hungry-delete-forward)
And then the answer was straight forward.
I think it's better to undefine local bindings that hide the global bindings you want, rather than redefining them. It's just as much work to find the problematic bindings, and this way if you want to change the function for the global binding, you only have to do it in one place.
Clearly the C mode is removing the binding, or changing it. You can try to add it to a c-mode-hook and see if it works then. Similar to this:
(add-hook `c-mode-hook '(lambda ()
(global-set-key "\C-c\C-d" "\C-a\C- \C-e\M-w\C-j\C-y")))
Either c-mode-hook or c-mode-common-hook. You can also use local-set-key instead of the global one to apply the binding just to this buffer.

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.

Globally override key binding in Emacs

How can I set a key binding that globally overrides and takes precedence over all other bindings for that key? I want to override all major/minor mode maps and make sure my binding is always in effect.
This of course doesn't work:
(global-set-key "\C-i" 'some-function)
It works in text-mode, but when I use lisp-mode, C-i is rebound to lisp-indent-line.
I can go through and override this binding in lisp-mode and in every other mode individually, but there must be an easier way. Every time I install a new mode for a new file type, I'd have to go back and check to make sure that all of my key bindings aren't being overridden by the new mode.
I want to do this because I want to emulate bindings I've already learned and ingrained from other editors.
I use a minor mode for all my "override" key bindings:
(defvar my-keys-minor-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-i") 'some-function)
map)
"my-keys-minor-mode keymap.")
(define-minor-mode my-keys-minor-mode
"A minor mode so that my key settings override annoying major modes."
:init-value t
:lighter " my-keys")
(my-keys-minor-mode 1)
This has the added benefit of being able to turn off all my modifications in one fell swoop (just disable the minor mode) in case someone else is driving the keyboard or if I need to see what a default key binding does.
Note that you may need to turn this off in the minibuffer:
(defun my-minibuffer-setup-hook ()
(my-keys-minor-mode 0))
(add-hook 'minibuffer-setup-hook 'my-minibuffer-setup-hook)
As an addition to scottfrazer's answer, I've written the following so that my keybindings retain precedence, even if subsequently-loaded libraries bring in new keymaps of their own.
Because keymaps can be generated at compile time, load seemed like the best place to do this.
(add-hook 'after-load-functions 'my-keys-have-priority)
(defun my-keys-have-priority (_file)
"Try to ensure that my keybindings retain priority over other minor modes.
Called via the `after-load-functions' special hook."
(unless (eq (caar minor-mode-map-alist) 'my-keys-minor-mode)
(let ((mykeys (assq 'my-keys-minor-mode minor-mode-map-alist)))
(assq-delete-all 'my-keys-minor-mode minor-mode-map-alist)
(add-to-list 'minor-mode-map-alist mykeys))))
Install use-package, eval and you're done:
(require 'bind-key)
(bind-key* "C-i" 'some-function)
I found this question while searching for "emacs undefine org mode keybindings", because I wanted to unbind the existing C-c C-b behavior to allow my global map to bury-buffer to work in an org buffer.
This ended up being the simplest solution for me:
(add-hook 'org-mode-hook
(lambda ()
(local-unset-key (kbd "C-c C-b"))))
Although scottfrazer's answer is exactly what you asked for, I will mention for posterity another solution.
From The Emacs Manual:
"Don't define C-c letter as a key in Lisp programs. Sequences consisting of C-c and a letter (either upper or lower case) are reserved for users; they are the only sequences reserved for users, so do not block them."
If you bind your personal global bindings to C-c plus a letter, then you "should" be safe. However, this is merely a convention, and any mode is still able to override your bindings.
If you want to "always use the keybinds in the map, unless I explicitly override them for a specific mode-map", and assuming you are using scottfrazier's approach, you want:
(defun locally-override (key cmd)
(unless (local-variable-p 'my-keys-minor-mode-map)
(set (make-variable-buffer-local 'my-keys-minor-mode-map)
(make-sparse-keymap))
(set-keymap-parent my-keys-minor-mode-map
(default-value 'my-keys-minor-mode-map)))
(define-key my-keys-minor-mode-map key cmd))
So
(locally-override "\C-i" nil)
should remove the "\C-i" binding from the minor mode in the current buffer only. Warning: this is completely untested, but seems like the right approach. The point of setting the parent rather than just coping the global value of my-keys-minor-mode-map is so any later changes to the global value are automatically reflected in the local value.
I don't think you can. That is roughly equivalent to saying that you want to define a global variable that cannot be hidden by local variable declarations in functions. Scope just doesn't work that way.
However, there might be a way to write an elisp function to go through the mode list and reassign it in every single one for you.
Unless you really want to do this yourself, you should check around and see if anyone else already has done it.
There is a package for Emacs which gives your windows-like keybindings. You should be able to find it through google.