Bringing my Emacs from the past

Translations: es - Tags: emacs

I started using Emacs in 1995, and since then I have been carrying a .emacs that by now has a lot of accumulated crap. It is such an old configuration that it didn't even use the modern convention of ~/.emacs.d/init.el (and it looks like a newer Emacs version will allow .config/emacs as per the XDG standard... at last).

I have wanted to change my Emacs configuration for some time, and give it all the pretty and modern toys.

The things that matter the most to me:

  • Not have a random dumpster in ~/.emacs if possible.
  • Pretty colors.
  • Magit.
  • Rust-mode or whatever the new thing is for rust-analyzer and the Language Server.

After looking at several examples of configurations that mention use-package as a unified way of loading packages and configuring them, I found this configuration which is extremely well documented. The author does literate programming with org-mode and elisp — something which I'm casually interested in, but not just now — but that way everything ends up very well explained and easy to read.

I extracted bits of that configuration and ended up with the following.

Everything in ~/.emacs/init.el and with use-package

;; Initialize package system

(require 'package)

(setq package-archives
      '(("org"     .       "https://orgmode.org/elpa/")
        ("gnu"     .       "https://elpa.gnu.org/packages/")
        ("melpa"   .       "https://melpa.org/packages/")))

(package-initialize)
;(package-refresh-contents)

;; Use-package for civilized configuration

(unless (package-installed-p 'use-package)
  (package-install 'use-package))
(require 'use-package)

(setq use-package-always-ensure t)

~/.emacs.d/custom.el for M-x customize stuff

;; Set customization data in a specific file, without littering
;; my init files.

(setq custom-file "~/.emacs.d/custom.el")
(load custom-file)

Which-key to get hints when typing command prefixes

;; Make it easier to discover key shortcuts

(use-package which-key
  :diminish
  :config
  (which-key-mode)
  (which-key-setup-side-window-bottom)
  (setq which-key-idle-delay 0.1))

Don't pollute the modeline with common modes

;; Do not show some common modes in the modeline, to save space

(use-package diminish
  :defer 5
  :config
  (diminish 'org-indent-mode))

Magit to use git in a civilized fashion

;; Magit

(use-package magit
  :config
  (global-set-key (kbd "C-x g") 'magit-status))

Move between windows with Shift-arrows

;; Let me switch windows with shift-arrows instead of "C-x o" all the time
(windmove-default-keybindings)

Pretty colors

I was using solarized-dark but I like this one even better:

;; Pretty colors

(use-package flatland-theme
  :config
  (custom-theme-set-faces 'flatland
   '(show-paren-match ((t (:background "dark gray" :foreground "black" :weight bold))))
   '(show-paren-mismatch ((t (:background "firebrick" :foreground "orange" :weight bold))))))

Nyan cat instead of scrollbars

;; Nyan cat instead of scrollbar
;; scroll-bar-mode is turned off in custom.el

(use-package nyan-mode
  :config
  (nyan-mode 1))

Move buffers to adjacent windows

;; Move buffers between windows

(use-package buffer-move
  :config
  (global-set-key (kbd "<C-S-up>")     'buf-move-up)
  (global-set-key (kbd "<C-S-down>")   'buf-move-down)
  (global-set-key (kbd "<C-S-left>")   'buf-move-left)
  (global-set-key (kbd "<C-S-right>")  'buf-move-right))

Change buffer names for files with the same name

;; Note that ‘uniquify’ is builtin.
(require 'uniquify)
(setq uniquify-separator "/"               ;; The separator in buffer names.
      uniquify-buffer-name-style 'forward) ;; names/in/this/style

Helm to auto-complete in grand style

(use-package helm
 :diminish
 :init (helm-mode t)
 :bind (("M-x"     . helm-M-x)
        ("C-x C-f" . helm-find-files)
        ("C-x b"   . helm-mini)     ;; See buffers & recent files; more useful.
        ("C-x r b" . helm-filtered-bookmarks)
        ("C-x C-r" . helm-recentf)  ;; Search for recently edited files
        ("C-c i"   . helm-imenu)
        ("C-h a"   . helm-apropos)
        ;; Look at what was cut recently & paste it in.
        ("M-y" . helm-show-kill-ring)

        :map helm-map
        ;; We can list ‘actions’ on the currently selected item by C-z.
        ("C-z" . helm-select-action)
        ;; Let's keep tab-completetion anyhow.
        ("TAB"   . helm-execute-persistent-action)
        ("<tab>" . helm-execute-persistent-action)))

Ripgrep to search in grand style

;; Ripgrep

(use-package rg
  :config
  (global-set-key (kbd "M-s g") 'rg)
  (global-set-key (kbd "M-s d") 'rg-dwim))

(use-package helm-rg)

Rust mode and Language Server

Now that RLS is in the process of being deprecated, it's getting substituted with rust-analyzer. Also, rust-mode goes away in favor of rustic.

;; Rustic, LSP

(use-package flycheck)

(use-package rustic)

(use-package lsp-ui)

(use-package helm-lsp
  :config
  (define-key lsp-mode-map [remap xref-find-apropos] #'helm-lsp-workspace-symbol))

Performatively not get distracted

;;; Show a notification when compilation finishes

(setq compilation-finish-functions
      (append compilation-finish-functions
          '(fmq-compilation-finish)))

(defun fmq-compilation-finish (buffer status)
  (when (not (member mode-name '("Grep" "rg")))
    (call-process "notify-send" nil nil nil
          "-t" "0"
          "-i" "emacs"
          "Compilation finished in Emacs"
          status)))

Stuff from custom.el

The interesting bits here are making LSP work; everything else is preferences.

(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.
 '(column-number-mode t)
 '(custom-safe-themes
   (quote
    ("2540689fd0bc5d74c4682764ff6c94057ba8061a98be5dd21116bf7bf301acfb" "bffa9739ce0752a37d9b1eee78fc00ba159748f50dc328af4be661484848e476" "0fffa9669425ff140ff2ae8568c7719705ef33b7a927a0ba7c5e2ffcfac09b75" "2809bcb77ad21312897b541134981282dc455ccd7c14d74cc333b6e549b824f3" default)))
 '(delete-selection-mode nil)
 '(lsp-rust-analyzer-display-chaining-hints t)
 '(lsp-rust-analyzer-display-parameter-hints nil)
 '(lsp-rust-analyzer-macro-expansion-method (quote rustic-analyzer-macro-expand))
 '(lsp-rust-analyzer-server-command (quote ("/home/federico/.cargo/bin/rust-analyzer")))
 '(lsp-rust-analyzer-server-display-inlay-hints nil)
 '(lsp-rust-full-docs t)
 '(lsp-rust-server (quote rust-analyzer))
 '(lsp-ui-doc-alignment (quote window))
 '(lsp-ui-doc-position (quote top))
 '(lsp-ui-sideline-enable nil)
 '(menu-bar-mode nil)
 '(package-selected-packages
   (quote
    (helm-lsp lsp-ui lsp-mode flycheck rustic rg helm-rg ripgrep helm-projectile helm buffer-move nyan-mode flatland-black-theme flatland-theme afternoon-theme spacemacs-theme solarized-theme magit diminish which-key use-package)))
 '(rustic-lsp-server (quote rust-analyzer))
 '(scroll-bar-mode nil)
 '(scroll-step 0)
 '(tool-bar-mode nil))
(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.
 )

Results

I am very happy with rustic / rust-analyzer and the Language Server. Having documentation on each thing when one moves the cursor around code is something that I never thought would work well in Emacs. I haven't decided if I love M-x lsp-rust-analyzer-inlay-hints-mode or if it drives me nuts; it shows you the names of function arguments and inferred types among the code. I suppose I'll turn it off and on as needed.

Some days ago, before using helm, I had projectile-mode to work with git checkouts and I was quite liking it. I haven't found how to configure helm-projectile to work; I'll have to keep experimenting.