transtone
transtone

Reputation: 203

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.

Upvotes: 20

Views: 7482

Answers (7)

lupl
lupl

Reputation: 934

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))

enter image description here

Upvotes: 3

mshohayeb
mshohayeb

Reputation: 473

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

Upvotes: 8

cheinigk
cheinigk

Reputation: 151

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.

Upvotes: 14

robru
robru

Reputation: 2383

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.

Upvotes: 10

Charles Stewart
Charles Stewart

Reputation: 11837

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:

  1. You can't use the ESC key as another, prefixed way of ALT-keymapping; and
  2. 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.

Upvotes: 2

ale
ale

Reputation: 994

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)

Upvotes: 0

Gordon Gustafson
Gordon Gustafson

Reputation: 41209

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))

Upvotes: 4

Related Questions