简体   繁体   English

如何在emacs javascript模式下更改缩进宽度

[英]How to change the indentation width in emacs javascript mode

I'd like to use 2 spaces for my indents instead of 4. I can change the default behavior of C mode using: 我想为我的缩进使用2个空格而不是4.我可以使用以下命令更改C模式的默认行为:

(setq c-basic-offset 2)

How do I change this in javascript mode? 如何在javascript模式下更改此设置?

js-indent-level can be used in the default javascript-mode, which is included by default starting in emacs 23.2. js-indent-level可以在默认的javascript模式下使用,默认包含在emacs 23.2中。

(setq js-indent-level 2)

should do what you're looking for. 应该做你想要的。 If you're using an older version of emacs, you may be in java-mode. 如果您使用的是较旧版本的emacs,则可能处于java模式。 I think this mode responds to c-basic-offset , however I may be wrong. 我认为这种模式会响应c-basic-offset ,但我可能错了。

I wish someone had told me about Custom a lot sooner! 我希望有人早点告诉我有关Custom的事情! Perhaps this will help another mere mortal like me ;) 也许这会帮助像我这样的另一个凡人;)

Invoke Custom: 调用自定义:

M-x customize

Then, choose "Programming," and then "Languages," and then select a language/mode to customize. 然后,选择“编程”,然后选择“语言”,然后选择要自定义的语言/模式。 Edit the options as you see fit. 根据需要编辑选项。 When done, choose either "Save for current session" or "Save for future sessions." 完成后,选择“保存当前会话”或“保存以备将来会话”。

If you're using js2-mode (which is more powerful IMHO), then the command is: 如果你正在使用js2-mode(更强大的恕我直言),那么命令是:

(setq-default js2-basic-offset 2)

Source. 资源。

您可能还希望将emacs设置为使用空格而不是制表符

(setq-default indent-tabs-mode nil)

In my javascript.el file (/usr/share/emacs/site-lisp) I found 在我的javascript.el文件(/ usr / share / emacs / site-lisp)中,我找到了

javascript-indent-level 4 javascript-indent-level 4

So if you are using the same lisp-mode you can change it by running 因此,如果您使用相同的lisp模式,则可以通过运行来更改它

(setq javascript-indent-level 2)

You can also use 你也可以使用

M-x customize-variable

and type 并输入

js-indent-level

( js- [TAB] shows a list of options). js- [TAB]显示选项列表)。 Then change the Js Indent Level as wanted and click [State:] and save. 然后根据需要更改Js Indent Level ,然后单击[State:]并保存。

如果要基于每个文件更改它,请将其放在文件的顶部:

// -*- mode: js; js-indent-level: 2; -*-

Using EditorConfig could be a good idea too. 使用EditorConfig也是一个好主意。 And of course, Emacs had a mode for it editorconfig-emacs . 当然,Emacs有一个模式为editorconfig-emacs

It's also available in the package manager ( Mx package-list-packages ) through Melpa or Marmalade. 它也可以通过Melpa或Marmalade在包管理器( Mx package-list-packages )中使用。

None of these solutions worked for me after upgrading to Emacs 26 (I already had js-indent-level set to 2 but my tab width went back to 8), but what did work was setting the tab-width variable to 2 , which seems to replace the old default-tab-width variable. 升级到Emacs 26之后,这些解决方案都没有适用于我(我已经将js-indent-level设置为2,但我的标签宽度又回到了8),但是工作的是将tab-width变量设置为2 ,这似乎是替换旧的default-tab-width变量。

I found this in Mx customize by searching for tab width . 我通过搜索tab widthMx customize找到了这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM