Made emacs more useful

This commit is contained in:
Aleksandr Lebedev 2025-01-28 16:47:15 +01:00
parent d7272fdeab
commit 7cee70abba
2 changed files with 396 additions and 14 deletions

View file

@ -3,6 +3,35 @@
#+OPTIONS: toc:2
#+PROPERTY: header-args:emacs-lisp :lexical t
* Table of contents :toc:
- [[#important-programs-to-load-first][IMPORTANT PROGRAMS TO LOAD FIRST]]
- [[#evil-mode][Evil Mode]]
- [[#recent-files][Recent Files]]
- [[#general-keybindings][General Keybindings]]
- [[#fonts][Fonts]]
- [[#zooming-inout][Zooming In/Out]]
- [[#graphical-user-interface-tweaks][GRAPHICAL USER INTERFACE TWEAKS]]
- [[#disable-menubar-toolbars-and-scrollbars][Disable Menubar, Toolbars and Scrollbars]]
- [[#display-line-numbers-and-truncated-lines][Display Line Numbers and Truncated Lines]]
- [[#org-mode][ORG MODE]]
- [[#enabling-table-of-contents][Enabling Table of Contents]]
- [[#enabling-org-bullets][Enabling Org Bullets]]
- [[#disable-electric-indent][Disable Electric Indent]]
- [[#source-code-block-tag-expansion][Source Code Block Tag Expansion]]
- [[#rainbow-mode][RAINBOW MODE]]
- [[#shells-and-terminals][SHELLS AND TERMINALS]]
- [[#eshell][Eshell]]
- [[#vterm][Vterm]]
- [[#vterm-toggle][Vterm-Toggle]]
- [[#sudo-edit][SUDO EDIT]]
- [[#nerd-icons][Nerd Icons]]
- [[#nerd-icons-completion][Nerd Icons Completion]]
- [[#buffer-move][Buffer Move]]
- [[#vertico][Vertico]]
- [[#descriptions][Descriptions]]
- [[#theme][Theme]]
- [[#theme-loading][Theme loading]]
- [[#which-key][WHICH-KEY]]
* IMPORTANT PROGRAMS TO LOAD FIRST
** Evil Mode
@ -24,22 +53,75 @@
(use-package evil-tutor
:ensure t)
#+end_src
** Recent Files
#+begin_src emacs-lisp
(recentf-mode t)
#+end_src
** General Keybindings
#+begin_src emacs-lisp
(use-package general
:ensure t
:config
(general-evil-setup)
;; set up 'SPC' as the global leader key
(general-create-definer leader-keys
:states '(normal insert visual emacs)
:keymaps 'override
:prefix "SPC" ;; set leader
:global-prefix "M-SPC") ;; access leader in insert mode
(leader-keys
"." '(find-file :wk "Find file")
"TAB TAB" '(comment-line :wk "Comment lines"))
)
(use-package general
:ensure t
:config
(general-evil-setup)
)
(require 'general) ;; If i do this in :config, it says that function is wrong
;; set up 'SPC' as the global leader key
(general-create-definer kylekrein/leader-keys
:states '(normal insert visual emacs)
:keymaps 'override
:prefix "SPC" ;; set leader
:global-prefix "M-SPC") ;; access leader in insert mode
(kylekrein/leader-keys
"." '(find-file :wk "Find file")
"f r" '(recentf-open :wk "Open recent file")
"TAB TAB" '(comment-line :wk "Comment lines"))
(kylekrein/leader-keys
"b" '(:ignore t :wk "buffer")
"b b" '(switch-to-buffer :wk "Switch buffer")
"b i" '(ibuffer :wk "Ibuffer")
"b k" '(kill-this-buffer :wk "Kill this buffer")
"b n" '(next-buffer :wk "Next buffer")
"b p" '(previous-buffer :wk "Previous buffer")
"b r" '(revert-buffer :wk "Reload buffer"))
(kylekrein/leader-keys
"e" '(:ignore t :wk "Evaluate")
"e b" '(eval-buffer :wk "Evaluate elisp in buffer")
"e d" '(eval-defun :wk "Evaluate defun containing or after point")
"e e" '(eval-expression :wk "Evaluate and elisp expression")
"e l" '(eval-last-sexp :wk "Evaluate elisp expression before point")
"e r" '(eval-region :wk "Evaluate elisp in region"))
(kylekrein/leader-keys
"h" '(:ignore t :wk "Help")
"h f" '(describe-function :wk "Describe function")
"h v" '(describe-variable :wk "Describe variable"))
;;"h r r" '((lambda () (interactive) (load-file "~/.config/emacs/init.el")) :wk "Reload emacs config"))
;;"h r r" '(reload-init-file :wk "Reload emacs config"))
(kylekrein/leader-keys
"t" '(:ignore t :wk "Toggle")
"t l" '(display-line-numbers-mode :wk "Toggle line numbers")
"t t" '(visual-line-mode :wk "Toggle truncated lines"))
(kylekrein/leader-keys
"w" '(:ignore t :wk "Windows")
;; Window splits
"w c" '(evil-window-delete :wk "Close window")
"w n" '(evil-window-new :wk "New window")
"w s" '(evil-window-split :wk "Horizontal split window")
"w v" '(evil-window-vsplit :wk "Vertical split window")
;; Window motions
"w h" '(evil-window-left :wk "Window left")
"w j" '(evil-window-down :wk "Window down")
"w k" '(evil-window-up :wk "Window up")
"w l" '(evil-window-right :wk "Window right")
"w w" '(evil-window-next :wk "Goto next window")
;; Move Windows
"w H" '(buf-move-left :wk "Buffer move left")
"w J" '(buf-move-down :wk "Buffer move down")
"w K" '(buf-move-up :wk "Buffer move up")
"w L" '(buf-move-right :wk "Buffer move right"))
#+end_src
@ -75,6 +157,17 @@ Defining the various fonts that Emacs will use.
(setq-default line-spacing 0.12)
#+end_src
** Zooming In/Out
You can use the bindings CTRL plus =/- for zooming in/out. You can also use CTRL plus the mouse wheel for zooming in/out.
#+begin_src emacs-lisp
(global-set-key (kbd "C-=") 'text-scale-increase)
(global-set-key (kbd "C--") 'text-scale-decrease)
(global-set-key (kbd "<C-wheel-up>") 'text-scale-increase)
(global-set-key (kbd "<C-wheel-down>") 'text-scale-decrease)
#+end_src
* GRAPHICAL USER INTERFACE TWEAKS
Let's make GNU Emacs look a little better.
@ -107,7 +200,295 @@ Org-bullets gives us attractive bullets rather than asterisks.
(use-package org-bullets :ensure t)
(add-hook 'org-mode-hook (lambda () (org-bullets-mode 1)))
#+end_src
** Disable Electric Indent
Org mode source blocks have some really weird and annoying default indentation behavior. I think this has to do with electric-indent-mode, which is turned on by default in Emacs. So let's turn it OFF!
#+begin_src emacs-lisp
(electric-indent-mode -1)
#+end_src
** Source Code Block Tag Expansion
Org-tempo is not a separate package but a module within org that can be enabled. Org-tempo allows for '<s' followed by TAB to expand to a begin_src tag. Other expansions available include:
| Typing the below + TAB | Expands to ... |
|------------------------+-----------------------------------------|
| <a | '#+BEGIN_EXPORT ascii' … '#+END_EXPORT |
| <c | '#+BEGIN_CENTER' … '#+END_CENTER' |
| <C | '#+BEGIN_COMMENT' … '#+END_COMMENT' |
| <e | '#+BEGIN_EXAMPLE' … '#+END_EXAMPLE' |
| <E | '#+BEGIN_EXPORT' … '#+END_EXPORT' |
| <h | '#+BEGIN_EXPORT html' … '#+END_EXPORT' |
| <l | '#+BEGIN_EXPORT latex' … '#+END_EXPORT' |
| <q | '#+BEGIN_QUOTE' … '#+END_QUOTE' |
| <s | '#+BEGIN_SRC' … '#+END_SRC' |
| <v | '#+BEGIN_VERSE' … '#+END_VERSE' |
#+begin_src emacs-lisp
(require 'org-tempo)
#+end_src
* RAINBOW MODE
Display the actual color as a background for any hex color value (ex. #ffffff). The code block below enables rainbow-mode in all programming modes (prog-mode) as well as org-mode, which is why rainbow works in this document.
#+begin_src emacs-lisp
(use-package rainbow-mode
:ensure t
:hook
((org-mode prog-mode) . rainbow-mode))
#+end_src
* SHELLS AND TERMINALS
** Eshell
Eshell is an Emacs 'shell' that is written in Elisp.
#+begin_src emacs-lisp
(use-package eshell-syntax-highlighting
:ensure t
:after esh-mode
:config
(eshell-syntax-highlighting-global-mode +1))
#+end_src
** Vterm
Vterm is a terminal emulator within Emacs. The 'shell-file-name' setting sets the shell to be used in M-x shell, M-x term, M-x ansi-term and M-x vterm. By default, the shell is set to 'fish' but could change it to 'bash' or 'zsh' if you prefer.
#+begin_src emacs-lisp
(use-package vterm
:ensure t
;;:config
)
#+end_src
** Vterm-Toggle
[[https://github.com/jixiuf/vterm-toggle][vterm-toggle]] toggles between the vterm buffer and whatever buffer you are editing.
#+begin_src emacs-lisp
(use-package vterm-toggle
:ensure t
:after vterm
:config
(setq vterm-toggle-fullscreen-p nil)
(setq vterm-toggle-scope 'project)
(add-to-list 'display-buffer-alist
'((lambda (buffer-or-name _)
(let ((buffer (get-buffer buffer-or-name)))
(with-current-buffer buffer
(or (equal major-mode 'vterm-mode)
(string-prefix-p vterm-buffer-name (buffer-name buffer))))))
(display-buffer-reuse-window display-buffer-at-bottom)
;;(display-buffer-reuse-window display-buffer-in-direction)
;;display-buffer-in-direction/direction/dedicated is added in emacs27
;;(direction . bottom)
;;(dedicated . t) ;dedicated is supported in emacs27
(reusable-frames . visible)
(window-height . 0.3))))
#+end_src
* SUDO EDIT
[[https://github.com/nflath/sudo-edit][sudo-edit]] gives us the ability to open files with sudo privileges or switch over to editing with sudo privileges if we initially opened the file without such privileges.
#+begin_src emacs-lisp
(use-package sudo-edit
:ensure t
:config
(kylekrein/leader-keys
"fu" '(sudo-edit-find-file :wk "Sudo find file")
"fU" '(sudo-edit :wk "Sudo edit file")))
#+end_src
* Nerd Icons
#+begin_src emacs-lisp
(use-package nerd-icons
:ensure t
;; :custom
;; The Nerd Font you want to use in GUI
;; "Symbols Nerd Font Mono" is the default and is recommended
;; but you can use any other Nerd Font if you want
;; (nerd-icons-font-family "Symbols Nerd Font Mono")
)
#+end_src
** Nerd Icons Completion
[[https://github.com/rainstormstudio/nerd-icons-completion]]
#+begin_src emacs-lisp
(use-package nerd-icons-completion
:ensure t
:after marginalia
:config
(nerd-icons-completion-mode)
(add-hook 'marginalia-mode-hook #'nerd-icons-completion-marginalia-setup))
#+end_src
* Buffer Move
Creating some functions to allow us to easily move windows (splits) around. The following block of code was taken from buffer-move.el found on the EmacsWiki:
https://www.emacswiki.org/emacs/buffer-move.el
#+begin_src emacs-lisp
(require 'windmove)
;;;###autoload
(defun buf-move-up ()
"Swap the current buffer and the buffer above the split.
If there is no split, ie now window above the current one, an
error is signaled."
;; "Switches between the current buffer, and the buffer above the
;; split, if possible."
(interactive)
(let* ((other-win (windmove-find-other-window 'up))
(buf-this-buf (window-buffer (selected-window))))
(if (null other-win)
(error "No window above this one")
;; swap top with this one
(set-window-buffer (selected-window) (window-buffer other-win))
;; move this one to top
(set-window-buffer other-win buf-this-buf)
(select-window other-win))))
;;;###autoload
(defun buf-move-down ()
"Swap the current buffer and the buffer under the split.
If there is no split, ie now window under the current one, an
error is signaled."
(interactive)
(let* ((other-win (windmove-find-other-window 'down))
(buf-this-buf (window-buffer (selected-window))))
(if (or (null other-win)
(string-match "^ \\*Minibuf" (buffer-name (window-buffer other-win))))
(error "No window under this one")
;; swap top with this one
(set-window-buffer (selected-window) (window-buffer other-win))
;; move this one to top
(set-window-buffer other-win buf-this-buf)
(select-window other-win))))
;;;###autoload
(defun buf-move-left ()
"Swap the current buffer and the buffer on the left of the split.
If there is no split, ie now window on the left of the current
one, an error is signaled."
(interactive)
(let* ((other-win (windmove-find-other-window 'left))
(buf-this-buf (window-buffer (selected-window))))
(if (null other-win)
(error "No left split")
;; swap top with this one
(set-window-buffer (selected-window) (window-buffer other-win))
;; move this one to top
(set-window-buffer other-win buf-this-buf)
(select-window other-win))))
;;;###autoload
(defun buf-move-right ()
"Swap the current buffer and the buffer on the right of the split.
If there is no split, ie now window on the right of the current
one, an error is signaled."
(interactive)
(let* ((other-win (windmove-find-other-window 'right))
(buf-this-buf (window-buffer (selected-window))))
(if (null other-win)
(error "No right split")
;; swap top with this one
(set-window-buffer (selected-window) (window-buffer other-win))
;; move this one to top
(set-window-buffer other-win buf-this-buf)
(select-window other-win))))
#+end_src
* Vertico
[[https://github.com/minad/vertico][Vertico]] provides a performant and minimalistic vertical completion UI based on the default completion system.
#+begin_src emacs-lisp
;; Enable vertico
(use-package vertico
:ensure t
:custom
;; (vertico-scroll-margin 0) ;; Different scroll margin
;; (vertico-count 20) ;; Show more candidates
;; (vertico-resize t) ;; Grow and shrink the Vertico minibuffer
;; (vertico-cycle t) ;; Enable cycling for `vertico-next/previous'
:init
(vertico-mode))
(vertico-mode t) ;; enable vertico for all buffers
;; Persist history over Emacs restarts. Vertico sorts by history position.
(use-package savehist
:ensure -1 ;;package is unavailable in nix
:init
(savehist-mode))
;; A few more useful configurations...
(use-package emacs
:custom
;; Support opening new minibuffers from inside existing minibuffers.
(enable-recursive-minibuffers t)
;; Hide commands in M-x which do not work in the current mode. Vertico
;; commands are hidden in normal buffers. This setting is useful beyond
;; Vertico.
(read-extended-command-predicate #'command-completion-default-include-p)
:init
;; Add prompt indicator to `completing-read-multiple'.
;; We display [CRM<separator>], e.g., [CRM,] if the separator is a comma.
(defun crm-indicator (args)
(cons (format "[CRM%s] %s"
(replace-regexp-in-string
"\\`\\[.*?]\\*\\|\\[.*?]\\*\\'" ""
crm-separator)
(car args))
(cdr args)))
(advice-add #'completing-read-multiple :filter-args #'crm-indicator)
;; Do not allow the cursor in the minibuffer prompt
(setq minibuffer-prompt-properties
'(read-only t cursor-intangible t face minibuffer-prompt))
(add-hook 'minibuffer-setup-hook #'cursor-intangible-mode))
;; Optionally use the `orderless' completion style.
(use-package orderless
:ensure t
:custom
;; Configure a custom style dispatcher (see the Consult wiki)
;; (orderless-style-dispatchers '(+orderless-consult-dispatch orderless-affix-dispatch))
;; (orderless-component-separator #'orderless-escapable-split-on-space)
(completion-styles '(orderless basic))
(completion-category-defaults nil)
(completion-category-overrides '((file (styles partial-completion)))))
#+end_src
** Descriptions
*** Marginalia
[[https://github.com/minad/marginalia/]]
Descriptions for completions
#+begin_src emacs-lisp
;; Enable rich annotations using the Marginalia package
(use-package marginalia
:ensure t
;; Bind `marginalia-cycle' locally in the minibuffer. To make the binding
;; available in the *Completions* buffer, add it to the
;; `completion-list-mode-map'.
:bind (:map minibuffer-local-map
("M-A" . marginalia-cycle))
;; The :init section is always executed.
:init
;; Marginalia must be activated in the :init section of use-package such that
;; the mode gets enabled right away. Note that this forces loading the
;; package.
(marginalia-mode))
#+end_src
* Theme
[[https://emacsfodder.github.io/emacs-theme-editor/][Emacs Theme Editor]]
** Theme loading
#+begin_src emacs-lisp
(use-package doom-themes
:ensure t
:config
;; Global settings (defaults)
(setq doom-themes-enable-bold t ; if nil, bold is universally disabled
doom-themes-enable-italic t) ; if nil, italics is universally disabled
(load-theme 'doom-one t)
;; Enable flashing mode-line on errors
(doom-themes-visual-bell-config)
;; Enable custom neotree theme (nerd-icons must be installed!)
(doom-themes-neotree-config)
;; or for treemacs users
(setq doom-themes-treemacs-theme "doom-atom") ; use "doom-colors" for less minimal icon theme
(doom-themes-treemacs-config)
;; Corrects (and improves) org-mode's native fontification.
(doom-themes-org-config))
#+end_src
* WHICH-KEY
#+begin_src emacs-lisp
(use-package which-key

View file

@ -69,6 +69,7 @@
jetbrains-mono
ubuntu-classic
nerd-fonts.jetbrains-mono
nerd-fonts.symbols-only
];
};
mkEmacs = emacs: