Emacs 缩进级别全局覆盖

发布于 2024-11-26 19:14:10 字数 816 浏览 2 评论 0原文

我想将缩进模式设置为仅制表符,任何模式的缩进宽度均为 4 个字符。这看起来是一件微不足道的事情,但我还没有成功。每种模式似乎都有自己的变量和选项。我尝试为 Perl 和 R 执行此操作,但没有成功。 没有起作用的事情:

(setq-default tab-width 4)
(setq standard-indent 4)
(setq-default r-indent-level 4)
(setq perl-indent-level 4)

(setq c-basic-offset 4) 适用于 c 模式,但没有其他效果。我是不是忘记了什么?我是否设置了错误的变量?没有这样的选择吗?

我每天都会使用多种语言(R、Perl、sh、C/C++ 等)。由于我喜欢在不同语言中使用相同的缩进,是否可以设置这样一个全局覆盖变量,以便缩进级别和样式在所有模式下保持一致?如果没有,有没有办法在启动时为每种模式设置它们?如果所有其他方法都失败,则必须有一个 elisp 脚本来执行此操作。

使用 Emacs 23


已解决:我必须单独设置每种模式的变量,因为没有这样的全局覆盖。您可以将以下语句放入 ~/.emacs 文件中以在启动时配置 emacs。

R 模式来自 ESS 包。通读文档,我发现了这个: (setq ess-indent-level 4)

In CPerl mode (setq cperl-indent-level 4)

看起来你只需必须在每种模式下搜索正确的变量。

I want to set the indentation mode to tabs only, with a 4 character width for any mode. This seems like a trivial thing, but I have not had success. Every mode seems to have its own variables and options. I've tried doing this for Perl and R without success.
Things that have not worked:

(setq-default tab-width 4)
(setq standard-indent 4)
(setq-default r-indent-level 4)
(setq perl-indent-level 4)

(setq c-basic-offset 4) works for c-mode but nothing else. Am I forgetting something? Did I set the wrong variables? Is there no such option?

I work with a variety of languages (R, Perl, sh, C/C++ etc.) on a daily basis. Since I like to use the same indentation across languages, is there such a global override variable that I can set so that the indentation level and style is consistent across all modes? If not, is there a way to set them for each mode on startup? If all else fails, there has to be an elisp script that does this.

Using Emacs 23


RESOLVED: I had to set the variables for each mode individually because there is no such global override. You can put the following statements in your ~/.emacs file to configure emacs on startup.

R mode comes from the ESS package. Reading through the documentation, I found this: (setq ess-indent-level 4)

In CPerl mode (setq cperl-indent-level 4)

Looks like you'll just have to search for the right variable in each mode.

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

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

发布评论

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

评论(3

时光是把杀猪刀 2024-12-03 19:14:10

Emacs 中的缩进并不是一件“小事”。您可以在 Emacs Wiki 上阅读所有相关内容:
http://www.emacswiki.org/emacs/CategoryIndentation

任何主要模式都可以随意实现缩进,并且,正如您所注意到的,其中一些引入了与缩进相关的变量;所以不,不存在保证影响每个可能的主要模式的全局缩进配置(尽管在实践中,某些变量按照惯例完全是标准的)。

如果没有,有没有办法在启动时为每种模式设置它们?

当然。最简单的方法是使用 Mx customize RET 接口配置值和默认值,尽管只有使用 defcustom 定义的变量出现在那里,所以它不一定全面(但它对于浏览一些可用设置仍然非常有用,即使您实际上并不使用它来设置值)。

正如您所做的那样,使用 setqsetq-default 在初始化文件中设置值(或者在自动缓冲区局部变量的情况下设置默认值)也可以。

如果您想要更多控制,可以使用模式挂钩。几乎每种模式在缓冲区中初始化后都会运行分配给 (mode-name)-hook 变量的函数列表,因此任何特定于模式的自定义都可以在 elisp 函数中编写并添加到适当的钩子列表,在你的初始化文件中。

例如:

(defun my-c-mode-config ()
  (whitespace-mode 1)
  (setq indent-tabs-mode t
        tab-width        4
        c-basic-offset   4))

(add-hook 'c-mode-hook 'my-c-mode-config)

Indentation in Emacs isn't really a "trivial thing". You can read all about it at the Emacs Wiki:
http://www.emacswiki.org/emacs/CategoryIndentation

Any major mode is free to implement indentation however it wishes and, as you've noticed, several of them introduce indentation-related variables; so no, there is no global indentation configuration which is guaranteed to affect every possible major mode (although in practice, certain variables are completely standard by convention).

If not, is there a way to set them for each mode on startup?

Of course. The easiest way is to configure values and defaults using the M-x customize RET interface, although only variables defined with defcustom appear there, so it isn't necessarily comprehensive (but it can still be very useful for browsing some of the available settings, even if you don't actually use it to set the values).

Setting values (or defaults in the case of automatically buffer-local variables) in your init file with setq and setq-default, as you've done, is also fine.

If you want more control, you can use mode hooks. Pretty much every mode runs the list of functions assigned to the (mode-name)-hook variable after initialising itself in a buffer, so any mode-specific customisations can be written in an elisp function and added to the appropriate hook list, in your init file.

e.g.:

(defun my-c-mode-config ()
  (whitespace-mode 1)
  (setq indent-tabs-mode t
        tab-width        4
        c-basic-offset   4))

(add-hook 'c-mode-hook 'my-c-mode-config)
江南月 2024-12-03 19:14:10

使用变量standard-indent。您可以在启动文件中设置它,或者自定义它;它位于缩进组中。执行Mxcustomize,然后选择编辑,然后缩进;或者,执行Mxcustomize-group indent

至于使用制表符而不是空格进行缩进,您只需将 indent-tabs-mode 设置为 t 即可。可以用同样的方式定制。

Use the variable standard-indent. You can set it in your startup file, or customize it; it's in the Indent group. Do M-x customize, then choose Editing, then Indent; alternatively, do M-x customize-group indent.

As for indenting with tabs instead of spaces, all you have to do is set indent-tabs-mode to t. It's customizable the same way.

昨迟人 2024-12-03 19:14:10

(setq default-tab-width 2) 在 emacs 24 中对我有用

(setq default-tab-width 2) works for me in emacs 24

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