0%

Emacs config from the scratch

I used to use spacemacs as my daily environment for developing. Spacemacs is powerful and fancy. But sometimes I have to accept it’s concept and common usages. Yesterday I tried to upgrade my Emacs from 25 to 27 which broke the spacemacs config. Then I think it’s a good time to learn more detail about Emacs. So I decide to write my own config from the scratch.

It will be time cost but maybe it’s worth to do it. I would like to have fully control over the powerful Emacs ;)

This blog will keep updating as my simple .emacs file growing. Thanks for every suggestion and tutorial. I will later list all the resources and tutorials I have met during the configuration.

Features

  1. multiple cursor operations
  2. Alt+arrow key to jump between windows
  3. Alt+num (0~8) to switch to windows directly
  4. Side-bar imenu

flymake settings

  1. global config file is under ~/.config/flake8

TODO

  • C++ language support
  • better M-x menu
  • select multiple words and edit
  • better fuzzy search
  • add ivy for search
  • window management. restore or forth to windows status. save windows status
  • general jump in buffer. save bookmark etc.

Language support packages

Python

  1. python programming in emacs
  2. Elpy The package I used in this configuration.

shortcuts

.emacs

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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

;;; HOW TO DEBUG
;;; (toggle-debug-on-error)
;;; (toggle-debug-on-quit)


