在 Vim 中搜索并替换多行并转换为小写

发布于 2024-09-02 07:24:38 字数 165 浏览 2 评论 0 原文

对于许多文件中的每个文件,我想做的是:

  • 搜索所有出现的 然后开始块选择
  • 选择,直到第一次出现 > >
  • 将整个匹配项转换为小写

Here's what I'd like to do, for each one of many files:

  • search for all occurrences of <a href then begin block selection
  • select until the first occurrence of >
  • convert the entire match to lowercase

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

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

发布评论

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

评论(1

幻想少年梦 2024-09-09 07:24:38

检查一下:

s/<a href\(\_[^>]*\)>/<a href\L\1>/

这表示:匹配 ,后跟除 > 之外的零个或多个字符,包括换行符(由 \_),最后是 >。将其替换为 、捕获的文本转换为小写(由 \L 表示)以及结尾的 >

显然,如果您的标签内有 > ,您就得靠自己了,但这就是生活。

(如果不清楚,您可以将其用作 :%s/.../...:s/.../... / 对于一些较小的范围。)

要使用它多个文件,您将使用 windobufdotabdo(具体取决于文件是以选项卡、窗口还是仅以选项卡形式打开)缓冲区)。有很多文档:

一般来说,它可能看起来像这样:

:tabdo <range>s/.../.../
:windo <range>s/.../.../
:bufdo <range>s/.../.../ | w

我在 bufdo 版本中添加了一个写入命令,因为它可以' t 移至下一个缓冲区而不保存当前缓冲区。如果你想立即写作,你也可以为其他人这样做,但他们不需要。打开所有文件取决于您 - 最简单的方法就是 vim 将它们作为缓冲区,或者 vim -o < /code> 将它们作为窗口(使用 -O 垂直分割而不是水平分割)。

Check it out:

s/<a href\(\_[^>]*\)>/<a href\L\1>/

This says: match <a href, followed by zero or more characters besides >, including newlines (indicated by \_), and then finally a >. Replace it with <a href, the captured text converted to lowercase (indicated by \L), and the trailing >.

Obviously you're on your own if there's a > somewhere inside your tag, but such is life.

(And in case it's not clear, you'll use this as :%s/.../... or :<range>s/.../.../ for some smaller range.)

To use this on multiple files, you'll use windo, bufdo, or tabdo (depending on whether the files are open as tabs, windows, or just as buffers). There's a lot of documentation out there:

In general, it'll probably look something like this:

:tabdo <range>s/.../.../
:windo <range>s/.../.../
:bufdo <range>s/.../.../ | w

I've added a write command to the bufdo version, because it can't move on to the next buffer without saving the current one. You could do this for the others too, if you wanted to write immediately, but they'll work without. It's up to you to get all the files open - the easiest way is just vim <lots of files> to get them as buffers, or vim -o <lots of files> to get them as windows (use -O to split vertically instead of horizontally).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文