emacs format

    格式化源码是很常见的需求,emacs有个indent-region函数用于格式化选定的代码,前提是你处在某个非text mode下,如c-mode或者java-mode之类。如果要格式化整个文件,你需要先选定整个文件(C-x-h),然后调用indent- region(或者 C-M- )。两个命令总是麻烦,我们可以定义个函数搞定这一切,并绑定在一个特定键上,实现一键格式化:

;;格式化整个文件函数
(defun indent
-whole ()
  (interactive)
  (indent
-region (point-min) (point-max))
  (message 
"format successfully"))
;;绑定到F7键
(global
-set-key [f7] 'indent-whole)


    将这段代码添加到你的emacs配置文件(~/.emacs),重启emacs,以后格式化源码都可以用F7一键搞定。

原文地址:https://www.cnblogs.com/jvava/p/3716655.html