Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emacs: Convert python spaces to tabs

Currently I use following setting in my .emacs file:

;; single tab for every indent
(add-hook 'python-mode-hook
  (lambda ()
    (setq indent-tabs-mode t)
    (setq python-indent 4)
    (setq tab-width 4)))

This works perfectly for any new file that I create. By if I download a project from git/hg which happened to be on space emacs will not convert them to be on space. How can I force emacs to convert all the spaces to tabs regardless whats the original setting of the file is.

I understand that this would create large chunk of change set if its a git/hg repo but I still like to figure out how to convert any file with space to change it to tabs via my emacs settings.

like image 501
add-semi-colons Avatar asked Jan 12 '23 19:01

add-semi-colons


2 Answers

Mark the entire buffer with C-x h, and then use M-x tabify.

like image 181
legoscia Avatar answered Jan 14 '23 07:01

legoscia


Built upon triplee's comment:

(add-hook 'python-mode-hook
  (lambda ()
    (setq indent-tabs-mode t)
    (setq python-indent 4)
    (setq tab-width 4))
    (tabify (point-min) (point-max)))
like image 40
Andreas Röhler Avatar answered Jan 14 '23 08:01

Andreas Röhler