开发者

Search and replace across multiple lines with conversion to lower case in Vim

开发者 https://www.devze.com 2022-12-31 09:59 出处:网络
Here\'s what I\'d like to do, for each one of many files: search for all occurrences of <a href 开发者_如何学Gothen begin block selection

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

  • search for all occurrences of <a href 开发者_如何学Gothen begin block selection
  • select until the first occurrence of >
  • convert the entire match to lowercase


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:

  • a previous question
  • a Vim tip
  • the vim help

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号