如何在 emacs javascript 模式下更改缩进宽度
我想使用 2 个空格而不是 4 个空格来缩进。我可以使用以下方法更改 C 模式的默认行为:
(setq c-basic-offset 2)
How do I change this in javascript mode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
js-indent-level
可以在默认的 javascript 模式下使用,从 emacs 23.2 开始默认包含该模式。应该做你正在寻找的事情。如果您使用的是旧版本的 emacs,您可能处于 java 模式。我认为这种模式响应
c-basic-offset
,但我可能是错的。js-indent-level
can be used in the default javascript-mode, which is included by default starting in emacs 23.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.我希望有人早点告诉我有关定制的事情!也许这会帮助像我这样的普通人;)
调用自定义:
然后,选择“编程”,然后选择“语言”,然后选择要自定义的语言/模式。根据需要编辑选项。完成后,选择“保存用于当前会话”或“保存用于未来会话”。
I wish someone had told me about Custom a lot sooner! Perhaps this will help another mere mortal like me ;)
Invoke Custom:
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."
如果您使用 js2-mode (恕我直言,这是更强大的),那么命令是:
来源。
If you're using js2-mode (which is more powerful IMHO), then the command is:
Source.
您可能还想将 emacs 设置为使用空格而不是制表符
You might also want to set emacs to use spaces instead of tabs
您还可以使用
并键入
(
js-
[TAB] 显示选项列表)。然后根据需要更改Js 缩进级别
,然后单击[State:]
并保存。You can also use
and type
(
js-
[TAB] shows a list of options). Then change theJs Indent Level
as wanted and click[State:]
and save.在我的 javascript.el 文件 (/usr/share/emacs/site-lisp) 中,我发现
javascript-indent-level 4
因此,如果您使用相同的 lisp-mode,您可以通过运行来更改它
In my javascript.el file (/usr/share/emacs/site-lisp) I found
javascript-indent-level 4
So if you are using the same lisp-mode you can change it by running
使用 EditorConfig 也是一个好主意。当然,Emacs 有一个模式 editorconfig-emacs。
也可以通过 Melpa 或 Marmalade 在包管理器 (
Mx package-list-packages
) 中使用它。Using EditorConfig could be a good idea too. And of course, Emacs had a mode for it editorconfig-emacs.
It's also available in the package manager (
M-x package-list-packages
) through Melpa or Marmalade.如果您想在每个文件的基础上更改它,请将其放在文件的顶部:
If you want to change it on a per-file basis, put this at the top of your file:
升级到 Emacs 26 后,这些解决方案都不适合我(我已经将 js-indent-level 设置为 2,但我的制表符宽度又回到了 8),但有效的是设置>tab-width 变量更改为
2
,这似乎取代了旧的default-tab-width
变量。我通过搜索
制表符宽度
在Mxcustomize
中找到了这个。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 thetab-width
variable to2
, which seems to replace the olddefault-tab-width
variable.I found this in
M-x customize
by searching fortab width
.