Enter indents previous line? - emacs

In Emacs 24.3 (9.0) I'm using the latest yaml-mode installed via el-get-update. When I hit ENTER at the end of a line, it has the unfortunate habit of auto-indenting the line I'm on before inserting the newline.
E.g. starting with this buffer:
foo:
bar:
- baz
baz:# <- Cursor here
Hitting enter results in the following buffer:
foo:
bar:
- baz
baz: # <- unwanted indentation!
# <- Cursor here
I've been working in a lot of YAML files lately, and this is slowly driving me mad. How do I stop it?
UPDATE:
To clarify, I'm not seeing this behavior in any other mode, just yaml-mode. I'm presently on commit 6d40a1dbd4c83f91d70f0e646e7bd8a45acc6fbf from http://github.com/yoshiki/yaml-mode.

As we discovered in the comments, something has caused reindent-then-newline-and-indent to get bound to RET.
I'm not sure where that binding came from, but you should be able to rebind RET to newline-and-indent in YAML mode like this:
(add-hook 'yaml-mode-hook
'(lambda ()
(define-key yaml-mode-map "\C-m" 'newline-and-indent)))
or simply to newline if you don't want automatic indenting of the next line:
(add-hook 'yaml-mode-hook
'(lambda ()
(define-key yaml-mode-map "\C-m" 'indent)))

Related

Emacs/Spacemacs — rebind SLIME `eval-last-expression-in-repl` (, s e) to ctrl-enter

I’m using emacs / spacemacs.
I see that:
, s e # slime-eval-last-expression-in-repl
sends the current highlighted line to the repl and runs it, exactly what I want.
How to add a binding into emacs/spacemacs to do the same but using ctrl-enter / ctrl-RET ?
Put the following lines in your ~/.emacs file:
(eval-after-load 'slime-repl
(define-key slime-repl-mode-map (kbd "<C-return>")
'slime-eval-last-expression-in-repl))
If you have trouble getting this to work in terminal emacs (emacs -nw), check out this related question.

Emacs evil-mode how to change insert-state to emacs-state automatically

I don't like the insert-state, and so I want to replace it with emacs-state. But this setting does not work:
(add-hook 'evil-insert-state-entry-hook 'evil-emacs-state)
After press o or cw, I am still in insert-state.
How about this approach:
(setq evil-insert-state-map (make-sparse-keymap))
(define-key evil-insert-state-map (kbd "<escape>") 'evil-normal-state)
I use it and it seems to do the trick. And since you're not changing the state, you retain state-related configs like cursor-color, etc.
Surprised nobody posted this yet...
(defalias 'evil-insert-state 'evil-emacs-state)
Anything that tries to call evil-insert-state will just end up calling evil-emacs-state. Works for i, a, o, O, etc.
There is now a bulitin way for Evil to do this
(setq evil-disable-insert-state-bindings t)
before loading evil
Reference: https://github.com/noctuid/evil-guide#use-some-emacs-keybindings
Tell me how this works. It's a hack that basically replaces the function evil-insert-state with evil-emacs-state. The problem is figuring out how to exit emacs state with the escape key. For instance, this version works fine when I exit emacs state with the ESC key, but not when I try to do the same with C-[:
; redefine emacs state to intercept the escape key like insert-state does:
(evil-define-state emacs
"Emacs state that can be exited with the escape key."
:tag " <EE> "
:message "-- EMACS WITH ESCAPE --"
:input-method t
;; :intercept-esc nil)
)
(defadvice evil-insert-state (around emacs-state-instead-of-insert-state activate)
(evil-emacs-state))
If the point is that you want to use normal Emacs editing when doing the kind of tasks vi uses insert mode for, then wiping the insert mode dictionary accomplishes this. It is probably desirable that the ESC key gets you back into normal mode and have C-z get you into Emacs state; Leo Alekseyev posts a tiny bit of code that does this:
(setcdr evil-insert-state-map nil)
(define-key evil-insert-state-map
(read-kbd-macro evil-toggle-key) 'evil-emacs-state)
which I use and like. There are two potential disadvantages to being in insert mode rather than emacs mode:
You can't use the ESC key as another, prefixed way of ALT-keymapping; and
There is a risk (so I am told, though I haven't encountered this) if you are accessing Emacs through a tty, that Emacs will interpret ALT-modified keys as ESC followed by the character, which gives a difference in insert mode than in emacs mode.
I don't think either problem is serious.
How I became a unix chad:
;; unix chad setting
(defalias 'evil-insert-state 'evil-emacs-state)
(define-key evil-emacs-state-map (kbd "<escape>") 'evil-normal-state)
(setq evil-emacs-state-cursor '(bar . 1))
From the documentation about evil-emacs-state-entry-hook:
Hooks to run when entering Emacs state.
So the evil-emacs-state function is run when you enter emacs-state (with C-z).
You can, however, do this:
(define-key evil-normal-state-map (kbd "i") 'evil-emacs-state)
The problem now is exiting emacs state. I remember there were some problems binding ESC in emacs state, as ESC is used as META, and (IIRC) Evil uses some "special" code to intercept the ESC key.
EDIT: following your comment: this one should work:
(fset 'evil-insert-state 'evil-emacs-state)

Executing a specific macro in emacs evil

Right now I am trying to use the evil plugin in emacs so that I can have the editing capability of vim with the extensibility of emacs. Right now I'm trying to port over one of my favorite parts of my .vimrc: have space repeat whatever my last executed macro was. In my .vimrc it was simply
nore <Space> ##
I am trying to do the same thing in my .emacs file with
(define-key evil-normal-state-map " " (lambda () (interactive) (evil-execute-macro 1 "#")))
## repeats the last macro fine, however hitting space gives me the error
After 0 kbd macro iterations: No previous macro
I'm fairly new to lisp and evil so I'm sure I'm doing something very wrong and I would appreciate any help.
You can bind it similarly to vim:
(define-key evil-normal-state-map " " (kbd "##"))
Regarding your code: The second argument of evil-execute-macro should be a character, i.e. ?#. But this only holds if evil-execute-macro is called interactively because the content of the corresponding register is only retrieved in the interactive form.
This boils down to this: The correct call would be (evil-execute-macro 1 last-kbd-macro).

Binding RET to newline in Emacs

In Emacs, while the major mode ESS[S] (Emacs speaks statistics) is in effect, RET is automatically bound to newline-and-ident but I would prefer it to be bound to just newline. Following the advice here, I bound RET to newline. This works for editing while in ESS, but it has the undesired effect of also affecting commands in the mini-buffer. I cannot use RET to finish commands in the mini-buffer; instead, it just inserts a new line into the mini-buffer rather than executing the command.
Is there a way to bind the key in a minor mode, but also have it not affect the mini-buffer whatsoever?
Even turning the minor-mode off doesn't seem to work and it would be cumbersome to constantly switch the minor mode on and off even if it did.
This is the relevant part of my .emacs file:
(defvar my-keys-minor-mode-map (make-keymap) "my-keys-minor-mode keymap.")
(define-key my-keys-minor-mode-map (kbd "RET") 'newline)
(define-minor-mode my-keys-minor-mode
"A minor mode so that my key settings override annoying major modes."
t " my-keys" 'my-keys-minor-mode-map)
(my-keys-minor-mode 1)
You have the answer in your question:
(define-key ess-mode-map (kbd "RET") 'newline)

Paths with whitespaces in ido (find-file) (on MS Windows)

I am running GNU Emacs 23 on a Windows-machine and having problems getting ido-find-file to match files that contain whitespaces.
In my .emacs I (setq ido-use-filename-at-point t) and would like to match a string like "D:\Dokumente und Einstellungen\Username\Eigene Dateien\Scratch\ipswitch.bat" when hitting C-x C-f.
When point is located between " and D:\, IDO suggests d:/Dokumente[Dokumente und Einstellungen], i.e. the pattern "breaks" on the whitespaces.
I would like the whole string (maybe restricted to certain modes (like org-mode) if that is necessary) to be matched and suggested.
How could I get that working?
I have not found a solution for ffap/thingatpt, but for ido-mode the following code in init.el works wonders.
I use Gnu Emacs on Windows with ido-mode, and have found joy in changing the minibuffer completion map:
;; EXPERIMENTAL: unbind SPACE and ? in minibuffer, to allow typing in completions with those chars
(add-hook 'minibuffer-setup-hook (lambda ()
(define-key minibuffer-local-completion-map " " nil)
(define-key minibuffer-local-must-match-map " " nil)
(define-key minibuffer-local-completion-map "?" nil)
(define-key minibuffer-local-must-match-map "?" nil)))
The comment warns it is "experimental" so I can reverse it in case I ever encounter strange behavior. But I have used this for over a year now with no problems.
Now when I do C-x C-f C:/Doc
ido-mode suggests C:/Documents and Settings/