Emacs 重新缩进整个 C++缓冲

发布于 2024-10-01 04:28:56 字数 53 浏览 2 评论 0原文

我有一个 C++ 代码文件,但有相当难看的缩进。我如何告诉 emacs 对文件重新应用缩进?

i have a c++ code file but has pretty ugly indetation. How do i tell emacs to reapply indentation to the file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

单挑你×的.吻 2024-10-08 04:28:56

Cx h CM-\

这两个命令选择整个缓冲区并运行 indent-region

C-x h C-M-\

These two commands select the whole buffer and run indent-region.

黄昏下泛黄的笔记 2024-10-08 04:28:56

这是我放置在 ~/.emacs.d/defuns.el 文件中的“缩进整个缓冲区”代码。我采取了额外的步骤并将其绑定到快捷键 Cx \。这也将清除所有悬挂的空白,并将制表符转换为其空格等效表示。

(defun indent-buffer ()                                                                                                                                                            
  "Indents an entire buffer using the default intenting scheme."                                                                                                                   
  (interactive)                                                                                                                                                                    
  (point-to-register 'o)                                                                                                                                                           
  (delete-trailing-whitespace)                                                                                                                                                     
  (indent-region (point-min) (point-max) nil)                                                                                                                                      
  (untabify (point-min) (point-max))                                                                                                                                               
  (jump-to-register 'o))                                                                                                                                                           

(global-set-key "\C-x\\" 'indent-buffer)                                                                                                                                           

编辑,合并下面的 @JSONs 建议将为您提供一个看起来像这样的 defun:

(defun indent-buffer ()                                                                                                                                                            
  "Indents an entire buffer using the default intenting scheme."                                                                                                                   
  (interactive)                                                                                                                                                                    
  (save-excursion                                                                                                                                                                  
    (delete-trailing-whitespace)                                                                                                                                                   
    (indent-region (point-min) (point-max) nil)                                                                                                                                    
    (untabify (point-min) (point-max))))                                                                                                                                           

我对此进行了测试,它的工作原理就像以前一样。感谢您指出 JSON。

Here's the "indent entire buffer" code I place in my ~/.emacs.d/defuns.el file. I took the extra step and bound it to a quick key, C-x \. This one also will clear out all your hanging whitspace as well as convert tab characters into their space equivalent representation.

(defun indent-buffer ()                                                                                                                                                            
  "Indents an entire buffer using the default intenting scheme."                                                                                                                   
  (interactive)                                                                                                                                                                    
  (point-to-register 'o)                                                                                                                                                           
  (delete-trailing-whitespace)                                                                                                                                                     
  (indent-region (point-min) (point-max) nil)                                                                                                                                      
  (untabify (point-min) (point-max))                                                                                                                                               
  (jump-to-register 'o))                                                                                                                                                           

(global-set-key "\C-x\\" 'indent-buffer)                                                                                                                                           

Edit, incorporating @JSONs suggestion below will give you a defun that looks like this instead:

(defun indent-buffer ()                                                                                                                                                            
  "Indents an entire buffer using the default intenting scheme."                                                                                                                   
  (interactive)                                                                                                                                                                    
  (save-excursion                                                                                                                                                                  
    (delete-trailing-whitespace)                                                                                                                                                   
    (indent-region (point-min) (point-max) nil)                                                                                                                                    
    (untabify (point-min) (point-max))))                                                                                                                                           

I tested this out and it works just like before. Thanks for pointing that out JSON.

再见回来 2024-10-08 04:28:56

选择整个缓冲区并执行 Mx indent-region

请参阅 本指南

Select the entire buffer and do M-x indent-region

See this guide.

娜些时光,永不杰束 2024-10-08 04:28:56
(defun iwb ()
  "indent whole buffer"
  (interactive)
  (delete-trailing-whitespace)
  (indent-region (point-min) (point-max) nil)
  (untabify (point-min) (point-max)))

你可以使用这个小宏(我从 http://emacsblog.org 复制这个/2007/01/17/indent-whole-buffer/

(defun iwb ()
  "indent whole buffer"
  (interactive)
  (delete-trailing-whitespace)
  (indent-region (point-min) (point-max) nil)
  (untabify (point-min) (point-max)))

You can use this little macro(i copied this from http://emacsblog.org/2007/01/17/indent-whole-buffer/)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文