emacs - 在 emacs 中自动格式化源文件

如何将一组格式规则应用于 emacs 中的现有源文件?

具体来说,我有一个程序集 (*.s) 文件,但我想要一个适用于所有类型文件的通用命令。

我正在尝试将 M-x c-set-stylegnu 样式一起使用,但出现错误:

Buffer *.s is not a CC Mode buffer (c-set-style)

最佳答案

打开文件,然后通过缩进整个区域来缩进它:

M-x find-file /path/to/file RET
C-x h                             (M-x mark-whole-buffer)
C-M-\                             (M-x indent-region)

现在,您似乎正在尝试将 C 缩进应用到不在 C 模式下的缓冲区。让它进入C模式

M-x c-mode

c++-mode,或任何你想要的模式。但是,由于它是汇编代码,您可能需要汇编模式(默认情况下,Emacs 将为 .s 文件执行该模式)。在这种情况下,上面的缩进命令(C-M-\ 也称为 M-x indent-region)应该适合你。

注意:顶部的命令序列可以像这样滚动成单个命令:

(defun indent-file (file)
  "prompt for a file and indent it according to its major mode"
  (interactive "fWhich file do you want to indent: ")
  (find-file file)
  ;; uncomment the next line to force the buffer into a c-mode
  ;; (c-mode)
  (indent-region (point-min) (point-max)))

而且,如果您想了解如何将主要模式与基于扩展名的文件相关联,请查看 documentation for auto-mode-alist .公平地说,它不一定是基于扩展名的,只是与文件名匹配的正则表达式。

https://stackoverflow.com/questions/992685/

相关文章:

c# - 如何将 bool 值一般格式化为是/否字符串?

visual-studio-2010 - 在 ReSharper for Visual Studio

c# - 在 Resharper 中为对象和数组初始化程序获取正确的缩进

java - 如何在 Java 中计算时间跨度并格式化输出?

java - 最好的 Eclipse 代码格式化程序?

python - 将整数格式化为十六进制字符串

c - 什么是 scanf ("%*s") 和 scanf ("%*d") 格式标识符?

c - 在 C 中打印十六进制的前导零

python - 使用 Python 减去两个 UNIX 时间戳时,如何产生人类可读的差异?

json - 如何在传出的 JSON 中格式化时间戳