Vim,正则表达式在所有缓冲区中搜索和替换字母大小写更改
Vim, regex search and replace in all buffers with letter case changes
我正在处理 LaTeX 文档。我需要重新格式化 vim 中所有打开缓冲区中的小节标题。
通常,当我需要修改所有缓冲区时,我会在 vim 中执行类似 bufdo %s/pattern/replacement/g | update
的命令。这就是我打算再次做的。我需要的只是模式和替换字符串的帮助。
我需要匹配的字符串格式如下:
\subsection{word}
\subsection{some words}
\subsection{some Words CAPitalized weirdlY}
\subsection{and some with $\control$ sequences in them}
生成的搜索和替换应该会影响如下所示的字符串:
\subsection{Word}
\subsection{Some Words}
\subsection{Some Words Capitalized Weirdly}
\subsection{And Some With $\control$ Sequences In Them}
到目前为止,我尝试过的搜索字符串是:
%s/\subsection{\(.\)\(\w*\)}/\subsection{\u\L}/gc
。
%s/\subsection{\v<\(.\)\(\w*\)}/\subsection{\u\L}/gc
数字 1 只匹配单个单词并将它们转换为正确的格式。
数字 2 无效。我的目标是将来自以下链接的 SO 答案中引用的序列与我的需要结合起来,但我想我没有正确使用它。
我试图用来解决这个问题的资源:
- This answer to a previous question.
- and the vim wiki
注意:如果我必须返回并重新输入 $\control
个字符(如果替换也将它们大写),我可以接受。
bufdo %s/\%(\subsection{.*\)\@<=\%(.*}\)\@=\<\(\w\)\(\w*\)\>/\u\L/gc
这应该将 \subsection{....}
块内的所有内容都大写,不幸的是包括数学序列内的内容,但每次都会要求您确认。搜索比链接的 "previous question":
涉及更多
\%( non-capturing group
\...{ literal `\subsection{`
.* any number of characters
\) end of group
\@<= zero-width lookbehind
\%( non-capturing group
.* any number of characters
\) end of group
\@= zero-width lookahead
\< zero-width start of word
\( start of 1st group
\w word letter
\) end of group
\( start of 2nd group
\w* any number of letters
\) end of group
\> zero-width end of word
我正在处理 LaTeX 文档。我需要重新格式化 vim 中所有打开缓冲区中的小节标题。
通常,当我需要修改所有缓冲区时,我会在 vim 中执行类似 bufdo %s/pattern/replacement/g | update
的命令。这就是我打算再次做的。我需要的只是模式和替换字符串的帮助。
我需要匹配的字符串格式如下:
\subsection{word}
\subsection{some words}
\subsection{some Words CAPitalized weirdlY}
\subsection{and some with $\control$ sequences in them}
生成的搜索和替换应该会影响如下所示的字符串:
\subsection{Word}
\subsection{Some Words}
\subsection{Some Words Capitalized Weirdly}
\subsection{And Some With $\control$ Sequences In Them}
到目前为止,我尝试过的搜索字符串是:
%s/\subsection{\(.\)\(\w*\)}/\subsection{\u\L}/gc
。%s/\subsection{\v<\(.\)\(\w*\)}/\subsection{\u\L}/gc
数字 1 只匹配单个单词并将它们转换为正确的格式。
数字 2 无效。我的目标是将来自以下链接的 SO 答案中引用的序列与我的需要结合起来,但我想我没有正确使用它。
我试图用来解决这个问题的资源:
- This answer to a previous question.
- and the vim wiki
注意:如果我必须返回并重新输入 $\control
个字符(如果替换也将它们大写),我可以接受。
bufdo %s/\%(\subsection{.*\)\@<=\%(.*}\)\@=\<\(\w\)\(\w*\)\>/\u\L/gc
这应该将 \subsection{....}
块内的所有内容都大写,不幸的是包括数学序列内的内容,但每次都会要求您确认。搜索比链接的 "previous question":
\%( non-capturing group
\...{ literal `\subsection{`
.* any number of characters
\) end of group
\@<= zero-width lookbehind
\%( non-capturing group
.* any number of characters
\) end of group
\@= zero-width lookahead
\< zero-width start of word
\( start of 1st group
\w word letter
\) end of group
\( start of 2nd group
\w* any number of letters
\) end of group
\> zero-width end of word