vi(m) 中的驼峰式下划线
如果由于某种原因我想在 Vim 中选择性地将驼峰命名法转换为下划线分隔,我该怎么做呢?
目前我发现我可以进行搜索 /s[az][AZ]
并记录一个宏来添加下划线并转换为小写,但我很好奇是否可以使用以下内容执行此操作:
%s/([az])([AZ])/\1\u\2/gc
提前致谢!
编辑:我找到了camelCase的答案(这是我真正需要的),但是其他人可以回答如何将CamelCase更改为camel_case吗?
If for some reason I want to selectively convert camelCase named things to being underscore separated in Vim, how could I go about doing so?
Currently I've found that I can do a search /s[a-z][A-Z]
and record a macro to add an underscore and convert to lower case, but I'm curious as to if I can do it with something like:
%s/([a-z])([A-Z])/\1\u\2/gc
Thanks in advance!
EDIT: I figured out the answer for camelCase (which is what I really needed), but can someone else answer how to change CamelCase to camel_case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可能想尝试 Tim Pope 的 Abolish 插件。它提供了一些强制从一种风格到另一种风格的快捷方式。例如,从以下内容开始:
输入
crc
[助记符:CoeRce to Camelcase] 将为您提供:输入
crs
[mnemonic: CoeRce to Snake_case] 将为您提供:并输入
crm
[助记符:CoeRce 到 MixedCase] 会带您返回:如果您还安装了 repeat.vim,然后你可以通过按点键重复强制命令。
You might want to try out the Abolish plugin by Tim Pope. It provides a few shortcuts to coerce from one style to another. For example, starting with:
Typing
crc
[mnemonic: CoeRce to Camelcase] would give you:Typing
crs
[mnemonic: CoeRce to Snake_case] would give you:And typing
crm
[mnemonic: CoeRce to MixedCase] would take you back to:If you also install repeat.vim, then you can repeat the coercion commands by pressing the dot key.
这有点长,但似乎可以完成工作:
:%s/\<\u\|\l\u/\= join(split(tolower(submatch(0)), '\zs' ), '_')/gc
This is a bit long, but seems to do the job:
:%s/\<\u\|\l\u/\= join(split(tolower(submatch(0)), '\zs'), '_')/gc
我想我应该再继续尝试大约 5 分钟。好吧...如果有人好奇:
%s/\(\l\)\(\u\)/\1\_\l\2/gc
就可以了。实际上,我意识到这适用于camelCase,但不适用于PascalCase,这对某人也可能有用。
I suppose I should have just kept trying for about 5 more minutes. Well... if anyone is curious:
%s/\(\l\)\(\u\)/\1\_\l\2/gc
does the trick.Actually, I realized this works for camelCase, but not PascalCase, which could also be useful for someone.
我制作了一个插件来执行此操作。
https://github.com/chiedojohn/vim-case-convert
转换大小写,在可视模式下选择一段文本,然后输入以下内容之一(不言自明):
: 骆驼转连字符
:骆驼对蛇
:连字符转骆驼
:连字符转蛇
:蛇对骆驼
:蛇到连字符
要转换文档中的所有场合,请运行以下命令之一:
:CamelToHyphenAll
:骆驼到蛇全部
:连字符转骆驼全部
:连字符转蛇全部
:蛇到骆驼全部
:蛇到连字符
在上述任何命令中添加感叹号(例如:CamelToHyphen!)以绕过每次转换之前的提示。
您可能不想这样做,因为插件不知道文件中变量或其他文本之间的差异。
I whipped up a plugin that does this.
https://github.com/chiedojohn/vim-case-convert
To convert the case, select a block of text in visual mode and the enter one of the following (Self explanatory) :
:CamelToHyphen
:CamelToSnake
:HyphenToCamel
:HyphenToSnake
:SnakeToCamel
:SnakeToHyphen
To convert all occerences in your document then run one of the following commands:
:CamelToHyphenAll
:CamelToSnakeAll
:HyphenToCamelAll
:HyphenToSnakeAll
:SnakeToCamelAll
:SnakeToHyphen
Add a bang (eg. :CamelToHyphen!) to any of the above command to bypass the prompts before each conversion.
You may not want to do that though as the plugin wouldn't know the different between variables or other text in your file.
对于驼峰式情况:
提示:可以像我的示例中那样更改正则表达式分隔符,以使其(在某种程度上)更清晰。
For the CamelCase case:
Tip: the regex delimiters can be altered as in my example to make it (somewhat) more legible.
对于
camelCase
:您可能需要多次运行此命令。
For the
camelCase
:You may need to run this command multi times.
我有一个用于各种面向开发的处理的API。除此之外,它还提供了一些用于在(可配置)约定之间转换名称的函数(变量 <-> 属性 <-> getter <-> setter <-> 常量 <-> 参数 < ;-> ...) 和样式(驼峰式(低/高)<-> 下划线)。这些转换函数已被封装到插件中。
插件 + API 可以从这里获取:https://github.com/LucHermitte/lh-dev,对于这个名字转换任务,需要 lh-vim-lib
即可使用方法如下:
:NameConvert
+ 您希望的转换类型(此处为:下划线
)。注意:该命令支持自动完成。I have an API for various development oriented processing. Among other things, it provides a few functions for transforming names between (configurable) conventions (variable <-> attribute <-> getter <-> setter <-> constant <-> parameter <-> ...) and styles (camelcase (low/high) <-> underscores). These conversion functions have been wrapped into a plugin.
The plugin + API can be fetch from here: https://github.com/LucHermitte/lh-dev, for this names conversion task, it requires lh-vim-lib
It can be used the following way:
:NameConvert
+ the type of conversion you wish (here :underscore
). NB: this command supports auto-completion.