Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 527
transtone Avatar asked Aug 28 '14 06:08

transtone


6 Answers

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.

like image 82
cheinigk Avatar answered Oct 11 '22 14:10

cheinigk


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.

like image 39
robru Avatar answered Oct 11 '22 13:10

robru


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

like image 31
mshohayeb Avatar answered Oct 11 '22 14:10

mshohayeb


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

like image 42
upgrd Avatar answered Oct 11 '22 13:10

upgrd


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))
like image 37
Gordon Gustafson Avatar answered Oct 11 '22 12:10

Gordon Gustafson


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.

like image 44
Charles Stewart Avatar answered Oct 11 '22 13:10

Charles Stewart