Vim 用另一个相同长度的单词列表替换单词列表?
我需要用同样长的单词列表替换单词列表。
例如你有: "a","b","c","d","e","f"
并且您想将每个单词替换为每个单词的大写版本: “A”,“B”,“C”,“D”,“E”,“F”
我知道如何使用正则表达式查找每个字符串: (a\|b\|c\|d\|e\|f)
我知道你可以对每个单词进行全局替换。 但当单词长度变大时,这种方法就会变得笨拙且不优雅。
有没有办法以某种方式进行一次全局替换? 类似于:
:%s/\(a\|b\|c\|d\|e\|f\)/INSERT_REPLACEMENT_LIST/
我不确定这是否可能。
I need to substitute a list of words with with an equally long list of words.
So for example you have:
"a","b","c","d","e","f"
And you want to replace each word with the uppercase version of each word:
"A","B","C","D","E","F"
I know how to find each string using the regex:
(a\|b\|c\|d\|e\|f)
I know you could do a global substitution for each word. But when the length of the words gets large this approach would become un-wieldly and inelegant.
Is there a way to somehow do one global substitution? Similar to:
:%s/\(a\|b\|c\|d\|e\|f\)/INSERT_REPLACEMENT_LIST/
I am not sure if this is even possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用映射到其替换项的项目字典,然后在搜索/替换的右侧使用它。
请参阅
:h sub-replace-\=
和:h submatch()
。 如果你想把它塞进一行,你可以使用一本字面字典。您给出的大写字母的具体示例会更简单地完成为
You can use a dictionary of items mapped to their replacements, then use that in the right side of the search/replace.
See
:h sub-replace-\=
and:h submatch()
. If you want to cram that into one line, you could use a literal dictionary.The specific example you gave of uppercasing letters would be more simply done as
:%s/(a\|b\|c\|d\|e\|f)/\U\0/g
:%s/(a\|b\|c\|d\|e\|f)/\U\0/g