Notes on Configuring Emacs for Web Development

New computer means new configuration from scratch! Since Emacs is the mainstay program I use across devices, I want to document how I'm doing web development with it right now.

First I took the time to go over my init.el and remove anything I'm not using. I like to aim for about 100-120 lines of code on it. It's an arbitrary number, but it makes it easy to know where things are whenever I change it. One of the drawbacks of Emacs is how fun and time consuming it can become to want to mess around with configurations, but that's just procrastination so this limit is important to keep me in check.

Here are my notes on how I'm setting up Emacs for web development.

I've installed Emacs on macOS via the emacs-plus homebrew recipe. I learned about it from Alvaro Ramirez's Awesome Emacs on macOS article.

Emacs packages

Installing Prettier

First I installed npm via homebrew:

$ brew install npm

Next, to get prettier to run in Emacs I installed it globally via npm:

$ npm i -g prettier@3.9.5

For Emacs to find the node executable, I had to update my init.el. This code synchronizes Emacs's internal environment variables with my system user shell.

(exec-path-from-shell-initialize)

File-based Mode Inits

I'm sure I'll update the list as I start working on other projects on this computer, but for now HTML and CSS is enough for this blog, so here's what I added in my init.el to start web-mode when opening files with those extensions:

;; File-based mode inits
(add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.css?\\'" . web-mode))

There's also a hook on web-mode to set some configuration and start the other minor modes:


;; Hooks 
(add-hook 'web-mode-hook
	  (lambda ()
	    (display-line-numbers-mode 1)
	    (emmet-mode 1)
	    (prettier-mode 1)
	    (setq web-mode-markup-indent-offset 2)
	    (setq web-mode-css-indent-offset 2)
	    (define-key emmet-mode-keymap (kbd "TAB") 'emmet-expand-line)))
        

That's it for now! :-)