Verified Commit a3c6b8b3 authored by peacerebel's avatar peacerebel
Browse files

Add emacs config

parents
;;; config.el --- Emacs configurations
;;; Code:
;; Configs
(require 'use-package-ensure)
(setq use-package-always-ensure t)
;; Load sensible defaults
;; https://github.com/hrs/sensible-defaults.el/blob/main/sensible-defaults.el
;; Thanks, HRS.
(load-file "~/.emacs.d/sensible-defaults.el")
(sensible-defaults/use-all-settings)
(sensible-defaults/use-all-keybindings)
(sensible-defaults/backup-to-temp-directory)
;; Set personal info
(setq user-full-name "Bipin"
user-mail-address "peacerebel@protonmail.com")
;; Disable toolbar, menu and scrollbar
(tool-bar-mode 0)
(menu-bar-mode 0)
(scroll-bar-mode -1)
;; Save location in file
(save-place-mode t)
;; Indent with space
(setq-default indent-tabs-mode nil
tab-width 2)
;; Disable scrollbar in minibuffer
(set-window-scroll-bars (minibuffer-window) nil nil)
;; Enable pretty symbols globally
(global-prettify-symbols-mode t)
;; Open emacs config
(defun pr/visit-emacs-config ()
"Opens file with Emacs configurations."
(interactive)
(find-file "~/.emacs.d/config.el"))
(global-set-key (kbd "C-c e") 'pr/visit-emacs-config)
;; Utils
(defun pr/append-to-path (path)
"Add a path both to the $PATH variable and to Emacs' `exec-path'."
(setenv "PATH" (concat (getenv "PATH") ":" path))
(add-to-list 'exec-path path))
(defun pr/add-auto-mode (mode &rest patterns)
"Add entries to `auto-mode-alist' to use `MODE' for all given file `PATTERNS'."
(dolist (pattern patterns)
(add-to-list 'auto-mode-alist (cons pattern mode))))
;; Move around open windows
(global-set-key (kbd "M-o") 'other-window)
(use-package windmove
:config
(global-set-key (kbd "s-[") 'windmove-left) ;; Super + [ go to left window
(global-set-key (kbd "s-]") 'windmove-right) ;; Super + ] go to right window
(global-set-key (kbd "s-{") 'windmove-up) ;; Super + Shift + { go to upper window
(global-set-key (kbd "s-}") 'windmove-down)) ;; Super + Shift + } go to down window
;; Move between buffers
(global-set-key (kbd "s-<") 'previous-buffer)
(global-set-key (kbd "s->") 'next-buffer)
;; Show line number
(require 'display-line-numbers)
(defcustom display-line-numbers-exempt-modes '(vterm-mode eshell-mode shell-mode term-mode ansi-term-mode)
"Major modes on which to disable the linum mode, exempts them from global requirement."
:group 'display-line-numbers
:type 'list
:version "green")
(defun display-line-numbers--turn-on ()
"Turn on line numbers but excempting certain majore modes defined in `display-line-numbers-exempt-modes'."
(if (and
(not (member major-mode display-line-numbers-exempt-modes))
(not (minibufferp)))
(display-line-numbers-mode)))
(global-display-line-numbers-mode)
;; Keystroke suggestion
(use-package which-key
:config
(setq which-key-popup-type 'minibuffer)
(which-key-mode))
;; THEME
(use-package cyberpunk-theme)
;; Font settings
(defvar pr/default-font "DejaVu Sans Mono")
(defvar pr/default-font-size 7)
(defvar pr/current-font-size pr/default-font-size)
(defvar pr/font-change-increment 1.0)
(defun pr/font-code ()
"Return a string representing the current font (like \"Inconsolata-14\")."
(concat pr/default-font "-" (number-to-string pr/current-font-size)))
(defun pr/set-font-size ()
"Set the font to `pr/default-font' at `pr/current-font-size'.
Set that for the current frame, and also make it the default for
other, future frames."
(let ((font-code (pr/font-code)))
(if (assoc 'font default-frame-alist)
(setcdr (assoc 'font default-frame-alist) font-code)
(add-to-list 'default-frame-alist (cons 'font font-code)))
(set-frame-font font-code)))
(defun pr/reset-font-size ()
"Change font size back to `pr/default-font-size'."
(interactive)
(setq pr/current-font-size pr/default-font-size)
(pr/set-font-size))
(pr/reset-font-size)
;; Highlight current line
(global-hl-line-mode)
;; Disable visual/audio bell
(setq ring-bell-function 'ignore)
;; Scroll only as far as the buffer point
(setq scroll-conservatively 100)
;; Regular undo-redo.
(use-package undo-fu
:config
(global-unset-key (kbd "C-z"))
(global-set-key (kbd "C-z") 'undo-fu-only-undo)
(global-set-key (kbd "C-S-z") 'undo-fu-only-redo))
;; Upcase word and region using the same keys.
(global-set-key (kbd "M-u") 'upcase-dwim)
(global-set-key (kbd "M-l") 'downcase-dwim)
;; Setup windows to be in golden ratio
(use-package zoom
:config
(custom-set-variables
'(zoom-mode t)
'(zoom-size '(0.618 . 0.618))))
;; modline
(use-package doom-modeline
:ensure t
:config
(setq doom-modeline-height 20)
(setq doom-modeline-bar-width 3)
(setq doom-modeline-project-detection 'project)
(setq doom-modeline-buffer-file-name-style 'auto)
(setq doom-modeline-icon (display-graphic-p))
(setq doom-modeline-major-mode-icon t)
(setq doom-modeline-major-mode-color-icon t)
(setq doom-modeline-buffer-state-icon t)
(setq doom-modeline-buffer-modification-icon t)
(setq doom-modeline-minor-modes nil)
(setq doom-modeline-enable-word-count nil)
(setq doom-modeline-buffer-encoding t)
(setq doom-modeline-indent-info nil)
(setq doom-modeline-checker-simple-format t)
(setq doom-modeline-workspace-name t)
(setq doom-modeline-lsp t)
(setq doom-modeline-modal-icon t)
:init
(doom-modeline-mode 1))
;; minor mode
(use-package minions
:config
(minions-mode t))
;; Use deadgrep
(use-package deadgrep
:init
(global-set-key (kbd "<f5>") #'deadgrep))
;; Dumb-jump
(use-package dumb-jump
:config
(setq dumb-jump-force-searcher 'rg)
:init
(add-hook 'xref-backend-functions #'dumb-jump-xref-activate))
;; Company-mode
(use-package company
:config
(setq company-frontends
'(company-pseudo-tooltip-unless-just-one-frontend
company-preview-frontend
company-echo-metadata-frontend)
company-require-match 'never)
(add-hook 'after-init-hook 'global-company-mode)
(eval-after-load 'company
(lambda ()
(set-face-attribute
'company-preview
nil
:background (face-attribute 'company-preview-common :background)))))
;; Company documentation view
(use-package company-quickhelp
:bind
("M-h" . company-quickhelp-manual-begin)
:config
(setq
company-quickhelp-delay nil
company-quickhelp-color-foreground "grey"
company-quickhelp-color-background "black"
company-quickhelp-use-propertized-text t)
:init
(company-quickhelp-mode t))
;; Language servers
(use-package lsp-mode
:config
(setq gc-cons-threshold 100000000) ;; Why? Visit: https://emacs-lsp.github.io/lsp-mode/page/performance/
(setq lsp-completion-provider :capf)
:hook ((js-mode . lsp))
:commands lsp)
(use-package lsp-ui :commands lsp-ui-mode)
(use-package lsp-ivy :commands lsp-ivy-workspace-symbol)
;; Counsel
(use-package counsel
:bind
("M-x" . 'counsel-M-x)
("C-s" . 'swiper)
("C-x C-f" . 'counsel-find-file)
("<f1> f" . 'counsel-describe-function)
("<f1> v" . 'counsel-describe-variable)
("<f1> l" . 'counsel-find-library)
("<f1> i" . 'counsel-info-lookup-symbol)
("C-c k" . 'counsel-rg)
:config
(ivy-mode 1)
(setq
ivy-use-virtual-buffers t
ivy-count-format "(%d/%d) "
ivy-initial-inputs-alist nil
ivy-re-builders-alist '((swiper . ivy--regex-plus)
(t . ivy--regex-fuzzy)
(counsel-rg . ivy--regex-plus))))
;; Projectile
(use-package projectile
:ensure t
:bind
("s-p" . projectile-command-map)
:config
(setq projectile-require-project-root t)
(setq projectile-completion-system 'ivy)
:init
(projectile-mode t))
;; Fuzzy match
(use-package flx)
;; Better M-x
(use-package smex)
;; Electric pair
(electric-pair-mode t)
;; Engine mode - Editor to browser search
(use-package engine-mode
:init
(engine-mode t))
(defengine duckduckgo
"https://duckduckgo.com/?q=%s"
:keybinding "d")
;; Flycheck
(use-package flycheck
:ensure t
:init (global-flycheck-mode))
(use-package flycheck-color-mode-line
:hook
(flycheck-mode . flycheck-color-mode-line-mode))
;; Language specific setup
;; Lua
(use-package lua-mode
:init
(autoload 'lua-mode "lua-mode" "Lua editing mode." t)
(add-to-list 'auto-mode-alist '("\\.lua$" . lua-mode))
(add-to-list 'interpreter-mode-alist '("lua" . lua-mode)))
;; Rust
;; Typescript
(use-package typescript-mode
:init
(add-to-list 'auto-mode-alist '("\\.ts$" . typescript-mode)))
(use-package tide
:config
(setq company-tooltip-align-annotations t)
(add-hook 'before-save-hook 'tide-format-before-save)
(add-hook 'typescript-mode-hook #'setup-tide-mode)
:init
(defun setup-tide-mode ()
(interactive)
(tide-setup)
(flycheck-mode +1)
(setq flycheck-check-syntax-automatically '(save mode-enabled))
(eldoc-mode +1)
(tide-hl-identifier-mode +1)
(company-mode +1)))
;;; config.el ends here
;; Configure package.el to include MELPA.
(require 'package)
(add-to-list 'package-archives '("gnu" . "https://elpa.gnu.org/packages/"))
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(add-to-list 'package-archives '("org" . "https://orgmode.org/elpa/"))
(setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3")
(package-initialize)
;; Ensure that use-package is installed.
;;
;; If use-package isn't already installed, it's extremely likely that this is a
;; fresh installation! So we'll want to update the package repository and
;; install use-package before loading the literate configuration.
(when (not (package-installed-p 'use-package))
(package-refresh-contents)
(package-install 'use-package))
(load-file "~/.emacs.d/config.el")
;; Save custom setting in ~/.emacs.d/custom.el
(setq custom-file "~/.emacs.d/custom.el")
(load custom-file)
;;; init.el ends here
;;; sensible-defaults.el --- Reasonable settings for getting started.
;; Author: Harry R. Schwartz <hello@harryrschwartz.com>
;; Version: 1.0.0
;; URL: https://github.com/hrs/sensible-defaults.el/sensible-defaults.el
;; This file is NOT part of GNU Emacs.
;;; License:
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
;; 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
# My personal configuration files for application I use.
I use bare git repository to manage these files. (https://www.atlassian.com/git/tutorials/dotfiles)
Contains configs for:
1. Emacs
# Refs
1. sensible-defaults: https://github.com/hrs/sensible-defaults.el
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment