如何在 emacs 中将分配和哈希的智能对齐添加到缩进区域?
当我有代码时:
a = 1
foo = 2
并且我想让它看起来像:
a = 1
foo = 2
我可以使用此处定义的align-to-equals函数:
-align-equal-signs">Emacs 热键通过选择区域并运行函数 。我可以通过将函数定义中的“=”更改为“=>”来更改该函数以适用于哈希值并具有:
bar = { :a => 1,
:foo => 2 }
转换为:
bar = { :a => 1,
:foo => 2 }
当我选择整个缓冲区并运行 indent-region 时,我希望对所有代码进行这种对齐。但我希望它能够智能地完成 - 不要将缓冲区中的每个“=”与最右边的“=”对齐,而是对赋值块和单个哈希文字进行对齐。
编辑:为了澄清最后一部分,假设我有以下缓冲区:
a = 1
foo = 2
some_other_code
def fn
bar = { :a => 1,
:foo => 2 }
end
我想做“Cx h”(选择整个缓冲区)、“MC-\”(缩进区域)并让它看起来像这样:
a = 1
foo = 2
some_other_code
def fn
bar = { :a => 1,
:foo => 2 }
end
When I have the code:
a = 1
foo = 2
and I want to make it look like:
a = 1
foo = 2
I can use the align-to-equals function defined here:
Emacs hotkey to align equal signs
by selecting the area and running the function. I can alter the function to work for hashes as well by changing the '=' in the function definition to '=>' and have:
bar = { :a => 1,
:foo => 2 }
be converted to:
bar = { :a => 1,
:foo => 2 }
I want this alignment to be done to all of my code when I select the whole buffer and run indent-region. But I want it to be done intelligently - not aligning every single '=' in the buffer to the rightmost '=', but instead doing it for assignment blocks and individual hash literals.
EDIT: To clarify this last part, say I have the following buffer:
a = 1
foo = 2
some_other_code
def fn
bar = { :a => 1,
:foo => 2 }
end
I want to do 'C-x h' (select whole buffer), 'M-C-\' (indent-region) and have it look like this:
a = 1
foo = 2
some_other_code
def fn
bar = { :a => 1,
:foo => 2 }
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
Mxalign-regexp
=
。对于其他人也是如此。Try
M-x align-regexp
=
. And same for others as well.align.el 为此提供了对齐函数:
在 .emacs 中添加
然后 Mxalign 将在 ruby 模式下对齐散列。您需要为您想要对齐的其他内容添加其他组。
align.el give the align function for this:
in .emacs add
then M-x align will align hashes in ruby-mode. You need to add other group for other thuings you want to align.