首页 > 解决方案 > 禁止 emacs 将制表符转换为空格

问题描述

我最近开始使用 Emacs,遇到的一个问题是编辑器会自动将所有制表符转换为空格。现在开始有点烦了。

这是我的.emacs文件供参考:

(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
                    (not (gnutls-available-p))))
       (proto (if no-ssl "http" "https")))
  (when no-ssl
    (warn "\
Your version of Emacs does not support SSL connections,
which is unsafe because it allows man-in-the-middle attacks.
There are two things you can do about this warning:
1. Install an Emacs version that does support SSL and be safe.
2. Remove this warning from your init file so you won't see it again."))
  ;; Comment/uncomment these two lines to enable/disable MELPA and MELPA Stable as desired
  (add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t)
  ;;(add-to-list 'package-archives (cons "melpa-stable" (concat proto "://stable.melpa.org/packages/")) t)
  (when (< emacs-major-version 24)
    (add-to-list 'package-archives (cons "gnu" (concat proto "://elpa.gnu.org/packages/")))))
(package-initialize)

(custom-set-variables
 '(custom-enabled-themes (quote (dracula)))
 '(custom-safe-themes)
 '(display-line-numbers-type (quote relative))
 '(global-display-line-numbers-mode t)
 '(menu-bar-mode nil)
 '(package-selected-packages
   (quote
    (company-irony-c-headers company-irony micgoline elpy company-jedi molokai-theme gruvbox-theme autopair auto-complete anaconda-mode nyan-mode dracula-theme company)))
 '(scroll-bar-mode nil)
 '(tool-bar-mode nil))
(custom-set-faces
 )

(setq make-backup-files nil)
(setq auto-save-default nil)
(setq inhibit-startup-message t)   ;; hide the startup message

(elpy-enable)
(pyenv-mode)
(setq python-shell-interpreter "ipython"
      python-shell-interpreter-args "-i --simple-prompt")
(require 'powerline)

有关如何阻止 emacs 执行此行为的任何建议?

标签: emacsindentationauto-indentspacemacs

解决方案


正如 Drew 所建议的,我将其发布为答案:

你检查变量了indent-tabs-mode吗?

有了这个,您应该能够使用空格或制表符在 emacs 之间切换。

正如这里的 emacs wiki 中所述,我假设,某些活动模式在您的 emacs 中将其设置为 nil。您可以在此处找到另一种解释,其中包含有关选项卡是否邪恶的讨论链接

编辑:奇怪的是,python-mode 设置indent-tabs-mode为 t。也许这个 Emacs Wiki条目可以解决您的问题。来自维基的这个片段:

(add-hook 'python-mode-hook
    (lambda ()
        (setq-default indent-tabs-mode t)
        (setq-default tab-width 4)
        (setq-default py-indent-tabs-mode t)
    (add-to-list 'write-file-functions 'delete-trailing-whitespace)))

看起来它会成功的。希望这可以帮助。


推荐阅读