(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(custom-safe-themes
'("04232a0bfc50eac64c12471607090ecac9d7fd2d79e388f8543d1c5439ed81f5" "6e70d505e0957aaa67562ff0487b7b1b1e10f879655f2c47adf85949790fb687" default))
'(display-line-numbers t)
'(nil nil t)
'(package-selected-packages
'(magit-stgit counsel-projectile projectile zenburn-theme zenburn magit go-mode counsel ivy-hydra imenus multiple-cursors imenu-list use-package elpy winum)))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(org-document-title ((t (:inherit default :weight bold :foreground "black" :family "Sans Serif" :height 2.0 :underline nil))))
'(org-level-1 ((t (:inherit default :weight bold :foreground "black" :family "Sans Serif" :height 1.75))))
'(org-level-2 ((t (:inherit default :weight bold :foreground "black" :family "Sans Serif" :height 1.5))))
'(org-level-3 ((t (:inherit default :weight bold :foreground "black" :family "Sans Serif" :height 1.25))))
'(org-level-4 ((t (:inherit default :weight bold :foreground "black" :family "Sans Serif" :height 1.1))))
'(org-level-5 ((t (:inherit default :weight bold :foreground "black" :family "Sans Serif"))))
'(org-level-6 ((t (:inherit default :weight bold :foreground "black" :family "Sans Serif"))))
'(org-level-7 ((t (:inherit default :weight bold :foreground "black" :family "Sans Serif"))))
'(org-level-8 ((t (:inherit default :weight bold :foreground "black" :family "Sans Serif")))))

;; load emacs 24's package system. Add MELPA repository.
(when (>= emacs-major-version 24)
(require 'package)
(add-to-list
'package-archives
;; '("melpa" . "http://stable.melpa.org/packages/") ; many packages won't show if using stable
'("melpa" . "http://melpa.org/packages/")
t))
(package-initialize)

;; create prefix key. M-m
(define-prefix-command 'M-m
)
(global-set-key (kbd "M-m") M-m)

;; split window
(global-set-key (kbd "M-m w -") 'split-window-vertically)
(global-set-key (kbd "M-m w /") 'split-window-horizontally)


;; M-[arrow] jump to
;; use-package bind-key shortcuts instead of global-set-key to prevent major mode overwrite global key settings
;; from https://emacs.stackexchange.com/questions/352/how-to-override-major-mode-bindings
(global-set-key (kbd "M-<left>") 'windmove-left)
(global-set-key (kbd "M-<right>") 'windmove-right)
(global-set-key (kbd "M-<up>") 'windmove-up)
(global-set-key (kbd "M-<down>") 'windmove-down)

;; M-num jump to window directly
(global-set-key (kbd "C-`") 'winum-select-window-by-number)
(global-set-key (kbd "C-²") 'winum-select-window-by-number)
(global-set-key (kbd "M-0") 'winum-select-window-0-or-10)
(global-set-key (kbd "M-1") 'winum-select-window-1)
(global-set-key (kbd "M-2") 'winum-select-window-2)
(global-set-key (kbd "M-3") 'winum-select-window-3)
(global-set-key (kbd "M-4") 'winum-select-window-4)
(global-set-key (kbd "M-5") 'winum-select-window-5)
(global-set-key (kbd "M-6") 'winum-select-window-6)
(global-set-key (kbd "M-7") 'winum-select-window-7)
(global-set-key (kbd "M-8") 'winum-select-window-8)

(require 'winum)
(defun winum-assign-9-to-calculator-8-to-flycheck-errors ()
(cond
((equal (buffer-name) "*Calculator*") 9)
((equal (buffer-name) "*Flycheck errors*") 8)))

(defun winum-assign-0-to-neotree ()
(when (string-match-p (buffer-name) ".*\\*NeoTree\\*.*") 10))

(add-to-list 'winum-assign-functions #'winum-assign-9-to-calculator-8-to-flycheck-errors)
(add-to-list 'winum-assign-functions #'winum-assign-0-to-neotree)

(set-face-attribute 'winum-face nil :weight 'bold)


(setq window-numbering-scope 'global
winum-reverse-frame-list nil
winum-auto-assign-0-to-minibuffer t
winum-assign-func 'my-winum-assign-func
winum-auto-setup-mode-line t
winum-format " %s "
winum-mode-line-position 1
winum-ignored-buffers '(" *which-key*"))

(winum-mode)

;; python
;; elpy
(elpy-enable)
;; disable shortcuts under elpy-mode, these are conflit with windows management and not so useful
(define-key elpy-mode-map (kbd "M-<left>") nil)
(define-key elpy-mode-map (kbd "M-<right>") nil)
(define-key elpy-mode-map (kbd "M-<up>") nil)
(define-key elpy-mode-map (kbd "M-<down>") nil)


;; windows management
;; use 'winner-mode' to redo and undo windows. This is usefull when you want to maxinum one window and back to multiple windows later.
;; winner-redo C-c <right>
;; winner-undo C-c <left>
(winner-mode 1)


;; imenu-list
;; shortcuts: Ctrl-'

(use-package imenu-list
:ensure t
:bind ("C-." . imenu-list-minor-mode)
:config
(setq imenu-list-focus-after-activation t))

(global-set-key (kbd "C-'") 'imenu-list-smart-toggle)


;; use which-function-mode to show where I am. usefull when function/class is huge
(which-function-mode)
;; when it cannot determinted, show n/a instead of ???
(setq which-func-unknown "n/a")
;; show the position on top
(setq-default header-line-format
'((which-func-mode ("" which-func-format " "))))
(setq mode-line-misc-info
;; We remove Which Function Mode from the mode line, because it's mostly
;; invisible here anyway.
(assq-delete-all 'which-func-mode mode-line-misc-info))


;; multiple-cursors
;; mark different lines/words and edit: 1. select the word. 2. C-> to select next lines

(require 'multiple-cursors)
(global-set-key (kbd "C-S-c C-S-c") 'mc/edit-lines)
(global-set-key (kbd "C->") 'mc/mark-next-like-this)
(global-set-key (kbd "C-<") 'mc/mark-previous-like-this)


;; ivy search
(ivy-mode 1)
(setq ivy-use-virtual-buffers t)
(setq enable-recursive-minibuffers t)
(global-set-key "\C-s" 'swiper)
(global-set-key (kbd "C-c C-r") 'ivy-resume)
(global-set-key (kbd "<f6>") 'ivy-resume)
(global-set-key (kbd "M-x") 'counsel-M-x)
(global-set-key (kbd "C-x C-f") 'counsel-find-file)
(global-set-key (kbd "<f1> f") 'counsel-describe-function)
(global-set-key (kbd "<f1> v") 'counsel-describe-variable)
(global-set-key (kbd "<f1> l") 'counsel-find-library)
(global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
(global-set-key (kbd "<f2> u") 'counsel-unicode-char)
(global-set-key (kbd "C-c g") 'counsel-git)
(global-set-key (kbd "C-c j") 'counsel-git-grep)
(global-set-key (kbd "C-c k") 'counsel-ag)
(global-set-key (kbd "C-x l") 'counsel-locate)
(global-set-key (kbd "C-S-o") 'counsel-rhythmbox)
(define-key minibuffer-local-map (kbd "C-r") 'counsel-minibuffer-history)


(global-set-key (kbd "C-s-<right>") (lambda () (interactive) (enlarge-window-horizontally 5)))
(global-set-key (kbd "C-s-<left>") (lambda () (interactive) (shrink-window-horizontally 5)))
(global-set-key (kbd "C-s-<up>") (lambda () (interactive) (enlarge-window 5)))
(global-set-key (kbd "C-s-<down>") (lambda () (interactive) (shrink-window 5)))


;; ;; projectile
;; ;; (projectile-mode)
;; (counsel-projectile-mode 1)
;; (define-key projectile-mode-map (kbd "C-c p") 'projectile-command-map)
;; ;; set completion option
;; (setq projectile-completion-system 'ivy)
;; (setq 'projectile-globally-ignored-directories '(".git"
;; ".svn"
;; ".venv"))

;; (setq 'projectile-globally-ignored-files '(".pyc"
;; "~"))


;; (let ((projectile-project-root-files-top-down-recurring '(".git"))))
;; ;; enable projectile caching
;; (setq projectile-enable-caching t)
;; (setq projectile-use-native-indexing t)


;;====================================
;; Searching
;;====================================

;; use counsel-git to find file under current git repo
;; C-c g (counsel-git)


;; search buffer
;; I know that string is in my Emacs somewhere!
(require 'cl)
(defcustom search-all-buffers-ignored-files (list (rx-to-string '(and bos (or ".bash_history" "TAGS") eos)))
"Files to ignore when searching buffers via \\[search-all-buffers]."
:type 'editable-list)

(require 'grep)
(defun search-all-buffers (regexp prefix)
"Searches file-visiting buffers for occurence of REGEXP. With
prefix > 1 (i.e., if you type C-u \\[search-all-buffers]),
searches all buffers."
(interactive (list (grep-read-regexp)
current-prefix-arg))
(message "Regexp is %s; prefix is %s" regexp prefix)
(multi-occur
(if (member prefix '(4 (4)))
(buffer-list)
(remove-if
(lambda (b) (some (lambda (rx) (string-match rx (file-name-nondirectory (buffer-file-name b)))) search-all-buffers-ignored-files))
(remove-if-not 'buffer-file-name (buffer-list))))

regexp))

(global-set-key [f7] 'search-all-buffers)


;; hide welcome screen, show scratch directly
(setq inhibit-startup-screen t)

;; enable code folding by using hideshow
;; https://www.emacswiki.org/emacs/HideShow
;; C-c @ C-M-s show all
;; C-c @ C-M-h hide all
;; C-c @ C-s show block
;; C-c @ C-h hide block
;; C-c @ C-c toggle hide/show
;; (hs-minor-mode t)


;;
;; flymake
;; Finding Syntax Errors On The Fly
;; https://www.gnu.org/software/emacs/manual/html_node/emacs/Flymake.html
(define-key flymake-mode-map (kbd "M-n") 'flymake-goto-next-error)
(define-key flymake-mode-map (kbd "M-p") 'flymake-goto-prev-error)

;;
;; c++ mode setting up
;;
;; mapping for file extension and mode
;; set .h header file open in c++ mode
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))

(defun bucketzxm-c++-mode-hook ()
(setq c-basic-offset 4)
(c-set-offset 'substatement-open 0))

(add-hook 'c++-mode-hook 'bucketzxm-c++-mode-hook)


;;
;; markdown
;;
;; M-x (flymd-flyit) to start a browser
(require 'flymd)
(setq flymd-output-directory "/tmp")
;; set browser to firefox due to compatibility problem
;; https://github.com/mola-T/flymd/blob/master/browser.md#user-content-chrome-windows-or-uix
(defun my-flymd-browser-function (url)
(let ((browse-url-browser-function 'browse-url-firefox))
(browse-url url)))
(setq flymd-browser-open-function 'my-flymd-browser-function)



;;
;; org-mode
;;
;; reference: https://zzamboni.org/post/beautifying-org-mode-in-emacs/
;;

;; hide the emphasis markup(e.g. /.../ for italics
(setq org-hide-emphasis-markers t)

;; set font-lock subsitution for list markers (use - for list)
(font-lock-add-keywords 'org-mode
'(("^ *\\([-]\\) "
(0 (prog1 () (compose-region (match-beginning 1) (match-end 1) "•"))))))
;; replace all headlines markers with different Unicode bullets
(use-package org-bullets
:config
(add-hook 'org-mode-hook (lambda () (org-bullets-mode 1))))

;; set up a nice proportional font, in different sizes, for the headlines.
(let* ((variable-tuple
(cond ((x-list-fonts "Source Sans Pro") '(:font "Source Sans Pro"))
((x-list-fonts "Lucida Grande") '(:font "Lucida Grande"))
((x-list-fonts "Verdana") '(:font "Verdana"))
((x-family-fonts "Sans Serif") '(:family "Sans Serif"))
(nil (warn "Cannot find a Sans Serif Font. Install Source Sans Pro."))))
(base-font-color (face-foreground 'default nil 'default))
(headline `(:inherit default :weight bold :foreground ,base-font-color)))

(custom-theme-set-faces
'user
`(org-level-8 ((t (,@headline ,@variable-tuple))))
`(org-level-7 ((t (,@headline ,@variable-tuple))))
`(org-level-6 ((t (,@headline ,@variable-tuple))))
`(org-level-5 ((t (,@headline ,@variable-tuple))))
`(org-level-4 ((t (,@headline ,@variable-tuple :height 1.1))))
`(org-level-3 ((t (,@headline ,@variable-tuple :height 1.25))))
`(org-level-2 ((t (,@headline ,@variable-tuple :height 1.5))))
`(org-level-1 ((t (,@headline ,@variable-tuple :height 1.75))))
`(org-document-title ((t (,@headline ,@variable-tuple :height 2.0 :underline nil))))))


;; change default font
(add-to-list 'default-frame-alist
'(font . "Fira Code-12:Regular"))

;; get a good theme
;; (add-to-list 'custom-theme-load-path "~/.emacs.d/themes/zenburn")
;; (load-theme 'zenburn 1)