I showed you my source code, pls respond
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
9.7 KiB

  1. #+TITLE: Editor
  2. #+AUTHOR: Christopher James Hayward
  3. #+EMAIL: chris@chrishayward.xyz
  4. #+PROPERTY: header-args:emacs-lisp :tangle editor.el :comments org
  5. #+PROPERTY: header-args :results silent :eval no-export :comments org
  6. #+OPTIONS: num:nil toc:nil todo:nil tasks:nil tags:nil
  7. #+OPTIONS: skip:nil author:nil email:nil creator:nil timestamp:nil
  8. Configuration and imrpovements to the editor experience within Emacs. *Vim*[fn:1] user? This module extends the keybindings by implementing *Evil*[fn:2]. *Doom*[fn:3], *Spacemacs*[fn:4]? The all powerful leader key is also implemented right here!
  9. * Keys
  10. Offer =ESC= as an alternative to quit (most) prompts, instead of the default =C-g=.
  11. #+begin_src emacs-lisp
  12. (global-set-key (kbd "<escape>") 'keyboard-escape-quit)
  13. #+end_src
  14. ** Completions
  15. Emacs has *a lot of keybindings*, sometimes it's useful to just start mashing keys and see what happens. This behaviour exists in a third-party package called *which-key*[fn:5]. It displays the current incomplete keybinding input in a mini-buffer, showing available completion options with their corresponding keybindings.
  16. #+begin_src emacs-lisp
  17. (use-package which-key
  18. :diminish which-key-mode
  19. :custom (which-key-idle-delay dotfiles/idle)
  20. :config (which-key-mode))
  21. #+end_src
  22. ** Turn Emacs into Vim
  23. Emacs has *some strange default keybindings*, they're not like any other editor you've likely ever used. To overcome this nearly show-stopping hurdle, we turn Emacs into Vim[fn:1] with *Evil Mode - The Extensible VI Layer for Emacs*[fn:2].
  24. + Enable visual line motions outside of ~visual-line-mode~ buffers
  25. #+begin_src emacs-lisp
  26. (use-package evil
  27. :custom (evil-want-integration t) ;; Required for `evil-collection'.
  28. (evil-want-keybinding nil) ;; Same as above
  29. :config (evil-global-set-key 'motion "j" 'evil-next-visual-line)
  30. (evil-global-set-key 'motion "k" 'evil-previous-visual-line)
  31. (evil-mode +1))
  32. #+end_src
  33. While covering substantial ground towards our goal, the default keybindings implemented in *Evil*[fn:2] alone are *lacking* compared to what you would expect from *Vim*[fn:1]. There's, of course, a communicated curated package *evil-collection*[fn:6] that does a much better job implementing the proper keybindings.
  34. #+begin_src emacs-lisp
  35. (use-package evil-collection
  36. :after evil
  37. :config (evil-collection-init))
  38. #+end_src
  39. *** Surround text
  40. Whether it's on purpose, or more likely, you forgot an opening brace; *evil-surround*[fn:7] surrounds highlighted blocks of text with functions, quotations, and any symbol you can input.
  41. #+begin_src emacs-lisp
  42. (use-package evil-surround
  43. :after evil
  44. :config (global-evil-surround-mode 1))
  45. #+end_src
  46. *** Toggle comments
  47. When you're in deep with errors, or just trying some new code, it's useful to be able to toggle large comment sections in a language agnostic manner. In comes *evil-nerd-commentor*[fn:8], with a custom binding to =M-;=. What is =M-= ? Typically that refers to the =Alt= key, called the =Meta= key in Emacs.
  48. #+begin_src emacs-lisp
  49. (use-package evil-nerd-commenter
  50. :after evil
  51. :bind ("M-;" . evilnc-comment-or-uncomment-lines))
  52. #+end_src
  53. ** Implementing the leader key
  54. If you're like me and started with Emacs using a framework like *Doom*[fn:3] or *Spacemacs*[fn:4], you probably have a lot of muscle memory for using =SPC= as a leader key. This behaviour is actually not difficult to implement, especially when using *general.el*[fn:9].
  55. + =SPC= in most situations as a prefix key
  56. + =C-SPC= when using the [[file:desktop.org][Desktop]] module within an =X= buffer
  57. #+begin_src emacs-lisp
  58. (use-package general
  59. :after evil
  60. :config
  61. (general-create-definer dotfiles/leader
  62. :states '(normal motion)
  63. :keymaps 'override
  64. :prefix dotfiles/leader-key
  65. :global-prefix dotfiles/leader-key-global))
  66. #+end_src
  67. ** Transient bindings
  68. Create transient keybindings with a shared prefix through *Hydra*[fn:10]. This is also used by a number of third-party packages as a completion system. An implementation example is available in the *Font* section of the [[file:interface.org][Interface]] module.
  69. + Defer loading for performance
  70. #+begin_src emacs-lisp
  71. (use-package hydra
  72. :defer t)
  73. #+end_src
  74. Place runtime tweaks behind =SPC t=
  75. #+begin_src emacs-lisp
  76. (dotfiles/leader
  77. "t" '(:ignore t :which-key "Tweaks"))
  78. #+end_src
  79. ** Cherry picked shortcuts
  80. Implement shortcut bindings, cherry picked from *Doom*[fn:3].
  81. + Close buffers with =SPC c=
  82. + Find files with =SPC , (comma)=
  83. #+begin_src emacs-lisp
  84. (dotfiles/leader
  85. "." '(find-file :which-key "Files")
  86. "c" '(kill-buffer-and-window :which-key "Close"))
  87. #+end_src
  88. *** Managing windows
  89. Window management with =SPC w=
  90. + Swap with =w=
  91. + Close with =c=
  92. + Move with =h,j,k,l=
  93. + Split with =s - <motion>=
  94. #+begin_src emacs-lisp
  95. (dotfiles/leader
  96. "w" '(:ignore t :which-key "Window")
  97. "ww" '(window-swap-states :which-key "Swap")
  98. "wc" '(delete-window :which-key "Close")
  99. "wh" '(windmove-left :which-key "Left")
  100. "wj" '(windmove-down :which-key "Down")
  101. "wk" '(windmove-up :which-key "Up")
  102. "wl" '(windmove-right :which-key "Right")
  103. "ws" '(:ignore t :which-key "Split")
  104. "wsj" '(split-window-below :which-key "Down")
  105. "wsl" '(split-window-right :which-key "Right"))
  106. #+end_src
  107. *** Quitting Emacs
  108. Quit Emacs with =SPC q=
  109. + Save and quit =q=
  110. + Quit without saving =w=
  111. + Exit the Frame (daemon) =f=
  112. #+begin_src emacs-lisp
  113. (dotfiles/leader
  114. "q" '(:ignore t :which-key "Quit")
  115. "qq" '(save-buffers-kill-emacs :which-key "Save")
  116. "qw" '(kill-emacs :which-key "Now")
  117. "qf" '(delete-frame :which-key "Frame"))
  118. #+end_src
  119. * Help
  120. Use the built-in ~describe-*~ functionality of Emacs to quickly access documentation for packages, variables, and functions.
  121. + Run helper functions with =SPC h=
  122. * Packages =p=
  123. * Variables =v=
  124. * Functions =f=
  125. #+begin_src emacs-lisp
  126. (dotfiles/leader
  127. "h" '(:ignore t :which-key "Help")
  128. "hp" '(describe-package :which-key "Package")
  129. "hv" '(describe-variable :which-key "Variable")
  130. "hf" '(describe-function :which-key "Function"))
  131. #+end_src
  132. * Files
  133. Emacs has some really cool built-in packages, *Dired*[fn:11] is one of them. It's not perfect out of the box though, there's work to do.
  134. ** Navigating to the current directory
  135. I don't want to have to press =RET= twice to navigate to the current directory. Avoid this behaviour with ~jump~, included in the =dired-x= package that ships with *Dired*[fn:11].
  136. + Open a new dired buffer with =SPC d=.
  137. #+begin_src emacs-lisp
  138. (require 'dired-x)
  139. (dotfiles/leader
  140. "d" '(dired-jump :which-key "Dired"))
  141. #+end_src
  142. ** Reusing the same buffer
  143. By default *Dired*[fn:11] will create a new buffer every time you press =RET= over a directory. This leads to unwanted buffers all over the place. Avoid this behaviour with *Dired Single*[fn:12], reusing the same dired buffer.
  144. + Move up a directory with =h=
  145. + Open a single buffer with =l=
  146. #+begin_src emacs-lisp
  147. (use-package dired-single
  148. :config (evil-collection-define-key 'normal 'dired-mode-map
  149. "h" 'dired-single-up-directory
  150. "l" 'dired-single-buffer))
  151. #+end_src
  152. * Shell
  153. Another really incredible piece of kit, shipped with Emacs. *Eshell*[fn:15] is a fully POSIX compliant shell written entirely in Emacs Lisp. While not a traditional terminal emulator, it provides me with all of the functionality I expect and require from one. The infamous lambda prompt implemented with the *Eshell Prompt Extras*[fn:16] package.
  154. #+begin_src emacs-lisp
  155. (use-package eshell-prompt-extras
  156. :custom (eshell-highlight-prompt nil)
  157. (eshell-prefer-lisp-functions nil)
  158. (eshell-prompt-function 'epe-theme-lambda))
  159. #+end_src
  160. Open an =eshell= buffer with =SPC e=.
  161. #+begin_src emacs-lisp
  162. (dotfiles/leader
  163. "e" '(eshell :which-key "Shell"))
  164. #+end_src
  165. * Magit
  166. #+ATTR_ORG: :width 420px
  167. #+ATTR_HTML: :width 420px
  168. #+ATTR_LATEX: :width 420px
  169. [[../docs/images/2021-02-13-example-magit.gif]]
  170. Yet another hallmark feature of Emacs: *Magit*[fn:17] with the *darling* name, the developer stresses it's supposed to be *Magic* but with *Git*[fn:19]. It's a complete *Git*[fn:19] porcelain within Emacs.
  171. #+begin_src emacs-lisp
  172. (use-package magit
  173. :commands magit-status
  174. :custom (magit-display-buffer-function
  175. #'magit-display-buffer-same-window-except-diff-v1))
  176. #+end_src
  177. Open the *status* page for the current repository with =SPC g=.
  178. #+begin_src emacs-lisp
  179. (dotfiles/leader
  180. "g" '(magit-status :which-key "Magit"))
  181. #+end_src
  182. ** GitHub integration
  183. Interact with *Git*[fn:19] forges from *Magit*[fn:17] and Emacs using *Forge*[fn:18], requiring only a *GitHub*[fn:20] token to get started. If you're not sure what *GitHub*[fn:20] is, it's to *Git*[fn:19] what *Porn* is to *PornHub*. No citations!
  184. + Requires a valid ~$GITHUB_TOKEN~
  185. #+begin_src emacs-lisp
  186. (use-package forge
  187. :after magit)
  188. #+end_src
  189. ** Deploying the global config
  190. *Git*[fn:19] reads its global config from ~$HOME/.gitconfig~, create a link to the custom configuration.
  191. #+begin_src emacs-lisp
  192. (dotfiles/symlink "~/.emacs.d/config/git"
  193. "~/.gitconfig")
  194. #+end_src
  195. * Resources
  196. [fn:1] https://vim.org
  197. [fn:2] https://evil.readthedocs.io/en/latest/index.html
  198. [fn:3] https://github.com/hlissner/doom-emacs/
  199. [fn:4] https://spacemacs.org
  200. [fn:5] https://github.com/justbur/emacs-which-key/
  201. [fn:6] https://github.com/emacs-evil/evil-collection
  202. [fn:7] https://github.com/emacs-evil/evil-surround
  203. [fn:8] https://github.com/redguardtoo/evil-nerd-commenter
  204. [fn:9] https://github.com/noctuid/general.el
  205. [fn:10] https://github.com/abo-abo/hydra
  206. [fn:11] https://en.wikipedia.org/wiki/Dired
  207. [fn:12] https://github.com/crocket/dired-single
  208. [fn:15] https://gnu.org/software/emacs/manual/html_node/eshell/index.html
  209. [fn:16] https://github.com/zwild/eshell-prompt-extras
  210. [fn:17] https://github.com/magit/magit
  211. [fn:18] https://github.com/magit/forge
  212. [fn:19] https://git-scm.com
  213. [fn:20] https://github.com