-
Notifications
You must be signed in to change notification settings - Fork 5
/
setup-evil.el
180 lines (154 loc) · 6.51 KB
/
setup-evil.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
;;; evil-leader - ViM leader key for evil-mode
;;; https://github.com/cofi/evil-leader
(require-package 'evil-leader)
(global-evil-leader-mode 1)
;; keyboard shortcuts
(evil-leader/set-key
"a" 'ag-project
"A" 'ag
"b" 'ido-switch-buffer
"B" 'ido-switch-buffer-other-window
"c" 'mc/mark-next-like-this
"C" 'mc/mark-all-like-this
"d" 'dired-jump
"e" 'er/expand-region
"E" 'mc/edit-lines
"f" 'ido-find-file
"g" 'magit-status
"G" 'magit-blame-mode
"i" 'idomenu
"j" 'ace-jump-mode
"J" 'ace-jump-word-mode
"k" 'kill-this-buffer
"K" 'kill-buffer
"l" 'linum-mode
"o" 'occur
"O" 'browse-url
"p" 'magit-find-file-completing-read
"P" 'popwin:popup-last-buffer
"r" 'bw/recentf-ido-find-file
"R" 'bookmark-jump
"s" 'ag-project
"t" 'bw/open-term
"T" 'eshell
"w" 'save-buffer
"x" 'smex
"y" 'bury-buffer)
;;; Evil - Emacs vi emulation
;;; http://gitorious.org/evil/pages/Home
(require-package 'evil)
(after-load 'evil
(setq
;; this stops evil from overwriting the cursor color
evil-default-cursor t
;; h/l wrap around to next lines
evil-cross-lines t
evil-default-state 'normal
;; include first/last character when moving to e/bol
evil-want-visual-char-semi-exclusive t
;; don't move the cursor around like Vim
evil-move-cursor-back nil
)
;; use ido to open files
(define-key evil-ex-map "e " 'ido-find-file)
(define-key evil-ex-map "b " 'ido-switch-buffer)
;; make end-of-line work in insert
(define-key evil-insert-state-map "\C-e" 'end-of-line)
;; Make C-g work like <esc>
(define-key evil-normal-state-map "\C-g" 'evil-normal-state)
(define-key evil-visual-state-map "\C-g" 'evil-normal-state)
(define-key evil-insert-state-map "\C-g" 'evil-normal-state)
(define-key evil-normal-state-map [escape] 'keyboard-quit)
(define-key evil-visual-state-map [escape] 'keyboard-quit)
(define-key minibuffer-local-map [escape] 'abort-recursive-edit)
(define-key minibuffer-local-ns-map [escape] 'abort-recursive-edit)
(define-key minibuffer-local-completion-map [escape] 'abort-recursive-edit)
(define-key minibuffer-local-must-match-map [escape] 'abort-recursive-edit)
(define-key minibuffer-local-isearch-map [escape] 'abort-recursive-edit)
;; evil integration for indirect regions
;; from: http://demonastery.org/2013/04/emacs-evil-narrow-region/
(evil-define-operator evil-narrow-indirect (beg end type)
"Indirectly narrow the region from BEG to END."
(interactive "<R>")
(evil-normal-state)
(narrow-to-region-indirect beg end))
(define-key evil-normal-state-map "m" 'evil-narrow-indirect)
(define-key evil-visual-state-map "m" 'evil-narrow-indirect)
;; advanced ace-jump-mode integration.
;; keybindings and advice gotten from:
;; https://gist.github.com/cofi/4963125
(dolist (ace-jump-map '(("SPC" . ace-jump-char-mode)
("C-SPC" . ace-jump-word-mode)
("C-<return>" . ace-jump-line-mode)))
(define-key evil-normal-state-map (kbd (car ace-jump-map)) `,(cdr ace-jump-map))
(define-key evil-motion-state-map (kbd (car ace-jump-map)) `,(cdr ace-jump-map)))
(defadvice evil-visual-line (before spc-for-line-jump activate)
(define-key evil-motion-state-map (kbd "SPC") #'evil-ace-jump-line-mode))
(defadvice evil-visual-char (before spc-for-char-jump activate)
(define-key evil-motion-state-map (kbd "SPC") #'evil-ace-jump-char-mode))
(defadvice evil-visual-block (before spc-for-char-jump activate)
(define-key evil-motion-state-map (kbd "SPC") #'evil-ace-jump-char-mode))
;; on OSX, stop copying each visual state move to the clipboard:
;; https://bitbucket.org/lyro/evil/issue/336/osx-visual-state-copies-the-region-on
;; Most of this code grokked from:
;; http://stackoverflow.com/questions/15873346/elisp-rename-macro
(defadvice evil-visual-update-x-selection (around clobber-x-select-text activate)
(unless (featurep 'ns)
ad-do-it))
;; modes to map to different default states
(dolist (mode-map '((ag-mode . emacs)
(cider-repl-mode . emacs)
(comint-mode . emacs)
(eshell-mode . emacs)
(fundamental-mode . emacs)
(git-commit-mode . insert)
(git-rebase-mode . emacs)
(help-mode . emacs)
(paradox-menu-mode . emacs)
(term-mode . emacs)))
(evil-set-initial-state `,(car mode-map) `,(cdr mode-map))))
(autoload 'evil-mode "evil" "Emacs Vi emuLation" t)
(global-set-key (kbd "C-z") 'evil-mode)
;;; linum-relative - makes Evil behave like:
;;; https://github.com/myusuf3/numbers.vim
;;; https://github.com/coldnew/linum-relative
(require-package 'linum-relative)
(after-load 'linum-relative
(defun bw/linum-non-relative (line-number)
"Linum formatter that copies the format"
(propertize (format linum-relative-format line-number)
'face 'linum))
(defun bw/linum-relative-formatting ()
"Turn on relative formatting"
(setq-local linum-format 'linum-relative))
(defun bw/linum-normal-formatting ()
"Turn on non-relative formatting"
(setq-local linum-format 'bw/linum-non-relative))
;; I never use linum-mode except for this, so it's okay to
;; clobber it
(setq linum-format 'bw/linum-non-relative
;; show >> on line where cursor is
linum-relative-current-symbol ">>")
;; in Normal mode, use relative numbering
(add-hook 'evil-normal-state-entry-hook 'bw/linum-relative-formatting)
;; in Insert mode, use normal line numbering
(add-hook 'evil-insert-state-entry-hook 'bw/linum-normal-formatting)
;; turn off linum mode automatically when entering Emacs mode
(add-hook 'evil-emacs-state-entry-hook 'bw/disable-linum-mode)
;; turn off linum mode when entering Emacs
(add-hook 'evil-emacs-state-entry-hook 'bw/linum-normal-formatting)
;; copy linum face so it doesn't look weird
(set-face-attribute 'linum-relative-current-face nil :foreground (face-attribute 'font-lock-keyword-face :foreground) :background nil :inherit 'linum :bold t))
(require 'linum-relative)
;; abbrev comes with evil
(diminish 'abbrev-mode)
;; evil-surround - Emacs version of surround.vim
;; https://github.com/timcharper/evil-surround
(require-package 'evil-surround)
(require 'evil-surround)
;; evil-god-state - One-shot god-mode from a leader key in evil
;; https://github.com/gridaphobe/evil-god-state
(require-package 'evil-god-state)
(after-load 'god-mode
(evil-define-key 'normal global-map "," 'evil-execute-in-god-state))
(provide 'setup-evil)