Vim、regex搜索和替换所有缓冲区中的字母大小写更改

时间:2020-12-22 16:49:26

I am working on a LaTeX document. I need to reformat the subsection titles that I have in all open buffers in vim.

我正在做一个乳胶文件。我需要重新格式化我在vim中所有开放缓冲区中的子节标题。

Normally, I do a command like bufdo %s/pattern/replacement/g | update inside of vim when I need to modify all buffers. That is what I intend to do again. All I need help with are the pattern and replacement strings.

通常,当需要修改所有缓冲区时,我在vim内部执行类似bufdo %s/pattern/replace /g |更新之类的命令。这就是我打算再做的事情。我所需要的只是模式和替换字符串。

The strings that I need to match have the following format:

我需要匹配的字符串有以下格式:

  • \subsection{word}
  • \分段{词}
  • \subsection{some words}
  • \分段{有些字}
  • \subsection{some Words CAPitalized weirdlY}
  • \分段{有些字大写古怪}
  • \subsection{and some with $\control$ sequences in them}
  • \分段{和一些带有$\控制$序列的

The resulting search and replace should affect the strings to be like below:

搜索和替换的结果将影响字符串如下:

  • \subsection{Word}
  • \分段{词}
  • \subsection{Some Words}
  • \分段{有些字}
  • \subsection{Some Words Capitalized Weirdly}
  • \分段{有些字大写古怪}
  • \subsection{And Some With $\control$ Sequences In Them}
  • \分段{和一些带有$\控件$序列的}

So far the search strings that I have tried are:

到目前为止,我尝试的搜索字符串是:

  1. %s/\\subsection{\(.\)\(\w*\)}/\\subsection{\u\1\L\2}/gc.
  2. % s / \ \分段{ \(\)\ \)(\ w * } / \ \分段{ \ u L \ 1 \ \ 2 } / gc。
  3. %s/\\subsection{\v<\(.\)\(\w*\)}/\\subsection{\u\1\L\2}/gc
  4. % s / \ \分段{ \ v < \(\)\ \ w * \ } / \ \分段{ \ u L \ 1 \ \ 2 } / gc

Number 1 only matches single words and turns them into the correct format.

数字1只匹配单个单词并将它们转换成正确的格式。

Number 2 does not work. My goal was to combine the sequence referenced in the SO answer from the links below with my needs but I guess I am not using it correctly.

第二点不管用。我的目标是将下面链接中SO答案中引用的序列与我的需要结合起来,但我想我没有正确地使用它。


The resources that I am trying to use to figure this out:

我试图用这些资源来解决这个问题

Note: If I have to go back through and retype the $\control characters if the replacement also capitalizes them, I would be ok with that.

注意:如果我需要重新输入$\control字符(如果替换字符也是大写的),我可以接受。

1 个解决方案

#1


2  

bufdo %s/\%(\\subsection{.*\)\@<=\%(.*}\)\@=\<\(\w\)\(\w*\)\>/\u\1\L\2/gc

This should capitalise everything inside \subsection{....} blocks, unfortunately including inside math sequences, but it will ask you for confirmation each time. The search is a bit more involved than in the linked "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

#1


2  

bufdo %s/\%(\\subsection{.*\)\@<=\%(.*}\)\@=\<\(\w\)\(\w*\)\>/\u\1\L\2/gc

This should capitalise everything inside \subsection{....} blocks, unfortunately including inside math sequences, but it will ask you for confirmation each time. The search is a bit more involved than in the linked "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