Skip to content

Instantly share code, notes, and snippets.

@gtello
Created August 24, 2023 15:39
Show Gist options
  • Save gtello/57199c37b950adcaebc15db181dc92d9 to your computer and use it in GitHub Desktop.
Save gtello/57199c37b950adcaebc15db181dc92d9 to your computer and use it in GitHub Desktop.
Emacs config

Emacs Configuration & Packages

General

Sensible defaults

Keybindings, startup options, interface tweaks, etc. Works along the sensible-defaults.el file.

(load-file "~/.emacs.d/sensible-defaults.el")
(sensible-defaults/use-all-settings)
(sensible-defaults/use-all-keybindings)
(sensible-defaults/backup-to-temp-directory)

(setq package-check-signature nil)  ; Livin' dangerously :^)

(add-to-list 'default-frame-alist '(fullscreen . maximized))

(global-set-key (kbd "<f5>") 'revert-buffer)

(setq save-interprogram-paste-before-kill t)  ; Keeps the clipboard contents of a killed buffer.

(setq split-height-threshold nil)  ; Vertical instead of horizontal
(setq split-width-threshold 0)

Spellcheck

Uses aspell dictionary. Tutorial on how to configure aspell. M-x ispell-change-dictionary | M-x flyspell-mode

(setq-default ispell-program-name "aspell")
(setq ispell-dictionary "spanish")

Try

To test packages without having to install them. M-x try

(use-package try
  :ensure t)

Which key

Displays the key bindings following a currently entered incomplete command.

(use-package which-key
  :ensure t
  :config (which-key-mode))

IBuffer

Buffer that lets you manipulate other buffers. C-x C-b

(defalias 'list-buffers 'ibuffer)
;; Or to show buffers in another window: 'ibuffer-other-window

Swiper, Counsel, Ivy

Swiper is a better search. Counsel for completion powered by Ivy.

(use-package counsel
  :ensure t
  :bind
  (("M-y" . counsel-yank-pop)
   :map ivy-minibuffer-map
   ("M-y" . ivy-next-line)))

(use-package swiper
  :ensure t
  :config
  (progn
    (ivy-mode 1)
    (setq ivy-use-virtual-buffers 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-load-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 k") 'counsel-ag)
    (global-set-key (kbd "C-x l") 'counsel-locate)))

Beacon mode

Flashes the cursor whenever you scroll, for visibility’s sake.

(use-package beacon
:ensure t
:config
(beacon-mode 1)
(setq beacon-color "#666600"))

Undo tree

Better, visual undo. Also adds redo, simply by adding shift to the undo. C-x u to use the visual undo tree. Move with arrow keys and exit with q.

(use-package undo-tree
  :ensure t
  :init
    (global-undo-tree-mode))

Global auto-complete

(use-package auto-complete
  :ensure t
  :init
  (progn
    (ac-config-default)
    (global-auto-complete-mode t)))

Aggressive indent

Makes sure that code you write stays indented.

(use-package aggressive-indent
:ensure t
:config
(global-aggressive-indent-mode 1))

Expand region

Expands selection in semantic increments. C-= start function, then keep using = to select, - to contract and 0 to end the function.

(use-package expand-region
:ensure t
:config
(global-set-key (kbd "C-=") 'er/expand-region))

Narrowing and iedit

Select text, then narrows it then widens it with C-x n. Use iedit to refactor all words in a buffer with C-;

(use-package iedit
  :ensure t)

(defun narrow-or-widen-dwim (p)
  "Widen if buffer is narrowed, narrow-dwim otherwise.
Dwim means: region, org-src-block, org-subtree, or
defun, whichever applies first. Narrowing to
org-src-block actually calls `org-edit-src-code'.

With prefix P, don't widen, just narrow even if buffer
is already narrowed."
  (interactive "P")
  (declare (interactive-only))
  (cond ((and (buffer-narrowed-p) (not p)) (widen))
        ((region-active-p)
         (narrow-to-region (region-beginning)
                           (region-end)))
        ((derived-mode-p 'org-mode)
         ;; `org-edit-src-code' is not a real narrowing
         ;; command. Remove this first conditional if
         ;; you don't want it.
         (cond ((ignore-errors (org-edit-src-code) t)
                (delete-other-windows))
               ((ignore-errors (org-narrow-to-block) t))
               (t (org-narrow-to-subtree))))
        ((derived-mode-p 'latex-mode)
         (LaTeX-narrow-to-environment))
        (t (narrow-to-defun))))

(define-key ctl-x-map "n" #'narrow-or-widen-dwim)
(add-hook 'LaTeX-mode-hook
          (lambda ()
            (define-key LaTeX-mode-map "\C-xn"
              nil)))

Python

C-c C-p for running virtual python environment, then to run the code C-c C-c

Flycheck

On-the-fly syntax checking package.

(use-package flycheck
  :ensure t
  :init
  (global-flycheck-mode t))

Jedi

Provides more extensive completion for Python.

(use-package jedi
  :ensure t
  :init
  (add-hook 'python-mode-hook 'jedi:setup)
  (add-hook 'python-mode-hook 'jedi:ac-setup))

Org-mode

Templates

Block of elisp. <el TAB

(add-to-list 'org-structure-template-alist
             '("el" "#+BEGIN_SRC emacs-lisp\n?\n#+END_SRC"))

Org-mode syntax highlighting

(setq org-src-fontify-natively t
    org-src-tab-acts-natively t
    org-confirm-babel-evaluate nil
    org-edit-src-content-indentation 0)

Org-mode auto-complete

(use-package org-ac
  :ensure t
  :config (org-ac/config-default))

Org bullets

Set ellipsis in mashed in there too, cuz why not.

(use-package org-bullets
  :ensure t
  :config
  (add-hook 'org-mode-hook (lambda () (org-bullets-mode 1))))

(setq org-ellipsis "")

Reveal.js

Framekwork for creating presentations in org-mode. Github page.

;(use-package ox-reveal
;:ensure ox-reveal)

;(setq org-reveal-root "https://cdn.jsdelivr.net/npm/reveal.js@3.6.0")
;(setq org-reveal-mathjax t)

(use-package htmlize
:ensure t)

Windows differences

Best search tool for Windows. M-x everything

(add-to-list 'load-path (expand-file-name "C:/Users/G t z/AppData/Roaming/.emacs.d/everything")) ;; Folder directory where 'everything.el' is
(setq everything-cmd (expand-file-name "F:/Program Files/Everything/es.exe")) ;; let 'everything.el' know where to find 'es.exe'
(require 'everything)
(package-initialize)
(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.
'(ansi-color-faces-vector
[default default default italic underline success warning error])
'(ansi-color-names-vector
["#212526" "#ff4b4b" "#b4fa70" "#fce94f" "#729fcf" "#e090d7" "#8cc4ff" "#eeeeec"])
'(custom-enabled-themes (quote (deeper-blue)))
'(custom-safe-themes
(quote
("bffa9739ce0752a37d9b1eee78fc00ba159748f50dc328af4be661484848e476" default)))
'(inhibit-startup-screen t)
'(menu-bar-mode nil)
'(package-archives
(quote
(("gnu" . "https://elpa.gnu.org/packages/")
("melpa" . "https://melpa.org/packages/")
("melpa stable" . "https://stable.melpa.org/packages/"))))
'(package-selected-packages
(quote
(expand-region aggressive-indent undo-tree beacon htmlize ox-reveal counsel swiper which-key try use-package org-ac pdf-tools spacemacs-theme color-theme-sanityinc-tomorrow night-owl-theme org)))
'(tool-bar-mode nil))
;("org" . "https://orgmode.org/elpa/"
;(unless (file-expand-wildcards (concat package-user-dir "/org-[0-9]*"))
; (package-install (elt (cdr (assoc 'org package-archive-contents)) 0)))
;(require 'org)
(add-to-list 'load-path "~/emacs.d/")
;; Tabs for switching a buffer. C-x b | Using swiper so no longer needed.
;;(setq ido-enable-flex-matching t)
;;(setq ido-everywhere t)
;;(ido-mode 1)
(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.
'(default ((t (:inherit nil :stipple nil :background "#181a26" :foreground "gray80" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 160 :width normal :foundry "unknown" :family "Inconsolata"))))
'(bold ((t (:weight semi-bold)))))
;; Function to verify installed packages
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
(org-babel-load-file (expand-file-name "~/.emacs.d/config.org"))
;; Utility functions:
(defun sensible-defaults/comment-or-uncomment-region-or-line ()
"Comments or uncomments the region or the current line if
there's no active region."
(interactive)
(let (beg end)
(if (region-active-p)
(setq beg (region-beginning) end (region-end))
(setq beg (line-beginning-position) end (line-end-position)))
(comment-or-uncomment-region beg end)))
(defun sensible-defaults/reset-text-size ()
(interactive)
(text-scale-set 0))
;; Settings:
(defun sensible-defaults/open-files-from-home-directory ()
"When opening a file, start searching at the user's home
directory."
(setq default-directory "~/"))
(defun sensible-defaults/increase-gc-threshold ()
"Allow 20MB of memory (instead of 0.76MB) before calling
garbage collection. This means GC runs less often, which speeds
up some operations."
(setq gc-cons-threshold 20000000))
(defun sensible-defaults/delete-trailing-whitespace ()
"Call DELETE-TRAILING-WHITESPACE every time a buffer is saved."
(add-hook 'before-save-hook 'delete-trailing-whitespace))
(defun sensible-defaults/treat-camelcase-as-separate-words ()
"Treat CamelCaseSubWords as separate words in every programming
mode."
(add-hook 'prog-mode-hook 'subword-mode))
(defun sensible-defaults/automatically-follow-symlinks ()
"When opening a file, always follow symlinks."
(setq vc-follow-symlinks t))
(defun sensible-defaults/make-scripts-executable ()
"When saving a file that starts with `#!', make it executable."
(add-hook 'after-save-hook
'executable-make-buffer-file-executable-if-script-p))
(defun sensible-defaults/single-space-after-periods ()
"Don't assume that sentences should have two spaces after
periods. This ain't a typewriter."
(setq sentence-end-double-space nil))
(defun sensible-defaults/offer-to-create-parent-directories-on-save ()
"When saving a file in a directory that doesn't exist, offer
to (recursively) create the file's parent directories."
(add-hook 'before-save-hook
(lambda ()
(when buffer-file-name
(let ((dir (file-name-directory buffer-file-name)))
(when (and (not (file-exists-p dir))
(y-or-n-p (format "Directory %s does not exist. Create it?" dir)))
(make-directory dir t)))))))
(defun sensible-defaults/apply-changes-to-highlighted-region ()
"Turn on transient-mark-mode."
(transient-mark-mode t))
(defun sensible-defaults/overwrite-selected-text ()
"If some text is selected, and you type some text, delete the
selected text and start inserting your typed text."
(delete-selection-mode t))
(defun sensible-defaults/ensure-that-files-end-with-newline ()
"If you save a file that doesn't end with a newline,
automatically append one."
(setq require-final-newline t))
(defun sensible-defaults/confirm-closing-emacs ()
"Ask if you're sure that you want to close Emacs."
(setq confirm-kill-emacs 'y-or-n-p))
(defun sensible-defaults/quiet-startup ()
"Don't present the usual startup message, and clear the scratch
buffer."
(setq inhibit-startup-message t)
(setq initial-scratch-message nil))
(defun sensible-defaults/make-dired-file-sizes-human-readable ()
"Add file sizes in human-readable units (KB, MB, etc) to dired
buffers."
(setq-default dired-listing-switches "-alh"))
(defun sensible-defaults/shorten-yes-or-no ()
"Don't ask `yes/no?', ask `y/n?'."
(fset 'yes-or-no-p 'y-or-n-p))
(defun sensible-defaults/always-highlight-code ()
"Turn on syntax highlighting whenever possible."
(global-font-lock-mode t))
(defun sensible-defaults/refresh-buffers-when-files-change ()
"When something changes a file, automatically refresh the
buffer containing that file so they can't get out of sync."
(global-auto-revert-mode t))
(defun sensible-defaults/show-matching-parens ()
"Visually indicate matching pairs of parentheses."
(show-paren-mode t)
(setq show-paren-delay 0.0))
(defun sensible-defaults/flash-screen-instead-of-ringing-bell ()
"When you perform a problematic operation, flash the screen
instead of ringing the terminal bell."
(setq visible-bell t))
(defun sensible-defaults/set-default-line-length-to (line-length)
"Set the default line length to LINE-LENGTH."
(setq-default fill-column line-length))
(defun sensible-defaults/open-clicked-files-in-same-frame-on-mac ()
"When you double-click on a file in the Mac Finder open it as a
buffer in the existing Emacs frame, rather than creating a new
frame just for that file."
(setq ns-pop-up-frames nil))
(defun sensible-defaults/yank-to-point-on-mouse-click ()
"When middle-clicking the mouse to yank from the clipboard,
insert the text where point is, not where the mouse cursor is."
(setq mouse-yank-at-point t))
(defun sensible-defaults/use-all-settings ()
"Use all of the sensible-defaults settings."
(sensible-defaults/open-files-from-home-directory)
(sensible-defaults/increase-gc-threshold)
(sensible-defaults/delete-trailing-whitespace)
(sensible-defaults/treat-camelcase-as-separate-words)
(sensible-defaults/automatically-follow-symlinks)
(sensible-defaults/make-scripts-executable)
(sensible-defaults/single-space-after-periods)
(sensible-defaults/offer-to-create-parent-directories-on-save)
(sensible-defaults/apply-changes-to-highlighted-region)
(sensible-defaults/overwrite-selected-text)
(sensible-defaults/ensure-that-files-end-with-newline)
(sensible-defaults/confirm-closing-emacs)
(sensible-defaults/quiet-startup)
(sensible-defaults/make-dired-file-sizes-human-readable)
(sensible-defaults/shorten-yes-or-no)
(sensible-defaults/always-highlight-code)
(sensible-defaults/refresh-buffers-when-files-change)
(sensible-defaults/show-matching-parens)
(sensible-defaults/flash-screen-instead-of-ringing-bell)
(sensible-defaults/set-default-line-length-to 80)
(sensible-defaults/open-clicked-files-in-same-frame-on-mac)
(sensible-defaults/yank-to-point-on-mouse-click))
;; Keybindings:
(defun sensible-defaults/bind-commenting-and-uncommenting ()
"Comment or uncomment a region by hitting M-;."
(global-set-key (kbd "M-;")
'sensible-defaults/comment-or-uncomment-region-or-line))
(defun sensible-defaults/bind-home-and-end-keys ()
"Make <home> and <end> move point to the beginning and end of
the line, respectively."
(global-set-key (kbd "<home>") 'move-beginning-of-line)
(global-set-key (kbd "<end>") 'move-end-of-line))
(defun sensible-defaults/bind-keys-to-change-text-size ()
"Bind C-+ and C-- to increase and decrease text size,
respectively."
(define-key global-map (kbd "C-)") 'sensible-defaults/reset-text-size)
(define-key global-map (kbd "C-+") 'text-scale-increase)
(define-key global-map (kbd "C-=") 'text-scale-increase)
(define-key global-map (kbd "C-_") 'text-scale-decrease)
(define-key global-map (kbd "C--") 'text-scale-decrease))
(defun sensible-defaults/use-all-keybindings ()
"Use all of the sensible-defaults keybindings."
(sensible-defaults/bind-commenting-and-uncommenting)
(sensible-defaults/bind-home-and-end-keys)
(sensible-defaults/bind-keys-to-change-text-size))
;; Non-default settings:
(defun sensible-defaults/backup-to-temp-directory ()
"Store backups and auto-saved files in
TEMPORARY-FILE-DIRECTORY (which defaults to /tmp on Unix),
instead of in the same directory as the file. This means we're
still making backups, but not where they'll get in the way.
WARNING: on most Unix-like systems /tmp is volatile, in-memory
storage, so your backups won't survive if your computer crashes!
If you're not willing to take this risk, you shouldn't enable
this setting."
(setq backup-directory-alist
`((".*" . ,temporary-file-directory)))
(setq auto-save-file-name-transforms
`((".*" ,temporary-file-directory t))))
;;; sensible-defaults.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment