How to toggle evil mode in Emacs

The evil-emacs-state

When you are in evil mode, press ctrl + z to switch to evil-emacs-state, to go back to evil mode press ctrl + z again, the two functions bind to them:


The evil-emacs-state has only one keybinding: the ctrl + z. Its completely the same as Emacs except the key binding. Almost useless, because its the same as turn on and off evil mode. And I already use the ctrl + z to do the undo, and I don't want to waste a key binding.

And when I go back to Emacs, I want a real Emacs, not a minor mode.

One of the best practices is only use the evil mode's normal mode and visual mode, whenever I need insert mode, I use Emacs instead, since the insert mode of VIM is almost useless, all the powerfulness comes from the normal mode and visual mode. This is the best solution if you already have a set of Emacs key bindings and want to get the best part of VIM at the same time. I think what the Evil mode should do is to combine the best part of VIM and Emacs, not to simulate VIM in Emacs.

I don't want use ctrl + z either, the M - u convert character to upper case which I rarely used, I use it to toggle between evil mode and Emacs mode. Here is the code:

defun toggle-evilmode ()
  (if (bound-and-true-p evil-local-mode)
      ; go emacs
      (evil-local-mode (or -1 1))
      (undo-tree-mode (or -1 1))
      (set-variable 'cursor-type 'bar)
      ; go evil
      (evil-local-mode (or 1 1))
      (set-variable 'cursor-type 'box)
(global-set-key (kbd "M-u") 'toggle-evilmode)

I also set the cursor to bar in Emacs mode and box in evil mode. Just like the normal mode and insert mode in VIM.

Another problem is when you enable evil mode, the undo-tree-mode is also enabled but when you disable evil mode, the undo-tree-mode will not go away. This breaks the Emacs undo mechanism, we need to disable it when toggle off the evil mode, as shown in the above code.

Actually I always use Emacs undo mechanism, I don't need another undo.