2017-06-27 32 views
2

我正在使用LaTeX文档。我需要重新格式化我在vim中所有打开的缓冲区中拥有的小节标题。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}

到目前为止,我已经尝试了搜索字符串:

  1. %s/\\subsection{\(.\)\(\w*\)}/\\subsection{\u\1\L\2}/gc
  2. %s/\\subsection{\v<\(.\)\(\w*\)}/\\subsection{\u\1\L\2}/gc

数量1只匹配单个的词语,把它们转化成正确的格式。

2号不起作用。我的目标是将下面链接中SO回答中引用的序列与我的需求相结合,但我想我没有正确使用它。


,我尝试使用摸不着头脑的资源:

注:如果我要回去通过,并重新输入$\control字符如果替换也大写他们,我会没事的。

回答

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

这应该利用一切内部\subsection{....}块,遗憾的是包括数学序列里面,但它每次都会要求您进行确认。搜索比链接的“前一个问题”涉及更多一点:

\%( 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 
+0

很好。谢谢。 –

相关问题