「GNU Emacs」- 编辑文件

问题描述

该笔记将记录:在 GNU Emacs 中,常用文件编辑,比如代码格式化、多行排序等等方法。

解决方案

对齐等号

在使用 GNU Emacs 编辑时,我们需要进行代码格式化,以增强代码的可读性。使用 M-x align-regexp 函数,进行代码对齐,这里可以输入自定义对齐符号。比如,对齐等号(=)。

多行排序(字母,数字)

对于根据字母排序,使用如下方法:
1)选中多行
2)然后对多行进行排序:

字母顺序排序:M-x sort-lines [RET];

字母顺序反向排序:C-u M-x sort-lines [RET]

对于数字排序,使用那个如下方法:
1)选中多行
2)然后对多行进行排序:

顺序排序: M-x sort-numeric-fields [RET];

反序排序:在顺序排序之后,执行 M-x reverse-region 以反序;

根据特定列排序:C-u 3 M-x sort-numeric-fields

参考文献

EmacsWiki: Reformat Buffer
formatting – In Emacs, how to line up equals signs in a series of initialization statements?
Sort lines in a region | Pragmatic Emacs