开发者

Emacs reindenting entire C++ buffer

i have a c++ code file but has pretty ugly indetation. Ho开发者_StackOverfloww do i tell emacs to reapply indentation to the file?


C-x h C-M-\

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


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.


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

See this guide.


(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/)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