If you work with other on text files, you often run into the problem that one of the programmers is using tabs set to 4, another to 8 spaces. For this reason, I always save my files without tabs. In Emacs there is a command to do this “untabify-buffer”, but I usually forget to do this.
Searching the internet I found the following code at stackoverflow.com, to do it automatically as soon as you save the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
(defun untabify-buffer () "Untabify current buffer" (interactive) (untabify (point-min) (point-max))) (defun progmodes-hooks () "Hooks for programming modes" (yas/minor-mode-on) (add-hook 'before-save-hook 'progmodes-write-hooks)) (defun progmodes-write-hooks () "Hooks which run on file write for programming modes" (prog1 nil (set-buffer-file-coding-system 'utf-8-unix) (untabify-buffer))) (add-hook 'gams-mode-hook 'progmodes-hooks) (add-hook 'ess-mode-hook 'progmodes-hooks) (add-hook 'python-mode-hook 'progmodes-hooks) |
You can define your own hooks for other modes.
If you would use the untabify-buffer as a common hook, you can run into problems, because every file will be untabified.