Skip to content

Latest commit

 

History

History
1178 lines (1059 loc) · 41 KB

init.org

File metadata and controls

1178 lines (1059 loc) · 41 KB

Emacs configuration file

Configuration

Meta

When this configuration is loaded for the first time, the init.el is the file that is loaded. It looks like this:

;; This file replaces itself with the actual configuration at first run.

;; We can't tangle without org!
(require 'org)
;; Open the configuration
(find-file (concat user-emacs-directory "init.org"))
;; tangle it
(org-babel-tangle)
;; load it
(load-file (concat user-emacs-directory "init.el"))
;; finally byte-compile it
(byte-compile-file (concat user-emacs-directory "init.el"))

It tangles the org-file, so that this file is overwritten with the actual configuration.

There is no reason to track the init.el that is generated; by running the following command git will not bother tracking it:

git update-index --assume-unchanged init.el

If one wishes to make changes to the repo-version of init.el start tracking again with:

git update-index --no-assume-unchanged init.el

I want lexical scoping for the init-file, which can be specified in the header. The first line of the configuration is as follows:

;;; -*- lexical-binding: t -*-

The init.el should (after the first run) mirror the source blocks in the init.org. We can use C-c C-v t to run org-babel-tangle, which extracts the code blocks from the current file into a source-specific file (in this case a .el-file).

To avoid doing this each time a change is made we can add a function to the after-save-hook ensuring to always tangle and byte-compile the org-document after changes.

   (defun tangle-init ()
     "If the current buffer is 'init.org' the code-blocks are
   tangled, and the tangled file is compiled."
     (when (equal (buffer-file-name)
                  (expand-file-name (concat user-emacs-directory "init.org")))
       ;; Avoid running hooks when tangling.
       (let ((prog-mode-hook nil))
         (org-babel-tangle)
       (byte-compile-file (concat user-emacs-directory "init.el"))
         (if (file-exists-p "init.elc") (delete-file "init.elc")) 
))
     )

   (add-hook 'after-save-hook 'tangle-init)

Package config

Managing extensions for Emacs is simplified using package which is built in to Emacs 24 and newer. To load downloaded packages we need to initialize package. cl is a library that contains many functions from Common Lisp, and comes in handy quite often, so we want to make sure it’s loaded, along with package, which is obviously needed.

(require 'cl)
(require 'package)
(package-initialize)

Packages can be fetched from different mirrors, melpa is the largest archive and is well maintained.

(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/"))

(add-to-list 'package-pinned-packages '(cider . "melpa-stable") t)

Default packages

These are some default packages that emacs needs to have by default. Other packages will be deferred subject to requirement.

   (let* ((package--builtins nil)
	    (packages
	     '(ag			; powerful silver searcher plugib
	       auto-compile		; automatically compile Emacs Lisp libraries
	       company			; Modular text completion framework
	       deadgrep			; Powerful grep 
	       define-word		; display the definition of word at point
	       doom-themes		;Another amazing theme
	       modus-operandi-theme	;Theme by prot
	       modus-vivendi-theme	;Another theme by prot
	       dracula-theme		; Theme
	       diminish			; Diminished modes from modeline
	       expand-region		; Increase selected region by semantic units
	       golden-ratio		; Automatic resizing windows to golden ratio
	       gruvbox-theme		;Another inspirational theme 
	       hydra			; Another nice aboabo package for persisten keys
	       magit			; control Git from Emacs
	       markdown-mode		; Emacs Major mode for Markdown-formatted files
	       matlab-mode		; MATLAB integration with Emacs
	       multiple-cursors		; Multiple cursors for Emacs
	       org			; Outline-based notes management and organizer
	       powerline		; Powerline for emacs
	       rg			; An even faster search optinon compared to ag 
	       try			; Try out Emacs packages
	       use-package		; Great package manager with nifty features
	       which-key
	       undo-tree		; Neat undo
	       )))         ; Display available keybindings in popup
     (ignore-errors ;; This package is only relevant for Mac OS X.
	 (when (memq window-system '(mac ns))
	   (push 'exec-path-from-shell packages)
	   (push 'reveal-in-osx-finder packages))
	 (let ((packages (remove-if 'package-installed-p packages)))
	   (when packages
	     ;; Install uninstalled packages
	     (package-refresh-contents)
	     (mapc 'package-install packages)))))

Mac OS based config

Switches meta to command from option

(when (memq window-system '(mac ns))
  (setq ns-pop-up-frames nil
        mac-option-modifier nil
        mac-command-modifier 'meta
        select-enable-clipboard t)
  (exec-path-from-shell-initialize)
  (when (fboundp 'mac-auto-operator-composition-mode)
    (mac-auto-operator-composition-mode 1)))'

Default configs

I’ve used the better-defaults package to get some basic configuration working. Some additional configurations are added here

(setq-default fill-column 80                    ; Maximum line width
              truncate-lines t                  ; Don't fold lines
              split-width-threshold 160         ; Split verticly by default
              split-height-threshold nil        ; Split verticly by default
              auto-fill-function 'do-auto-fill  ; Auto-fill-mode everywhere
              )	; Relative line numbering by default

Don’t ask “yes/No” questions, instead ask “y/n” questions

(fset 'yes-or-no-p 'y-or-n-p)

Start emacs-server

;; (server-start)

Eye candy

I play around with different font options and color themes. Currently I’ve settled down with the nice modus operandi theme by Protesilaos Stavrou and the overpass fonts from here.

    ;; (load-theme 'modus-vivendi t)
    (load-theme 'modus-operandi t)
    ;; (set-frame-font "Overpass 13" nil t)
    ;; (load-file "~/.emacs.d/clean.el")
    ;; (
    ;; (set-frame-font "Inconsolata 12" nil t)
     ;; (set-face-attribute 'default nil
     ;;            :family "Inconsolata 14"
     ;;            :height 110
     ;;            :weight 'normal
     ;;            :width 'normal)

    (use-package doom-modeline
          :ensure t
          :hook (after-init . doom-modeline-mode)
          :config
          (setq doom-modeline-env-python-executable "python")

          ;; Whether display perspective name or not. Non-nil to display in mode-line.
          (setq doom-modeline-persp-name t)

          ;; Whether display `lsp' state or not. Non-nil to display in mode-line.
          (setq doom-modeline-lsp nil)

          ;; Whether display github notifications or not. Requires `ghub` package.
          (setq doom-modeline-github nil)
          ;; Whether display minor modes in mode-line or not.
          (setq doom-modeline-minor-modes nil)

          ;; If non-nil, a word count will be added to the selection-info modeline segment.
          (setq doom-modeline-enable-word-count nil)

          ;; If non-nil, only display one number for checker information if applicable.
          (setq doom-modeline-checker-simple-format t)
          ;; Whether display the icon for major mode. It respects `doom-modeline-icon'.
          (setq doom-modeline-major-mode-icon t)

          ;; Whether display color icons for `major-mode'. It respects
          ;; `doom-modeline-icon' and `all-the-icons-color-icons'.
          (setq doom-modeline-major-mode-color-icon t)

          ;; Whether display icons for buffer states. It respects `doom-modeline-icon'.
          (setq doom-modeline-buffer-state-icon t)

          ;; Whether display buffer modification icon. It respects `doom-modeline-icon'
          ;; and `doom-modeline-buffer-state-icon'.
          (setq doom-modeline-buffer-modification-icon t)
) 

Other UI refinements

(tool-bar-mode -1)
(menu-bar-mode -1)
(toggle-scroll-bar -1) 
(setq inhibit-startup-message t) 

Powerline for emacs

(powerline-vim-theme) 

Diminish some modes form modeline

(defmacro safe-diminish (file mode &optional new-name)
  `(with-eval-after-load ,file
     (diminish ,mode ,new-name)))

(diminish 'auto-fill-function)
(safe-diminish "eldoc" 'eldoc-mode)
(safe-diminish "company" 'company-mode)
(safe-diminish "undo-tree" 'undo-tree-mode)
(safe-diminish "flyspell" 'flyspell-mode)
;; (safe-diminish "helm-mode" 'helm-mode)
(safe-diminish "paredit" 'paredit-mode "()") 

Default package config (mostly setting global modes)

Enable several packages by default. These are typically used throughout the config on all the major modes.

(dolist (mode
         '(abbrev-mode                  ; E.g. sopl -> System.out.println
           ;column-number-mode           ; Show column number in mode line
           delete-selection-mode        ; Replace selected text
           dirtrack-mode                ; directory tracking in *shell*
           global-company-mode          ; Auto-completion everywhere
           global-display-line-numbers-mode
           show-paren-mode              ; Highlight matching parentheses
           which-key-mode))             ; Available keybindings in popup
  (funcall mode 1)
  (tooltip-mode -1))

Basic company mode config

;; (setq company-minimum-prefix-length 3
;;       company-selection-wrap-around t)  ;wrapping around list of selections when scrolling
;; (setq company-selection-wrap-around t)
;; (setq company-dabbrev-downcase 0)
;; (setq company-idle-delay nil)

;;  (global-set-key "\t" 'company-complete-common)
;; ;; got this from https://www.reddit.com/r/emacs/comments/3r9fic/best_practicestip_for_companymode_andor_yasnippet/
;; ;; (setq company-transformers '(company-sort-by-occurrence))

Disabling suggestions automatically by company mode

;;; Prevent suggestions from being triggered automatically. In particular,
 ;;; this makes it so that:
 ;;; - TAB will always complete the current selection.
 ;;; - RET will only complete the current selection if the user has explicitly
 ;;;   interacted with Company.
 ;;; - SPC will never complete the current selection.
 ;;;
 ;;; Based on:
 ;;; - https://github.com/company-mode/company-mode/issues/530#issuecomment-226566961
 ;;; - https://emacs.stackexchange.com/a/13290/12534
 ;;; - http://stackoverflow.com/a/22863701/3538165
 ;;;
 ;;; See also:
 ;;; - https://emacs.stackexchange.com/a/24800/12534
 ;;; - https://emacs.stackexchange.com/q/27459/12534

 ;; <return> is for windowed Emacs; RET is for terminal Emacs
 ;; (dolist (key '("<return>" "RET"))
 ;;   ;; Here we are using an advanced feature of define-key that lets
 ;;   ;; us pass an "extended menu item" instead of an interactive
 ;;   ;; function. Doing this allows RET to regain its usual
 ;;   ;; functionality when the user has not explicitly interacted with
 ;;   ;; Company.
 ;;   (define-key company-active-map (kbd key)
 ;;     `(menu-item nil company-complete
 ;;                 :filter ,(lambda (cmd)
 ;;                            (when (company-explicit-action-p)
 ;;                              cmd)))))
 ;; (define-key company-active-map (kbd "TAB") #'company-complete-selection)
 ;; (define-key company-active-map (kbd "SPC") nil)

 ;; Company appears to override the above keymap based on company-auto-complete-chars.
 ;; Turning it off ensures we have full control.
 (setq company-auto-complete-chars nil)

Modes

Now we write down language specific (python, latex, org, etc.) or feature specific (spelling, autocompletion etc.) configuration

Better defaults

Sets up better defaults for emacs

     (use-package better-defaults
	:ensure)

Spelling

For spell-checking we will use the wonderful flyspell package. We will enable flyspell for all text-mode buffers and comment regions for prog-mode buffers. This is the standard practise

;; (add-hook 'text-mode-hook 'turn-on-flyspell)
;; (add-hook 'prog-mode-hook 'flyspell-prog-mode)

Addtionally for correcting spelling (or getting suggestions for corrections), we will use a nice wrapper called fly-spell-correct flyspell-correct via helm

(use-package flyspell-correct-ivy
  :ensure t
  :bind ("C-'" . flyspell-correct-wrapper)
  :init
  (setq flyspell-correct-interface #'flyspell-correct-ivy))

Window navigation

Ace-window mode provides comprehensive functions and keybindings to move and manipulate windows.

(use-package ace-window
  :ensure t
  :config
  (setq aw-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l))
  (setq ace-ignore-current t)
  :bind ("M-o" . ace-window))

Amx

This is an alternative to the popular smex mode, which is apparently no longer under active development.

(use-package amx
  :ensure t
  :bind ("s-x" . amx))

Async

Essential package for supporting asynchronous operations in emacs. This is especially useful when carrying out cpu intensive operations such as copy.

(use-package async
  :ensure t
  :config
  (dired-async-mode 1)
  (async-bytecomp-package-mode 1))

Selectrum

(use-package selectrum
  :ensure
  :config

  (use-package prescient
    :ensure t
    :config
    (prescient-persist-mode +1)
    )
  (use-package selectrum-prescient
    :ensure
    :after selectrum)
  (selectrum-mode -1)
  (selectrum-prescient-mode -1))

Yasnippet

(use-package yasnippet                  ; Snippets
  :ensure t
  :config
  (setq
   yas-verbosity 1                      ; No need to be so verbose
   yas-wrap-around-region t)

  ;; (with-eval-after-load 'yasnippet
  ;;   (setq yas-snippet-dirs '(yasnippet-snippets-dir)))

  (yas-reload-all)
  (yas-global-mode))

(use-package yasnippet-snippets         ; Collection of snippets
  :ensure t)

Ivy

(use-package ivy :ensure t
  :diminish (ivy-mode . "")
  :bind
  (:map ivy-mode-map
   ("C-'" . ivy-avy)
   ("s-b" . ivy-switch-buffer-other-window)
   ("s-f" . find-file-other-window))
  :config
  (use-package ivy-prescient
    :ensure t
    :after ivy)
  (ivy-mode 1)
  (ivy-prescient-mode 1)
  ;; add ‘recentf-mode’ and bookmarks to ‘ivy-switch-buffer’.
  (setq ivy-use-virtual-buffers t)
  ;; number of result lines to display
  (setq ivy-height 10)
  ;; does not count candidates
  (setq ivy-count-format "")
  ;; no regexp by default
  (setq ivy-initial-inputs-alist nil)
  ;; configure regexp engine.
  (setq ivy-re-builders-alist
	  ;; allow input not in order
	  '((t   . ivy--regex-ignore-order))))

Counsel

  1. Basic counsel definition
    (use-package counsel :ensure t)
    
        
  2. Counsel etags This is a drop-in replacement for xref-find-definition. I’m going to rebind M-. to counsel-etags-find-tag. The cool thing about this package is, if the TAGS file is not found in the directory, the package will create it for you.
             (use-package counsel-etags
               :ensure t
               :bind
               ("C-." . 'counsel-etags-find-tag-at-point)
               ("s-i" . 'counsel-outline)
               ("s-." . 'counsel-git-grep)
    )
        

Ctrlf

This is a new search application. I’m going to try this in place of isearch

(use-package ctrlf
  :ensure
  :init
  (ctrlf-mode +1)
  )

Dumb jump

Package to jump around code efficiently. Officially claims to support more that 40 languages including python and matlab.

(use-package dumb-jump
  :bind (("M-g o" . dumb-jump-go-other-window)
         ("M-g j" . dumb-jump-go)
         ("M-g b" . dumb-jump-back)
         ("M-g i" . dumb-jump-go-prompt)
         ("M-g x" . dumb-jump-go-prefer-external)
         ("M-g z" . dumb-jump-go-prefer-external-other-window)
         ("M-g M-l" . dumb-jump-quick-look)
         )
  :config (setq dumb-jump-selector 'ivy) ;; (setq dumb-jump-selector 'helm)
  :ensure)

Iedit mode

(use-package iedit
  :ensure
  :defer 
  :bind ("C-:" . 'iedit-mode))

Dired

Some configuration regarding dired and some of it utility packages

(use-package dired
  :config 
  (setq dired-recursive-copies 'always)
  (setq dired-recursive-deletes 'always)
  (setq delete-by-moving-to-trash t)
  (setq dired-listing-switches
        "-AGFhlv --group-directories-first --time-style=long-iso")
  (setq dired-dwim-target t)
  ;; Note that the the syntax for `use-package' hooks is controlled by
  ;; the `use-package-hook-name-suffix' variable.  The "-hook" suffix is
  ;; not an error of mine.
  :hook ((dired-mode-hook . dired-hide-details-mode)
         (dired-mode-hook . hl-line-mode)))

Dired narrow

Dynamically narrow Dired buffer

(use-package dired-narrow
  :ensure 
  :bind (:map dired-mode-map
              ("/" . dired-narrow)))

Dired jump

     (use-package emacs
	:bind
	("C-x C-j" . 'counsel-dired-jump))

Dired subtree

Use tab and s-tab to open and close directories

(use-package dired-subtree
  :ensure
  :after dired
  :config
  (setq dired-subtree-use-backgrounds nil)
  :bind (:map dired-mode-map
              ("<tab>" . dired-subtree-toggle)
              ("<C-tab>" . dired-subtree-cycle)
              ("<S-iso-lefttab>" . dired-subtree-remove)))

Diredfl

More colors on the dired buffer

(use-package diredfl
  :ensure
  :hook (dired-mode-hook . diredfl-mode))

Dired peep

Preview files inside dired

(use-package peep-dired
  :ensure
  :after dired
  :config
  (setq peep-dired-cleanup-on-disable t)
  (setq peep-dired-enable-on-directories nil)
  (setq peep-dired-ignored-extensions
        '("mkv" "webm" "mp4" "mp3" "ogg" "iso"))
  :bind (:map dired-mode-map
              ("P" . peep-dired)))

Tramp

This has some refinements for tramp mode

(use-package tramp
  :config
  (setq tramp-inline-compress-start-size 1000000)
  ;;(setq vc-handled-backends (quote (RCS CVS SVN SCCS Bzr Hg Mtn Arch)))
  (setq vc-handled-backends '(SVN Git))
  (setq remote-file-name-inhibit-cache nil)
  (setq vc-ignore-dir-regexp
        (format "%s\\|%s"
                vc-ignore-dir-regexp
                tramp-file-name-regexp))
  (setq tramp-verbose 1)
  )

Deft mode

(use-package deft
  :ensure t
  :bind ("<f8>" . 'deft)
  :config
  (setq deft-directory "/Users/vigneshganapathiraman/Dropbox/notes"
        deft-extensions '("md" "org"))
  )

Flycheck

Flycheck mode is apparently faster and more efficient than flymake mode that comes by default with emacs

(use-package flycheck
  :ensure
  :config
  (global-flycheck-mode 1)
  (flymake-mode -1))

Python mode

We will use anaconda mode for python.

(use-package pyvenv
  :ensure t
  :config
  (setenv "WORKON_HOME" "/home/vigneshpop/miniconda3/envs")
  (pyvenv-workon "torch"))		;Can be changed later



(use-package elpy
  :ensure t
  :after (python)
  :config
  (elpy-enable)
  (setq elpy-rpc-virtualenv-path 'current)


  ;; Some recommended configuration options by elpy

                                        ; Use flycheck instead of flymake
  (when (load "flycheck" t t)
    (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
    (add-hook 'elpy-mode-hook 'flycheck-mode))

  ;; Use this improved function for function navigation
  ;; ;; 
  ;; This function additionally searches for the symbol (or function) using rgrep
  ;; if the function definition using tag information is not found. 

  (defun elpy-goto-definition-or-rgrep ()
  "Go to the definition of the symbol at point, if found. Otherwise, run `elpy-rgrep-symbol'."
  (interactive)
  (ring-insert find-tag-marker-ring (point-marker))
  (condition-case nil (elpy-goto-definition)
    (error (elpy-rgrep-symbol
            (concat "\\(def\\|class\\)\s" (thing-at-point 'symbol) "(")))))

  :bind (:map python-mode-map
             ("M-." . 'elpy-goto-assignment)
  )
  )

Highlight indent guide mode for highlighting indentation. This is especially useful if we are having long blocks of python code.

(use-package highlight-indent-guides
  :ensure t
  :after (python))

Linting support using the awesome black linter

(use-package python-black
  :ensure
  :after python
  :bind (:map python-mode-map
              ("M-q". 'python-black-statement)
              ("M-Q" . 'python-black-buffer)))

Some more improvements

(use-package python
  :after python
  :config
  (setq python-indent-offset 4)    
  (setq flycheck-check-syntax-automatically nil)
  (setq python-shell-interpreter "python3") ; Set interpreter to just
                                           ; python, let the virtual
                                           ; env figure out the right
                                           ; python based on the context
  :bind (:map python-mode-map
              ("C-c n f" . 'narrow-to-defun)
              ("C-c n w" . 'widen)))

Ipython notebook mode

This is a frontend to jupyter notebooks with emacs keybindings and other features.

     (use-package ein
	:ensure t
	:defer
	:diminish "ein"
	)

Org mode

  1. Babel: Org babel lets you to write and execute a lot of languages within org mode
    ;; active Babel languages
    (use-package org
      :defer t
      :config
        (org-babel-do-load-languages
        'org-babel-load-languages
        '((R . t)
        (emacs-lisp . t)
              (matlab . t)
              (latex . t)
        (python . t))
        )
    
        ;; When editing org-files with source-blocks, we want the source
        ;; blocks to be themed as they would in their native mode.
    
        (setq org-src-fontify-natively t
              ;; org-src-tab-acts-natively t
              org-confirm-babel-evaluate nil
              org-adapt-indentation t
              org-hide-leading-stars nil
              )
        )
        
  2. Some default configs while editing files in org mode
    ;; (add-hook 'org-mode-hook 'auto-fill-mode)
    (use-package org
      :config
      (org-indent-mode +1)
      (auto-fill-mode +1))
        
  3. Reference management using org-ref
    (use-package org-ref
      :ensure t
      :defer t
      :after org
      :config
      (setq bibtex-completion-bibliography "~/Dropbox/bibliography/references_zotero.bib")
      )
        
  4. Org pandoc : conversion between several formats via org-export
    (use-package ox-pandoc
      :ensure t
      :defer t)
        
  5. Ox reveal for amazing presentations
    (use-package ox-reveal
      :ensure 
      :demand
      ox-reveal)
    
    ;; (setq org-reveal-root "http://cdn.jsdelivr.net/reveal.js/3.0.0/")
    (setq org-reveal-root "file:////Users/z0041v0/Downloads/reveal.js")
    (setq org-reveal-mathjax t)
    
    (use-package htmlize
      :ensure t
      :demand)
        

Org mode and latex

  1. Make org latex export use latexmk while exporting documents to pdf
    (setq org-latex-pdf-process
          '("pdflatex -interaction nonstopmode -output-directory %o %f"
            "bibtex %b"
            "pdflatex -interaction nonstopmode -output-directory %o %f"
            "pdflatex -interaction nonstopmode -output-directory %o %f"))
        

latex

We will use the wonderful auctex mode

;; (setq reftex-default-bibliography "/Users/vigneshganapathiraman/bibliography/references_zotero.bib")
(use-package reftex
  :ensure t
  :after auctex
  :config
  (setq reftex-enable-partial-scans t)
  (setq reftex-save-parse-info t)
  (setq reftex-use-multiple-selection-buffers t)
  (setq reftex-plug-into-AUCTeX t)
  (setq reftex-label-alist '(AMSTeX))   ;enable eqref inside reftex

  ) 

(use-package tex-site
  :ensure auctex
  :mode ("\\.tex\\'" . latex-mode)
  :defer t
  :config
  (setq-default bibtex-dialect 'biblatex)
  (add-hook 'LaTeX-mode-hook
            (lambda ()
              (turn-on-reftex)
              (turn-on-auto-fill)
              (latex-math-mode)
              (TeX-PDF-mode t)
              (TeX-source-correlate-mode t)
              (setq TeX-source-correlate-method 'synctex)
              (setq 
               TeX-source-correlate-start-server t
               )
              (setq TeX-view-program-selection '((output-pdf "pdf-tools")))
              (setq auctex-latexmk-inherit-TeX-PDF-mode t)
              (setq TeX-view-program-list
                    '(("pdf-tools" "TeX-pdf-tools-sync-view")))
              (add-hook 'TeX-after-compilation-finished-functions #'TeX-revert-document-buffer)

              ;; Fix indentation
              (setq LaTeX-indent-level 3)
              (setq LaTeX-item-indent 3)
              (setq TeX-brace-indent-level 3)
              (add-to-list 'company-backends 'company-math-symbols-unicode)
              ))
  )
(use-package auctex-latexmk
  :ensure t
  :defer
  :config
  (auctex-latexmk-setup)
  )

(use-package company-math
  :ensure t
  :after auctex)

Markdown

(use-package markdown-mode
  :ensure t
  :defer t)
(setq auto-mode-alist 
      (cons '("\\.md" . markdown-mode) auto-mode-alist))

matlab

(defun vig/matlab-shell-send-line ()
  "send the current line to python repl"
  (interactive)
  (matlab-shell-run-region
   (progn (forward-visible-line 0) (point))
   (progn (forward-visible-line 1) (point)) )
  )
(use-package matlab-mode
  :ensure t
  :mode ("\\.m\\'" . matlab-mode)
  :bind ("C-RET" . 'matlab-shell-run-region-or-line)
  :after matlab
  :init
  :config
  (setq matlab-shell-echoes nil)
  (setq matlab-indent-function t)
  (setq matlab-shell-command "matlab")
  (company-mode)
)

Julia

(use-package julia-mode
  :ensure t
  :defer t)

Go lang

Emacs setup for golang

  (use-package go-mode
    :ensure
    :config
    (add-to-list 'auto-mode-alist '("\\.go\\'" . go-mode))
    (add-hook 'go-mode-hook 'lsp-deferred)
  (use-package lsp-mode
  :ensure t
  :commands (lsp lsp-deferred)
  :hook (go-mode . lsp-deferred))
  (use-package company-lsp
  :ensure t
  :commands company-lsp)

  ;; Set some parameters
  

(setq lsp-ui-doc-enable nil
      lsp-ui-peek-enable t
      lsp-ui-sideline-enable t
      lsp-ui-imenu-enable t
      lsp-ui-flycheck-enable t))

    

ESS

ESS (Emacs speaks statistics) is a comprehensive package for using statistcs related packagse in emacs. I mostly use it to interact with R and write R code. However, recently I’m also using it to program in julia.

(use-package ess
  :ensure t
  :defer t
  :mode (
         ("\\.R\\'" . ess-r-mode)
         ("\\.jl\\'" . ess-julia-mode))

  )

General

(use-package general
  :ensure t)

Utils

(defun er-copy-file-name-to-clipboard ()
  "Copy the current buffer file name to the clipboard."
  (interactive)
  (let ((filename (if (equal major-mode 'dired-mode)
                      default-directory
                    (buffer-file-name))))
    (when filename
      (kill-new filename)
      (message "Copied buffer file name '%s' to the clipboard." filename))))


(defun copy-line (arg)
      "Copy lines (as many as prefix argument) in the kill ring"
      (interactive "p")
      (kill-ring-save (line-beginning-position)
                      (line-beginning-position (+ 1 arg)))
      (message "%d line%s copied" arg (if (= 1 arg) "" "s")))

(defun quick-cut-line ()
  "Cut the whole line that point is on.  Consecutive calls to this command append each line to the kill-ring."
  (interactive)
  (let ((beg (line-beginning-position 1))
	(end (line-beginning-position 2)))
    (if (eq last-command 'quick-cut-line)
	(kill-append (buffer-substring beg end) (< end beg))
      (kill-new (buffer-substring beg end)))
    (delete-region beg end))
  (beginning-of-line 1)
  (setq this-command 'quick-cut-line))

Centaur tabs

Nice tabs mode for modern UI switching

(use-package centaur-tabs
 :ensure t
 :demand
 :init (setq centaur-tabs-set-bar 'over)
 :config
 (centaur-tabs-mode)
 (centaur-tabs-headline-match)
 (setq centaur-tabs-set-modified-marker t
       centaur-tabs-modified-marker ""
       centaur-tabs-cycle-scope 'tabs
       centaur-tabs-height 30
       centaur-tabs-set-icons t
       centaur-tabs-close-button " × ")
 (dolist (centaur-face '(centaur-tabs-selected
                         centaur-tabs-selected-modified
                         centaur-tabs-unselected
                         centaur-tabs-unselected-modified))
   (set-face-attribute centaur-face nil :family "Arial" :height 130))
 :bind
 ("C-S-<tab>" . centaur-tabs-backward)
 ("C-<tab>" . centaur-tabs-forward))

Multiple cursors

Setup borrowed from Kaushal Modi’s setup

;; Time-stamp: <2017-09-20 09:52:55 kmodi>

;; Multiple Cursors
;; https://github.com/magnars/multiple-cursors.el

(use-package multiple-cursors
  :bind (
         ("C-S-c C-S-c" . mc/edit-lines)
         ("C->" . mc/mark-next-like-this)
         ("C-<" . mc/mark-previous-like-this)
         ("C-c C-<" . mc/mark-all-like-this)
         ("C-S-<down>" . mc/mark-next-lines)
         ("C-S-<up>" . mc/mark-previous-lines)
         ("C-S-<mouse-1>" . mc/add-cursor-on-click))
  :init
  (progn
    ;; Temporary hack to get around bug # 28524 in emacs 26+
    ;; https://debbugs.gnu.org/cgi/bugreport.cgi?bug=28524
    (setq mc/mode-line
          `(" mc:" (:eval (format ,(propertize "%-2d" 'face 'font-lock-warning-face)
                                  (mc/num-cursors)))))

    (setq mc/list-file (locate-user-emacs-file "mc-lists"))

    ;; The `multiple-cursors-mode-enabled-hook' and
    ;; `multiple-cursors-mode-disabled-hook' are run in the
    ;; `multiple-cursors-mode' minor mode definition, but they are not declared
    ;; (not `defvar'd). So do that first before using `add-hook'.
    (defvar multiple-cursors-mode-enabled-hook nil
      "Hook that is run after `multiple-cursors-mode' is enabled.")
    (defvar multiple-cursors-mode-disabled-hook nil
      "Hook that is run after `multiple-cursors-mode' is disabled.")))

;; * Mark one more occurrence
;;
;; | mc/mark-next-like-this            | Adds a cursor and region at the next part of the buffer       |
;; |                                   | forwards that matches the current region.                     |
;; | mc/mark-next-word-like-this       | Like `mc/mark-next-like-this` but only for whole words.       |
;; | mc/mark-next-symbol-like-this     | Like `mc/mark-next-like-this` but only for whole symbols.     |
;; | mc/mark-previous-like-this        | Adds a cursor and region at the next part of the buffer       |
;; |                                   | backwards that matches the current region.                    |
;; | mc/mark-previous-word-like-this   | Like `mc/mark-previous-like-this` but only for whole words.   |
;; | mc/mark-previous-symbol-like-this | Like `mc/mark-previous-like-this` but only for whole symbols. |
;; | mc/mark-more-like-this-extended   | Use arrow keys to quickly mark/skip next/previous occurances. |
;; | mc/add-cursor-on-click            | Bind to a mouse event to add cursors by clicking.             |
;; |                                   | See tips-section.                                             |
;;
;; * Mark many occurrences
;;
;; | mc/mark-all-like-this                  | Marks all parts of the buffer that matches the current region.        |
;; | mc/mark-all-words-like-this            | Like `mc/mark-all-like-this` but only for whole words.                |
;; | mc/mark-all-symbols-like-this          | Like `mc/mark-all-like-this` but only for whole symbols.              |
;; | mc/mark-all-in-region                  | Prompts for a string to match in the region, adding cursors           |
;; |                                        | to all of them.                                                       |
;; | mc/mark-all-like-this-in-defun         | Marks all parts of the current defun that matches the current region. |
;; | mc/mark-all-words-like-this-in-defun   | Like `mc/mark-all-like-this-in-defun` but only for whole words.       |
;; | mc/mark-all-symbols-like-this-in-defun | Like `mc/mark-all-like-this-in-defun` but only for whole symbols.     |
;; | mc/mark-all-like-this-dwim             | Tries to be smart about marking everything you want. Can be           |
;; |                                        | pressed multiple times.                                               |
;;
;; * Special
;;
;; | set-rectangular-region-anchor | Think of this one as `set-mark` except you're marking a rectangular region. |
;; | mc/mark-sgml-tag-pair         | Mark the current opening and closing tag.                                   |
;; | mc/insert-numbers             | Insert increasing numbers for each cursor, top to bottom.                   |
;; | mc/sort-regions               | Sort the marked regions alphabetically.                                     |
;; | mc/reverse-regions            | Reverse the order of the marked regions.                                    |
;;
;; ** Tips and tricks
;; - To get out of multiple-cursors-mode, press `<return>` or `C-g`. The latter will
;;   first disable multiple regions before disabling multiple cursors. If you want to
;;   insert a newline in multiple-cursors-mode, use `C-j`.
;; - Sometimes you end up with cursors outside of your view. You can
;;   scroll the screen to center on each cursor with `C-v` and `M-v`.
;; - Try pressing `mc/mark-next-like-this` with no region selected. It will just add a cursor
;;   on the next line.
;; - Try pressing `mc/mark-all-like-this-dwim` on a tagname in html-mode.
;; - Notice that the number of cursors active can be seen in the modeline.
;; - If you get out of multiple-cursors-mode and yank - it will yank only
;;   from the kill-ring of main cursor. To yank from the kill-rings of
;;   every cursor use yank-rectangle, normally found at C-x r y.
;; - You can use `mc/reverse-regions` with nothing selected and just one cursor.
;;   It will then flip the sexp at point and the one below it.
;; - If you would like to keep the global bindings clean, and get custom keybindings
;;   when the region is active, you can try [region-bindings-mode](https://github.com/fgallina/region-bindings-mode).
;;
;; It is recommended to add `mc/mark-next-like-this` to a key binding that's
;; right next to the key for `er/expand-region`.

PDF tools

Awesom pdf reader right inside emacs

      (use-package pdf-tools
	:ensure
	:init
	(pdf-tools-install)
        :config
        (setq auto-revert-interval 0.5) 
)

Custom functions

  • Create an empty buffer
(defun create-empty-buffer () 
  "Open a new empty buffer.
   Borrowed from
   https://emacs.stackexchange.com/questions/20/re-open-scratch-buffer"
  (interactive)
  (let ((buf (generate-new-buffer "untitled")))
    (switch-to-buffer buf)
    (funcall (and initial-major-mode))
    (setq buffer-offer-save t)))

Keybindings

Basic keybindings

These are evil specific keybindings. All definitions use general.el

  1. Essentials
    (general-define-key
     "C-e" 'end-of-line
     "C-a" 'beginning-of-visual-line
     "C-k" 'kill-line
     "M-+" 'text-scale-increase
     "M-_" 'text-scale-decrease
     "C-x C-b" 'ibuffer
     "C-M-o" 'hydra-window/body
     "<f6>" 'deadgrep
     "M-O" 'occur
     "C-x r y" 'er-copy-file-name-to-clipboard
     "s-w" 'copy-line
     "s-W" 'quick-cut-line
     )
        
  2. Narrow and widen
    (general-define-key 
     :prefix "M-n"
     "n" 'narrow-to-region
     "w" 'widen)
        
  3. Program mode bindings
    (general-define-key
     "C-;" 'comment-line)
        
  4. Winner mode
    (general-define-key
     "<s-right>" 'winner-undo
     "<s-left>" 'winner-redo)
        

Expand region

(global-set-key (kbd "C-S-m") 'er/expand-region)
(global-set-key (kbd "C-S-n") 'er/contact-region)

Magit

(use-package magit
  :defer
  :bind (("M-s-g" . 'magit-status)))

Custom global shortcuts

  1. Create new buffer with custom
    (global-set-key (kbd "C-x |") 'create-empty-buffer)