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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4177929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:15:49  来源:igfitidea点击:

How to change the indentation width in emacs javascript mode

javascriptemacsindentation

提问by mksuth

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 模式下更改此设置?

回答by Willi Ballenthin

js-indent-levelcan be used in the default javascript-mode, which is included by default starting in emacs 23.2.

js-indent-level可以在默认的 javascript-mode 中使用,默认情况下从 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. I think this mode responds to c-basic-offset, however I may be wrong.

应该做你正在寻找的。如果您使用的是较旧版本的 emacs,则您可能处于 java 模式。我认为这种模式会响应c-basic-offset,但是我可能是错的。

回答by wprl

I wish someone had told me about Custom a lot sooner! 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."

然后,选择“编程”,然后选择“语言”,然后选择要自定义的语言/模式。编辑您认为合适的选项。完成后,选择“保存当前会话”或“保存以备将来会话”。

回答by metakermit

If you're using js2-mode (which is more powerful IMHO), then the command is:

如果您使用的是 js2-mode(恕我直言更强大),则命令为:

(setq-default js2-basic-offset 2)

Source.

来源。

回答by Tyler

You might also want to set emacs to use spaces instead of tabs

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

(setq-default indent-tabs-mode nil)

回答by Helge

In my javascript.el file (/usr/share/emacs/site-lisp) I found

在我的 javascript.el 文件(/usr/share/emacs/site-lisp)中,我发现

javascript-indent-level 4

javascript 缩进级别 4

So if you are using the same lisp-mode you can change it by running

因此,如果您使用相同的 lisp 模式,则可以通过运行来更改它

(setq javascript-indent-level 2)

回答by kwarnke

You can also use

你也可以使用

M-x customize-variable

and type

并输入

js-indent-level

(js-[TAB] shows a list of options). Then change the Js Indent Levelas wanted and click [State:]and save.

js-[TAB] 显示选项列表)。然后Js Indent Level根据需要更改并单击[State:]并保存。

回答by hs0ucy

Using EditorConfigcould be a good idea too. And of course, Emacs had a mode for it editorconfig-emacs.

使用EditorConfig也可能是一个好主意。当然,Emacs 有一个模式editorconfig-emacs

It's also available in the package manager (M-x package-list-packages) through Melpa or Marmalade.

它也可以M-x package-list-packages通过 Melpa 或 Marmalade在包管理器 ( ) 中使用。

回答by MatthewD

If you want to change it on a per-file basis, put this at the top of your file:

如果您想在每个文件的基础上更改它,请将其放在文件的顶部:

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

回答by Malvineous

None of these solutions worked for me after upgrading to Emacs 26 (I already had js-indent-levelset to 2 but my tab width went back to 8), but what did work was setting the tab-widthvariable to 2, which seems to replace the old default-tab-widthvariable.

升级到 Emacs 26 后,这些解决方案都不适合我(我已经js-indent-level设置为 2,但我的选项卡宽度又回到了 8),但是有效的是将tab-width变量设置为2,这似乎取代了旧default-tab-width变量。

I found this in M-x customizeby searching for tab width.

M-x customize通过搜索找到了这个tab width