To install any package, this config will be using use-package
. So
before proceeding ahead with anything, install use-package
. Check this tutorial.
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
(eval-and-compile
(setq use-package-always-ensure t
use-package-expand-minimally t))
Setting use-package-always-ensure
to t
saves us the trouble of
having to specify :ensure t
in any future packages we’d like to
declare and install. The :ensure
macro basically makes sure that the
packages are correctly installed at every startup, and automatically
installs the missing ones for you.
Initialize theme at start, so that any other visual changes won’t be overridden by default theme changes.
Function to disable all applied themes.
(defun cc/disable-all-themes ()
"Disable all active themes."
(interactive)
(dolist (i custom-enabled-themes)
(disable-theme i)))
Add advice to load-theme
to disable all themes before loading new theme.
(defadvice load-theme (before disable-themes-first activate)
(cc/disable-all-themes))
(use-package solarized-theme
:config
(load-theme 'solarized-dark t))
(use-package org)
Show ▾
instead of ...
after a heading in org mode.
(setq org-ellipsis " ▾")
Display available key bindings in pop-up
(unless (package-installed-p 'which-key)
(package-refresh-contents)
(package-install 'which-key))
;; Enable which-keys
(which-key-mode)
(use-package evil
:config
(evil-mode 1))
Highlight all occurrences of word under cursor. Package enabled in
programming and text mode only. idle-highlight-idle-time
sets time
taken to highlight the word.
(use-package idle-highlight-mode
:config
(setq idle-highlight-idle-time 1.0)
:hook ((prog-mode) . idle-highlight-mode))
Set the face of the highlighted word. Use :weight bold
to make it bold.
(custom-set-faces
'(idle-highlight ((t (:background "yellow" :foreground "black")))))
Display bullets instead of stars in org mode.
(use-package org-bullets
:config
(add-hook 'org-mode-hook (lambda () (org-bullets-mode)))
:custom
(org-bullets-bullet-list '("◉" "○" "●" "○" "●" "○" "●")))
(add-hook 'shell-mode-hook
(lambda ()
(face-remap-set-base 'comint-highlight-prompt :inherit
nil)))
;; Set indentatoin for bullets.
(setq-default org-list-indent-offset 4)
This package highlights cursor for very small time when switching buffers.
(use-package beacon
:config
(beacon-mode 1))
Use git in emacs in style.
(use-package magit)
Navigating C database easier.
(use-package xcscope
:config
;; Enable cscope minor mode in all major modes.
(cscope-minor-mode 1)
(cscope-setup)
;; Add cscope databases using this variable.
(setq cscope-set-initial-directory '("~/.cstags_dir/nomo/"))
;; Unset the default binding C-\
(define-key global-map "\C-\\" nil)
;; Set easy key bindings to navigate
(define-key global-map "\C-\\S" 'cscope-tell-user-about-directory)
(define-key global-map "\C-\\a" 'cscope-set-initial-directory)
(define-key global-map "\C-\\i" 'cscope-find-files-including-file)
(define-key global-map "\C-\\c" 'cscope-find-functions-calling-this-function)
(define-key global-map "\C-\\d" 'cscope-find-called-functions)
(define-key global-map "\C-\\e" 'cscope-find-egrep-pattern)
(define-key global-map "\C-\\t" 'cscope-find-this-text-string)
(define-key global-map "\C-\\s" 'cscope-find-this-symbol)
(define-key global-map "\C-\\f" 'cscope-find-this-file)
(define-key global-map "\C-\\g" 'cscope-find-global-definition)
(define-key global-map "\C-\\G" 'cscope-find-global-definition-no-prompting)
(define-key global-map "\C-\\u" 'cscope-pop-mark))
(use-package helm)
(setq helm-split-window-in-side-p t
helm-move-to-line-cycle-in-source t)
(custom-set-variables '(helm-completion-style (quote emacs)))
;; Enable helm mode
(helm-mode 1)
;; List and manage buffers.
;; Select multiple buffers with C-Space and M-D to kill all buffers.
(global-set-key (kbd "C-x C-b") 'helm-buffers-list)
(define-key helm-map (kbd "TAB") 'helm-execute-persistent-action)
;; Bookmark menu.
(global-set-key (kbd "C-x r b") 'helm-bookmark)
;; Helm resizes according to the number of candidates
(helm-autoresize-mode 1)
;; Finding files with Helm
(global-set-key (kbd "C-x C-f") 'helm-find-files)
;; Use Helm for calculations
(global-set-key (kbd "M-c") 'helm-calcul-expression)
;; Replaces the default isearch keybinding
(global-set-key (kbd "C-s") 'helm-occur)
;; Helmized apropos interface
(global-set-key (kbd "C-h a") 'helm-apropos)
;; Improved M-x menu
(global-set-key (kbd "M-x") 'helm-M-x)
;; Show kill ring, pick something to paste
(global-set-key (kbd "M-y") 'helm-show-kill-ring)
Emacs does not have support for markdown by default. 🤦
(use-package markdown-mode
:ensure t)
Set font size. The value is in 1/10pt, so 130 will give 13pt, etc.
(set-face-attribute 'default nil :height 130)
;; To set font and size, use:
;; (set-default-font "Monaco 14")
Always wrap text (Move to next line when window is over).
(setq visual-line-mode 1)
Set cursor color
(custom-set-faces
'(cursor ((t (:background "brown")))))
Hide toolbar, menubar and scrollbar.
(tool-bar-mode -1)
(menu-bar-mode -1)
(scroll-bar-mode -1)
Display line numbers
(global-display-line-numbers-mode t)
Make line numbers relative
(setq display-line-numbers-type 'relative)
Change message of scratch buffer.
To start the scratch buffer in text mode, modify initial-major-mode
variable.
(setq initial-scratch-message "**** Hello World!!! ****\n\n")
(setq initial-major-mode 'text-mode)
Follow window after splitting horizontally or vertically. Default behavior is to just split window and remain in same window.
(defun split-and-follow-horizontally()
(interactive)
(split-window-below)
(balance-windows)
(other-window 1))
(global-set-key (kbd "C-x 2") 'split-and-follow-horizontally)
(defun split-and-follow-vertically()
(interactive)
(split-window-right)
(balance-windows)
(other-window 1))
(global-set-key (kbd "C-x 3") 'split-and-follow-vertically)
Always show matching bracket.
(show-paren-mode 1)
;; Highlight brackets (default option).
(setq show-paren-style 'parenthesis)
;; By default, there is a delay in showing matching parenthesis.
(setq show-paren-delay 0)
Show line and column number on bottom bar.
(line-number-mode 1)
(column-number-mode 1)
Set transparency when focused and unfocused.
(set-frame-parameter (selected-frame) 'alpha '(100 92))
Truncate lines to next line.
(setq truncate-lines nil)
Enable word wrap
(setq word-wrap t)
Auto-fill for text mode (Basically, insert new line after defined
characters {set by set-fill-coloumn
} in the given line.)
(add-hook 'text-mode-hook 'turn-on-auto-fill)
(setq set-fill-coloumn 70)
Change yes/no question to y/n.
(defalias 'yes-or-no-p 'y-or-n-p)
Enable spell check. This will enable spell check in text modes and only for comments in programming mode.
(add-hook 'text-mode-hook 'flyspell-mode)
(add-hook 'prog-mode-hook 'flyspell-prog-mode)
ESC to be used as keyboard-quit (C-g)
(global-set-key (kbd "<escape>") 'keyboard-escape-quit)
Global line highlight mode keybinding.
(global-set-key (kbd "C-c l h") 'global-hl-line-mode)
Set scroll margin. Vim’s emulation of scrolloff or so. Set it to 0 for terminal Set it to 0 for terminal.
(setq scroll-margin 3)
(add-hook 'term-mode-hook
(lambda ()
(make-local-variable 'scroll-margin)
(setq scroll-margin 0)))
No welcome screen on startup
(setq inhibit-startup-screen t)
Scroll line by line when reaching end of screen.
(setq scroll-conservatively 100)
When using GUI version, convert symbol text into symbols.
(when window-system (global-prettify-symbols-mode t))
Do not create backup and autosave files.
(setq make-backup-files nil)
(setq auto-save-default nil)
Show function name in mode-line for C functions
(add-hook 'c-mode-common-hook
(lambda ()
(which-function-mode t)))
Display current file name in title bar.
(setq frame-title-format
(list (format "%s %%S: %%j " (system-name))
'(buffer-file-name "%f" (dired-directory dired-directory "%b"))))
Treat _
as part of the word.
(modify-syntax-entry ?_ "w")
Set tab length to 4 spaces. In cc-mode
based modes for C-like
languages, something like this should do what you want:
;; Use tabs instead of spaces.
(setq-default indent-tabs-mode t)
;; Visualize as 4 spaces when used a tab.
(setq-default tab-width 4)
;; In C like languages, use ~tab-width~ value to represent tab.
(defvaralias 'c-basic-offset 'tab-width)
Scroll one line up and down by M-n
and M-p
.
(global-set-key (kbd "M-n") (kbd "C-u 1 C-v"))
(global-set-key (kbd "M-p") (kbd "C-u 1 M-v"))
List recently opened files in emacs.
(require 'recentf)
(recentf-mode 1)
(setq recentf-max-menu-items 25)
;; Bind to `C-c f r'.
(global-set-key "\C-c\ f\ r" 'recentf-open-files)
Toggle split view. Switch from vertical to horizontal split and
vice-versa with same binding. Same as C-w L
and C-w J
in vi
.
(defun toggle-window-split ()
(interactive)
(if (= (count-windows) 2)
(let* ((this-win-buffer (window-buffer))
(next-win-buffer (window-buffer (next-window)))
(this-win-edges (window-edges (selected-window)))
(next-win-edges (window-edges (next-window)))
(this-win-2nd (not (and (<= (car this-win-edges)
(car next-win-edges))
(<= (cadr this-win-edges)
(cadr next-win-edges)))))
(splitter
(if (= (car this-win-edges)
(car (window-edges (next-window))))
'split-window-horizontally
'split-window-vertically)))
(delete-other-windows)
(let ((first-win (selected-window)))
(funcall splitter)
(if this-win-2nd (other-window 1))
(set-window-buffer (selected-window) this-win-buffer)
(set-window-buffer (next-window) next-win-buffer)
(select-window first-win)
(if this-win-2nd (other-window 1))))))
(global-set-key (kbd "C-x |") 'toggle-window-split)
Copy file path to clipboard.
(defun copy-file-name-to-clipboard ()
"Put the current file name on the clipboard"
(interactive)
(let ((filename (if (equal major-mode 'dired-mode)
default-directory
(buffer-file-name))))
(when filename
(with-temp-buffer
(insert filename)
(clipboard-kill-region (point-min) (point-max)))
(message "%s copied to clipboard." filename ))))
(global-set-key (kbd "C-c f c") 'copy-file-name-to-clipboard)
Toggle trailing whitespaces
(defun cc/toggle-trailing-whitespace()
"This function toggles display of trailing whitespaces."
(interactive)
(setq show-trailing-whitespace (not show-trailing-whitespace))
(message "show-trailing-whitespace is set to %s"
show-trailing-whitespace))
(global-set-key (kbd "C-c l w t") 'cc/toggle-trailing-whitespace)
Clear all whitespaces.
(global-set-key (kbd "C-c l w d") 'whitespace-cleanup)
In Org mode, insert emacs lisp snippet with the keybinding C-c C-,
followed by el
.
(add-to-list 'org-structure-template-alist '("el" . "src emacs-lisp"))
Switch line numbers between relative and absolute.
(defun cc/toggle-line-numbering ()
"Switch line numbering between absolute and relative."
(interactive)
(if (eq display-line-numbers 'relative)
(setq display-line-numbers t)
(setq display-line-numbers 'relative)))
;; Lisp Hacking: Same functionality can be achieved with this snippet also.
;; (setq display-line-numbers (if (eq display-line-numbers 'relative) t 'relative))
;; Set key binding to toggle line number mode.
(global-set-key (kbd "C-c a") 'cc/toggle-line-numbering)
Enable ibuffer
(global-set-key (kbd "C-x b") 'ibuffer)
Edit config.org
(defun config-visit()
(interactive)
(find-file "~/.emacs.d/config.org"))
(global-set-key (kbd "C-c e") 'config-visit)
Reload config.org
(defun config-reload()
(interactive)
(org-babel-load-file (expand-file-name "~/.emacs.d/config.org")))
(global-set-key (kbd "C-c r") 'config-reload)
Kill buffer and close split
(defun cc/kill-buf-and-close-split()
"Kill current buffer and delete the window."
(interactive)
(kill-buffer)
(delete-window))
(global-set-key (kbd "C-x K") 'cc/kill-buf-and-close-split)
Setting default shell to bash. Due to this, ansi-term will not ask which shell to use.
;; `ansi-term' will launch bash without prompt.
(defvar my-term-shell "/bin/bash")
(defadvice ansi-term (before force-bash)
(interactive (list my-term-shell)))
(ad-activate 'ansi-term)
Keybinding to open ansi-term in a new split.
(defun cc/split-ansi-term()
"Start a ansi-term in a new split."
(interactive)
(split-window-sensibly)
(other-window 1)
(ansi-term "bash"))
(global-set-key "\C-c\ t" 'cc/split-ansi-term)
Quit ansi-term without confirming to kill running process.
(defun set-no-process-query-on-exit ()
(let ((proc (get-buffer-process (current-buffer))))
(when (processp proc)
(set-process-query-on-exit-flag proc nil))))
(add-hook 'term-exec-hook 'set-no-process-query-on-exit)
View markdown preview as you edit a markdown file. Source.
(defun cam/-scroll-percentage ()
(/ (float (line-number-at-pos (window-start)))
(float (line-number-at-pos (point-max)))))
(defun cam/-set-window-start-to-percentage (scroll-percentage)
(goto-char (point-min))
(let ((target-line-number (truncate (* (line-number-at-pos (point-max)) scroll-percentage))))
(forward-line (1- target-line-number)))
(set-window-start nil (point)))
(defun cam/-render-markdown-preview-current-buffer ()
(message "Rendering Markdown preview of %s" buffer-file-name)
(shell-command-on-region (point-min) (point-max) "pandoc -f gfm" "*Preview Markdown Output*")
(switch-to-buffer-other-window "*Preview Markdown Output*")
(let ((document (libxml-parse-html-region (point) (point-max))))
(erase-buffer)
(shr-insert-document `(base ((href . ,url)) ,document))
(setq buffer-read-only t)))
(defun cam/-preview-markdown-file (filename)
(save-selected-window
(find-file filename)
(let ((url (concat "file://" filename))
(scroll-percentage (cam/-scroll-percentage)))
(cam/-render-markdown-preview-current-buffer)
(cam/-set-window-start-to-percentage scroll-percentage))))
(defun cam/preview-markdown (&optional filename)
"Render a markdown preview of FILENAME (by default, the current file) to HTML and display it with `shr-insert-document'."
(interactive "fFile: ")
(if filename
(progn
(cam/-preview-markdown-file filename)
(switch-to-buffer (current-buffer)))
(cam/-preview-markdown-file buffer-file-name)))