无论主要模式如何,如何强制使用空格而不是制表符?

发布于 2024-11-05 23:06:23 字数 203 浏览 1 评论 0原文

我希望所有制表符都是 4 个空格。我在 .emacs 中有以下内容,

(setq-default indent-tabs-mode nil)
(setq c-basic-indent 4)
(setq tab-width 4)

但这会被我可以使用的一些主要模式主题覆盖。有没有办法通过我的 .emacs 文件强制解决此问题?

I want all tabs to be 4 spaces. I have the following in .emacs

(setq-default indent-tabs-mode nil)
(setq c-basic-indent 4)
(setq tab-width 4)

But this gets overwritten by some of the major mode themes that I can use. Is there a way to force this issue through my .emacs file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝不服输 2024-11-12 23:06:23

尝试覆盖任何主要模式覆盖的内容:

(add-hook 'after-change-major-mode-hook 
          '(lambda () 
             (setq-default indent-tabs-mode nil)
             (setq c-basic-indent 4)
             (setq tab-width 4)))

请注意,不基于 c-mode 的主要模式不太可能关心 c-basic-indent 并且可能会使用自己的、特定于模式的缩进变量。在这种情况下,无法手动配置这些变量。

Try this to overwrite whatever any major mode overwrites:

(add-hook 'after-change-major-mode-hook 
          '(lambda () 
             (setq-default indent-tabs-mode nil)
             (setq c-basic-indent 4)
             (setq tab-width 4)))

Note though that major modes that aren't based on c-mode are not likely to care about c-basic-indent and may potentially use their own, mode-specific indentation variables. In such cases, there's no way around configuring these variables manually.

魔法少女 2024-11-12 23:06:23

声明默认的 C 缩进样式,而不是声明特定的样式参数。

(setq c-default-style "k&r2")  ;; or whatever your preference is
(set-default 'indent-tabs-mode nil)

Declare a default C indentation style, rather than declaring specific style parameters.

(setq c-default-style "k&r2")  ;; or whatever your preference is
(set-default 'indent-tabs-mode nil)
攒眉千度 2024-11-12 23:06:23

我用一个特别丑陋的黑客“解决”了这个问题。我没有尝试弄清楚如何获得正确的主要模式挂钩,而是执行了以下操作:

(defun save-buffer-without-tabs ()
  (interactive)
  (untabify (point-min) (point-max))
  (save-buffer))
(global-set-key "\C-x\C-s" 'save-buffer-without-tabs)

这严重破坏了一些东西(我关心的是,这些东西是 Python 和 Makefile)。因此,我还执行了以下操作:

;; restore the original save function for makefiles
(add-hook 'makefile-mode-hook
  (lambda ()
    (define-key makefile-mode-map "\C-x\C-s" 'save-buffer)))

;; restore the original save function for python files
(add-hook 'python-mode-hook
  (lambda () (define-key python-mode-map "\C-x\C-s" 'save-buffer)))

我不知道 Thomas 提到的 after-change-major-mode-hook,但如果他的解决方案由于某种原因不适合您,我已经使用这个几年了,没有发生任何事故。

编辑:经过仔细检查,我认为您提出的问题并不完全是我回答的问题。我想取消所有选项卡是获得一致缩进的一种方法。 :)

I "solved" this problem with a particularly ugly hack. Rather than try to figure out how to get the right major mode hooks in place, I just did the following:

(defun save-buffer-without-tabs ()
  (interactive)
  (untabify (point-min) (point-max))
  (save-buffer))
(global-set-key "\C-x\C-s" 'save-buffer-without-tabs)

This horribly breaks some things (that I care about, those things are Python and Makefiles). Thus, I also did the following:

;; restore the original save function for makefiles
(add-hook 'makefile-mode-hook
  (lambda ()
    (define-key makefile-mode-map "\C-x\C-s" 'save-buffer)))

;; restore the original save function for python files
(add-hook 'python-mode-hook
  (lambda () (define-key python-mode-map "\C-x\C-s" 'save-buffer)))

I wasn't aware of the after-change-major-mode-hook mentioned by Thomas, but if his solution doesn't work for you for some reason, I've been using this for a few years now without incident.

Edit: Upon closer inspection, I don't think you're asking exactly the question I answered. I guess nuking all the tabs is one way to get consistent indentation. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文