Question

I'm having trouble getting Emacs to behave and set Python (or any environment) to 4-space tabs. Below is my .emacs file, and I've tried all the options under ;; --- Set python indent to 4 spaces ---, and none of them have worked. Is it possible there is some conflict or override happening with the indents?

Also, no matter how many times I set the tab-stop-list variables to be multiples of 4, they always end up as multiples of 8 when I use "Customize Emacs" from the menu and set the variables in the .emacs file.

I'm using GNU emacs for Mac OS X (http://emacsformacosx.com/).

(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.
 '(ansi-color-names-vector ["#242424" "#e5786d" "#95e454" "#cae682" "#8ac6f2" "#333366" "#ccaa8f" "#f6f3e8"])
 '(custom-enabled-themes (quote (deeper-blue)))
 '(indent-tabs-mode nil)
 '(inhibit-startup-screen t)
 '(python-guess-indent nil)
 '(python-honour-comment-indentation t)
 '(python-use-skeletons t)
 '(speedbar-indentation-width 2)
 '(tab-stop-list (quote (4 8 12 16 20 24 28 32 36 40 44 48 52 56 60))))
(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.
 '(default ((t (:inherit nil :stipple nil :background "#181a26" :foreground "gray80" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 120 :width normal :family "consolas"))))
 '(bold ((t (:foreground "white" :weight bold))))
 '(variable-pitch ((t (:family "Helvetica Neue")))))

(add-to-list 'load-path "~/.emacs.d/el-get/el-get")

(unless (require 'el-get nil 'noerror)
  (with-current-buffer
      (url-retrieve-synchronously
       "https://raw.github.com/dimitri/el-get/master/el-get-install.el")
    (let (el-get-master-branch)
      (goto-char (point-max))
      (eval-print-last-sexp))))

(el-get 'sync)

(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/") t)


;; Yet another snippet extension for emacs -------------------------------------
(add-to-list 'load-path
              "~/.emacs.d/plugins/yasnippet")
(require 'yasnippet)
(yas-global-mode 1)


;; Develop and keep personal snippets under ~/emacs.d/mysnippets
(setq yas/root-directory "~/.emacs.d/mysnippets")

;; Load the snippets
(yas-load-directory yas/root-directory)

;; Auto headers for files ---------------------------------------------------
(add-to-list 'load-path "~/.emacs.d/plugins/")
(require 'header2)
(add-hook 'emacs-lisp-mode-hook 'auto-make-header)


;; Invoke Emacs Speaks Statistics (ESS) for R
(require 'ess-site)

; automatically get the correct mode 
auto-mode-alist (append (list '("\\.c$" . c-mode)
                  '("\\.tex$" . latex-mode)
                  '("\\.S$" . S-mode)
                  '("\\.s$" . S-mode)
                  '("\\.R$" . R-mode)
                  '("\\.r$" . R-mode)
                  '("\\.html$" . html-mode)
                              '("\\.emacs" . emacs-lisp-mode)
                    )
              auto-mode-alist)

;; --- Automatically close all parens and quotes ---
(add-to-list 'load-path "~/.emacs.d/plugins/autopair")
(require 'autopair)
(autopair-global-mode) ;; enable autopair in all buffers


; Match Triple-quoting in python
(add-hook 'python-mode-hook
          #'(lambda ()
              (setq autopair-handle-action-fns
                    (list #'autopair-default-handle-action
                          #'autopair-python-triple-quote-action))))

;; --- Add autopairing of $ in latex ---
(add-hook 'latex-mode-hook
          #'(lambda ()
              (push '(?$ . ?$)
                    (getf autopair-extra-pairs :string))))


; Show column numbers
(setq column-number-mode t)

; --- Set python indent to 4 spaces ---
;; Set indent size to 4
(setq standard-indent 4)
(setq default-tab-width 4)

;; (add-hook 'python-mode-hook '(lambda () 
;;  (setq python-indent 4)))
;; (setq-default py-indent-offset 4)
;; (setq indent-tabs-mode t
;;       tab-width 4
;;       python-indent 4)

;; Python Hook
;; (add-hook 'python-mode-hook
;;           (function (lambda ()
;;                       (setq indent-tabs-mode nil
;;                             tab-width 4))))


;; --- Remove all tabs, convert them to spaces ---
(setq indent-tabs-mode nil)


;; --- Auto complete latex ---
Was it helpful?

Solution

From C-h v default-tab-width RET

This variable is obsolete since 23.2; use `tab-width' instead.

and for tab-width

Documentation:
*Distance between tab stops (for display of tab characters), in columns.
You can customize this variable.

When I insert

(custom-set-variables
 '(tab-width 4))

into ~/.emacs and restart emacs, it is set to 4.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top