如何在 vim 中使用正则表达式切换文件中所有字符的大小写

Nov*_*ice 2 regex linux vim replace

我有一个包含以下文本的文件

Lorem IpSuM is SIMPLY duMMy TeXt of the PrINtinG and typesetting industry.
Lorem Ipsum has been the industry's stanDaRd dummy text ever since the 1500s,
whEn an unknown printer took a galley of type and sCrAMBled it to MakE a type specimen BoOk.
Run Code Online (Sandbox Code Playgroud)

切换所有字符的大小写后,文本文件应如下所示:

lOREM iPsUm IS simply DUmmY tExT OF THE pRinTINg AND TYPESETTING INDUSTRY.
lOREM iPSUM HAS BEEN THE INDUSTRY'S STANdArD DUMMY TEXT EVER SINCE THE 1500S,
WHeN AN UNKNOWN PRINTER TOOK A GALLEY OF TYPE AND ScRambLED IT TO mAKe A TYPE SPECIMEN bOok.
Run Code Online (Sandbox Code Playgroud)

我正在使用以下正则表达式模式:

%s/\<\([A-Z\s\]+\)\>/\L&/
Run Code Online (Sandbox Code Playgroud)

但 vi 编辑器抛出错误:

E486: Pattern not found: \<\([A-Z\s\]+\)\>/\L&/
Run Code Online (Sandbox Code Playgroud)

在 vim 中使用正则表达式一次切换文件中所有字符的大小写的正确模式是什么?

Enr*_*lis 6

:s如果可以的话,为什么要使用正则表达式ggShift+vG~呢?

如果您必须在脚本中执行此操作,您可以使用:normal:

:normal ggVG~
Run Code Online (Sandbox Code Playgroud)

好吧,如果你真的想这么做:s,你可以这样做

%s/\a/\=submatch(0) >= 'a' && submatch(0) <= 'z' ? toupper(submatch(0)) : tolower(submatch(0))/g
Run Code Online (Sandbox Code Playgroud)

(我怀疑有一种方法可以写得submatch(0) >= 'a' && submatch(0) <= 'z'更简洁,但我不知道,或者我根本就没有想到。romainl的答案表明了这一点。)