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

标签 emacs formatting

如何将一组格式规则应用于 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 .公平地说,它不一定是基于扩展名的,只是与文件名匹配的正则表达式。

关于emacs - 在 emacs 中自动格式化源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/992685/

相关文章:

emacs - Emacs 是否可以运行位于远程服务器上的 Matlab shell?

emacs - 强制表排序

python - 打印超出其精度的 float

c# - 自动格式化 t4 创建的文件

java - 文件夹名称中的最后一个字符无法替换

emacs - org-mode: native 字体化代码块

emacs - 为什么 Emacs 项目 C-c p 未定义?

emacs - 等待编译完成

java - 如何在 IntelliJ 中停止流式 API 的扁平化

objective-c - 为 NSString 添加双引号