19

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
transtone
  • 193
  • 1
  • 5

7 Answers7

13

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.

cheinigk
  • 141
  • 1
  • 8
  • 1
    neat, simple and works well, thanks! I also recommend `(define-key evil-insert-state-map (kbd "C-o") 'evil-execute-in-normal-state)` to still be able to execute one-off evil command from insert state – tlegutko Jul 08 '17 at 18:45
10

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.

robru
  • 2,313
  • 2
  • 29
  • 38
7

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

mshohayeb
  • 453
  • 6
  • 8
4

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))
Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
  • "The problem is figuring out how to exit emacs state with the escape key." It's not a problem. I already done this with: (define-key evil-emacs-state-map [escape] 'evil-normal-state) Some action like o(evil-open-below) is bingding with evil-insert-state in file evil-insert.el – transtone Aug 28 '14 at 23:26
  • Thanks! The answer along with the suggestion in the comment seems to work well. I also added `(define-key evil-emacs-state-map (kbd "C-[") 'evil-normal-state)`. – Aditya Kashi May 04 '18 at 21:35
2

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.

Charles Stewart
  • 11,661
  • 4
  • 46
  • 85
2

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

upgrd
  • 720
  • 7
  • 16
0

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)
ale
  • 986
  • 1
  • 9
  • 14
  • 1
    Thanks, I already change the key map. I just want o/O, cw to evil-emacs-state, not evil-insert-state. – transtone Aug 28 '14 at 16:08