Skip to content

Instantly share code, notes, and snippets.

@gridaphobe
Last active November 21, 2016 14:21
Show Gist options
  • Save gridaphobe/9765143 to your computer and use it in GitHub Desktop.
Save gridaphobe/9765143 to your computer and use it in GitHub Desktop.
evil-mode god state
(evil-define-state god
"God state."
:tag " <G> "
:message "-- GOD MODE --"
:entry-hook (evil-god-start-hook)
:exit-hook (evil-god-stop-hook)
:input-method t
:intercept-esc nil)
(defun evil-god-start-hook ()
(diminish 'god-local-mode)
(god-local-mode 1))
(defun evil-god-stop-hook ()
(god-local-mode -1)
(diminish-undo 'god-local-mode))
(defvar evil-execute-in-god-state-buffer nil)
(defun evil-stop-execute-in-god-state ()
(when (and (not (eq this-command #'evil-execute-in-god-state))
(not (minibufferp)))
(remove-hook 'post-command-hook 'evil-stop-execute-in-god-state)
(when (buffer-live-p evil-execute-in-god-state-buffer)
(with-current-buffer evil-execute-in-god-state-buffer
(if (and (eq evil-previous-state 'visual)
(not (use-region-p)))
(progn
(evil-change-to-previous-state)
(evil-exit-visual-state))
(evil-change-to-previous-state))))
(setq evil-execute-in-god-state-buffer nil)))
(evil-define-command evil-execute-in-god-state ()
"Execute the next command in God state."
(add-hook 'post-command-hook #'evil-stop-execute-in-god-state t)
(setq evil-execute-in-god-state-buffer (current-buffer))
(cond
((evil-visual-state-p)
(let ((mrk (mark))
(pnt (point)))
(evil-god-state)
(set-mar mrk)
(goto-char pnt)))
(t
(evil-god-state)))
(evil-echo "Switched to God state for the next command ..."))
(evil-define-key 'normal global-map "," 'evil-execute-in-god-state)
@saulius
Copy link

saulius commented Mar 25, 2014

Brilliant, thank you!

@gridaphobe
Copy link
Author

Sure thing! I should note that I copied the evil-execute-in-god-state stuff wholesale from evil-execute-in-emacs-state, I just replaced emacs with god.. :)

@kryft
Copy link

kryft commented Apr 2, 2014

Excellent! :) I'm about to migrate from vim to emacs and I've been trying to decide between evil and god-mode for a while. I like evil, but it seemed to me that I would have to either use emacs chords for some commands or come up with my own leader key bindings for everything. Clearly stalling long enough for someone to combine evil and god-mode was the best solution.

@valrus
Copy link

valrus commented May 5, 2014

Outstanding! This is exactly what I was looking for.

I added

(defun god-state-bail ()
  "Stop current God command and exit God state."
  (interactive)
  (evil-stop-execute-in-god-state)
  (evil-god-stop-hook)
  (evil-normal-state))

(evil-define-key 'god global-map [escape] 'god-state-bail)

to keep escape as a get-out-of-everything-ever key. Too used to just whacking Esc a few times to back out of everything in vim.

Edit: Actually, that doesn't work. I'll update this when I get it right...
Edit2: Updated. What I have is probably overkill but what the heck. I'm a noob and it seems to do what I want.

@purcell
Copy link

purcell commented May 10, 2014

Now available in MELPA as evil-god-state.

@jasonm23
Copy link

Nice :) @purcell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment