在 emacs 中对齐或美化代码
我记得这在 emacs 中是可能的,但不知道如何实现。如果我有这样的东西:
'abc' => 1, 'abcabc' =>2, 'abcabcabc' => 3,
如何将键、箭头和值与这样的东西对齐?
'abc' => 1, 'abcabc' => 2, 'abcabcabc' => 3,
干杯
I remember this was possible in emacs, but don't know how. If I have something like:
'abc' => 1, 'abcabc' =>2, 'abcabcabc' => 3,
How can I align the keys, arrows and values to something like this?
'abc' => 1, 'abcabc' => 2, 'abcabcabc' => 3,
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选择区域。
输入
Mxalign-regexp RET
输入
=
并按 Enter 键。< /p>Select the region.
Type
M-x align-regexp RET
Type
=
and hit enter.您还可以使用
align
命令代替align-regexp
。不同之处在于,align
根据缓冲区的主模式自动选择要使用的正则表达式。因此,如果您尝试在 c 模式文件中对齐变量初始化和赋值块,那么它将自动执行正确的操作,而无需您考虑所需的正则表达式。可以方便。例如,选择以下行:
并输入
Mxalign RET
。结果是:不过,我应该补充一点,这并不总是有效。如果没有为当前缓冲区的主模式定义正则表达式,则对
align
的调用将不会执行任何操作。然后,您需要依靠align-regexp
。但这并不是什么大的不便。实际上,我相当频繁地使用align-regexp
。为了方便起见,我定义了一个别名来节省一些击键次数:You can also use the
align
command instead ofalign-regexp
. The difference is thatalign
automatically chooses the regular expression(s) to use based on the major-mode of the buffer. So if you are trying to align a block of variable initializations and assignments in a c-mode file, then it will automatically do the right thing without you needing to think of the regular expressions which are needed. Can be convenient.For example select the following lines:
And type
M-x align RET
. The result is:I should add, though, that this will not always work. If there are no regular expressions defined for the major-mode of the current buffer, then the call to
align
will do nothing. Then, you need to fall back onalign-regexp
. But this is hardly a large inconvenience. I actually usealign-regexp
fairly frequently. For convenience, I have defined an alias to save myself a few key-strokes: