Evil Mode best practice? [closed]
Solution 1:
I used a highly customized vim, and now use an even more customized emacs. I think you'll find every instance of keymapping in my keymapping config file https://github.com/mbriggs/.emacs.d-oldv2/blob/master/init/init-keymaps.el
Keep in mind, I am rebinding stuff that real emacs users would consider heresy, so YMMV if you ever want to learn "real" emacs (I really don't).
One thing I would recommend to any ex vimmer is this
;;; esc quits
(defun minibuffer-keyboard-quit ()
"Abort recursive edit.
In Delete Selection mode, if the mark is active, just deactivate it;
then it takes a second \\[keyboard-quit] to abort the minibuffer."
(interactive)
(if (and delete-selection-mode transient-mark-mode mark-active)
(setq deactivate-mark t)
(when (get-buffer "*Completions*") (delete-windows-on "*Completions*"))
(abort-recursive-edit)))
(define-key evil-normal-state-map [escape] 'keyboard-quit)
(define-key evil-visual-state-map [escape] 'keyboard-quit)
(define-key minibuffer-local-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-ns-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-completion-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-must-match-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-isearch-map [escape] 'minibuffer-keyboard-quit)
so that esc actually quits pretty much anything (like pending prompts in the minibuffer)
Solution 2:
As someone who came from emacs, tried vim, and realized there were a huge number of things to gain, I did a lot of experimenting when I first started using evil. While the following are controversial, I wanted to keep the emacs keys that are used more universally in terminal, firefox, cocoa, etc..., but didn't want to lose the vim editing capabilities. I ended up deciding to rebind the following keys in my .emacs:
(define-key evil-normal-state-map "\C-e" 'evil-end-of-line)
(define-key evil-insert-state-map "\C-e" 'end-of-line)
(define-key evil-visual-state-map "\C-e" 'evil-end-of-line)
(define-key evil-motion-state-map "\C-e" 'evil-end-of-line)
(define-key evil-normal-state-map "\C-f" 'evil-forward-char)
(define-key evil-insert-state-map "\C-f" 'evil-forward-char)
(define-key evil-insert-state-map "\C-f" 'evil-forward-char)
(define-key evil-normal-state-map "\C-b" 'evil-backward-char)
(define-key evil-insert-state-map "\C-b" 'evil-backward-char)
(define-key evil-visual-state-map "\C-b" 'evil-backward-char)
(define-key evil-normal-state-map "\C-d" 'evil-delete-char)
(define-key evil-insert-state-map "\C-d" 'evil-delete-char)
(define-key evil-visual-state-map "\C-d" 'evil-delete-char)
(define-key evil-normal-state-map "\C-n" 'evil-next-line)
(define-key evil-insert-state-map "\C-n" 'evil-next-line)
(define-key evil-visual-state-map "\C-n" 'evil-next-line)
(define-key evil-normal-state-map "\C-p" 'evil-previous-line)
(define-key evil-insert-state-map "\C-p" 'evil-previous-line)
(define-key evil-visual-state-map "\C-p" 'evil-previous-line)
(define-key evil-normal-state-map "\C-w" 'evil-delete)
(define-key evil-insert-state-map "\C-w" 'evil-delete)
(define-key evil-visual-state-map "\C-w" 'evil-delete)
(define-key evil-normal-state-map "\C-y" 'yank)
(define-key evil-insert-state-map "\C-y" 'yank)
(define-key evil-visual-state-map "\C-y" 'yank)
(define-key evil-normal-state-map "\C-k" 'kill-line)
(define-key evil-insert-state-map "\C-k" 'kill-line)
(define-key evil-visual-state-map "\C-k" 'kill-line)
(define-key evil-normal-state-map "Q" 'call-last-kbd-macro)
(define-key evil-visual-state-map "Q" 'call-last-kbd-macro)
(define-key evil-normal-state-map (kbd "TAB") 'evil-undefine)
(defun evil-undefine ()
(interactive)
(let (evil-mode-map-alist)
(call-interactively (key-binding (this-command-keys)))))
Unfortunately, these overlap with the vim "move one screen up or down" operations. However, I have become comfortable using the following instead:
(define-key evil-normal-state-map (kbd "DEL") (lambda ()
(interactive)
(previous-line 10)
(evil-scroll-line-up 10)
))
(define-key evil-normal-state-map (kbd "=") (lambda ()
(interactive)
(next-line 10)
(evil-scroll-line-down 10)
))
Also, if you are coming from vim and want a quick path from insert to normal mode using "jk" (or any other 2 stroke combination), the best way is to copy the text from http://www.emacswiki.org/emacs/download/key-chord.el and paste it into your ~/.emacs.d/key-chord.el . Then add the following to your .emacs:
;load a file named key-chord.el from some directory in the load-path (e.g. "~/.emacs.d")
(require 'key-chord)
(key-chord-mode 1)
(key-chord-define-global "jk" 'evil-normal-state)
Also, if you are coming from vim and you think the copy-to-clipboard in emacs is no good, you're probably right. However, you may find the following useful after running sudo apt-get install xsel:
(defun copy-to-clipboard ()
(interactive)
(if (display-graphic-p)
(progn
(message "Yanked region to x-clipboard!")
(call-interactively 'clipboard-kill-ring-save)
)
(if (region-active-p)
(progn
(shell-command-on-region (region-beginning) (region-end) "xsel -i -b")
(message "Yanked region to clipboard!")
(deactivate-mark))
(message "No region active; can't yank to clipboard!")))
)
(evil-define-command paste-from-clipboard()
(if (display-graphic-p)
(progn
(clipboard-yank)
(message "graphics active")
)
(insert (shell-command-to-string "xsel -o -b"))
)
)
(global-set-key [f8] 'copy-to-clipboard)
(global-set-key [f9] 'paste-from-clipboard)
Obviously, you will have to decide for yourself whether any of these controversial changes are worth it, but perhaps these basic changes will inspire you.
For some other really cool function implementations, such as delete and paste, delete without copying to clipboard, efficient 4x / 16x movement, use of counts for paste register specification, tab settings that actually work for c/c++, and more, you can check out the full .emacs, init.el, my-keymaps.el, and my-functions.el versons on my git at https://github.com/Russell91/emacs
Solution 3:
I also used to be a Viper/Vimpulse user, with a huge amount of configuration. Then I found Evil-mode.
What's your sharing-worthy experiences/tips on this topic?
This is my whole evil-mode configuration, and it works great for me:
(require 'evil)
(evil-mode 1)
;; Remap org-mode meta keys for convenience
(mapcar (lambda (state)
(evil-declare-key state org-mode-map
(kbd "M-l") 'org-metaright
(kbd "M-h") 'org-metaleft
(kbd "M-k") 'org-metaup
(kbd "M-j") 'org-metadown
(kbd "M-L") 'org-shiftmetaright
(kbd "M-H") 'org-shiftmetaleft
(kbd "M-K") 'org-shiftmetaup
(kbd "M-J") 'org-shiftmetadown))
'(normal insert))
Have you encounter any conflicts between this mode and others?
No, in contrast to Viper/Vimpulse which was causing trouble in several modes.
Solution 4:
I started to use Evil a month ago; before it, I tried to use viper/vimpulse without much of success. To be honest, vimpulse is quite nice, but using it with various modes was a bit troublesome (e.g. compilation mode where vimpulse went always crazy) leaving emacs in some mode between vi-emacs-something.
When I switched to Evil, I finally started to explore full Emacs power, and believe me, I didn't regret. Evil works nicely in all modes I used (mostly editing, compilation, scratch and eshell) and even reading info/man/help is working without any problems.
Except that, I only found buffer switching odd as I used to do :b<0-9> instead :b-TAB-then-complete-name or :bn. Note however that Evil developers tries (in some cases) to reduce duplicate functionalities, so instead :! (to exec shell command), you should use native M-!.
If you find urge to add/redefine some custom ex commands, just open evil-maps.el and edit it (try that in vim!).
Evil is still young but promising project and I'm waiting the day when will replace viper in official Emacs distribution.