使用搜索和替换将内容移到大括号命令之外 (Vim)

发布于 2025-01-06 00:47:07 字数 277 浏览 2 评论 0原文

我有这样的命令:

\bold{Foo Bar}
\bold{Foo Bars}
\bold{Foos Bar}
....
\bold{Zoo Cars}

并且我想将它们变成

Foo Bar
Foo Bars
Foos Bar
...
Zoo Cars

:%s/\bold{*}//gc 甚至找不到任何匹配项。我该怎么做?

注意:我到处搜索,但没有任何搜索有帮助。

I have commands like this:

\bold{Foo Bar}
\bold{Foo Bars}
\bold{Foos Bar}
....
\bold{Zoo Cars}

and I want to turn them into

Foo Bar
Foo Bars
Foos Bar
...
Zoo Cars

:%s/\bold{*}//gc does not even find any matches. How do I do this?

Note: I googled all around but none of the searches helped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

路弥 2025-01-13 00:47:07

更改分隔符搜索,用逗号交换栏。轻松查看

%s,\\bold{\([^}]*\)},\1,g

whole file .............. %
swap .................... s
, ....................... delimiter search
\\bold{ ................. literal \bold{
\( ...................... open group one
[^}]* ................... all except }
\) ...................... close group one
, ....................... open swap (close search)
\1 ...................... back reference to group one
, ....................... close swap
g ....................... global

change the delimiter searches, swapping bar by comma. Easy view

%s,\\bold{\([^}]*\)},\1,g

whole file .............. %
swap .................... s
, ....................... delimiter search
\\bold{ ................. literal \bold{
\( ...................... open group one
[^}]* ................... all except }
\) ...................... close group one
, ....................... open swap (close search)
\1 ...................... back reference to group one
, ....................... close swap
g ....................... global
寂寞陪衬 2025-01-13 00:47:07

您正在编写一个正则表达式,但您认为它是一个全局变量。您也根本不提供替代品。尝试 :%s/\\bold{\(.*\)}/\1/gc

You're writing a regexp, but you think it's a glob. You're also not providing a replacement at all. Try :%s/\\bold{\(.*\)}/\1/gc.

心欲静而疯不止 2025-01-13 00:47:07

你可以用两种方法(至少)

(你需要转义粗体前面的“\”字符)

  1. 我的建议,分两次完成

    %s/\\bold{/gc 
    %s/}//GC
    
  2. 如果你想一次性完成

    <前><代码>%s/\\粗体{\(.*\)}/\1/

另外,你真的需要最后的“gc”吗?根据您的样本数据并非如此。

我希望这有帮助。

you can do it 2 ways (at least)

(you need to escape the '\' char in front of bold)

  1. my recommendation, do it in 2 passes

    %s/\\bold{/gc 
    %s/}//gc
    
  2. if you want to do it all at once

    %s/\\bold{\(.*\)}/\1/
    

Also, do you really need the 'gc' at the end? Not so based on your sample data.

I hope this helps.

诗化ㄋ丶相逢 2025-01-13 00:47:07

我知道已经有很多答案,但我真的不喜欢与 VIM 的替换作斗争,我总是使用 globalnormal 命令来执行以下任务:

<代码>
:g/^\\粗体/正常 df{f}x

g/^\\bold/ 选择以 \bold 开头的行,normal 告诉 VIM 运行以下命令普通模式下的指令:df{f}x。这意味着:

  • df{ = 删除,直到找到 {;
  • f} = 将光标移动到下一个};
  • x= 删除光标下的 };

I know that there are already a lot of answers, but I really don't like to struggle with VIM's substitutions, I always use the global and normalcommand for tasks this:


:g/^\\bold/normal df{f}x

The g/^\\bold/ selects the lines that starts with \bold and the normal tells VIM to run the following instructions in the normal mode: df{f}x. Which means:

  • df{ = Delete until you find an {;
  • f} = Mode the cursor to the next };
  • x= Removes the } under the cursor;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文