Easy indentation setup in Emacs for web development
This question has been asked so many times, so I answered it here once for all.
Insert below code into ~/.emacs:
You can use `prog-mode-hook` and `buffer-file-name` to do the per project setup easily.
For any new programming language not covered in this post, there are two steps:
- Know your major-mode by pressing `ALT-X major-mode ENTER`. Say you got "org-mode".
- Google "Emacs org-mode indent offset"
- Please note the first paramter of `string-match-p` is actually regular expression. So you can write regular expression like "\\(/proj1\\|work.org\\)"
- Emacs gives you full freedom to setup project. If you prefer per computer setup instead of per project setup, you only need re-write `my-setup-develop-environment',
(defun my-setup-develop-environment () (interactive) (let ((hostname (with-temp-buffer (shell-command "hostname" t) (goto-char (point-max)) (delete-char -1) (buffer-string)))) (if (string-match-p "home-pc" hostname) (my-personal-code-style)) (if (string-match-p "office-pc" hostname) (my-office-code-style))))
- Instead of press TAB manually to indent code line by line, you can select a region and press `Alt-x indent-region ENTER`
- Because I use Evil, indenting the whole file is as simple as pressing `gg=G`