VIM, Run a command on multiple files

痴心易碎 提交于 2019-12-02 20:41:03

See this: http://vimdoc.sourceforge.net/htmldoc/editing.html#:argdo

I learned this command right now, but the help is clear.

Go for:

:args *.css
:argdo %s/\([[:alpha:]-]\+\):/:\1/ge | update

Here is an example

:bufdo %s/oldStuff/newStuff/ge | update

just change your regex to fit your needs

I think the best tool for this case is sed, the stream editor.

sed -i.old 's/:\([-a-z]*\)/\1:/' *.css

This will edit all your .css files leaving the original ones with the .old extensions.

The set of regular expressions used by sed is a bit different, and depending on your version more or less limited. The expression I used apparently works fine for your case — with the BSD tool.

A couple of steps.

:vimgrep /^[^:]\w+/ %:p:h/*    " find all of the lines that don't start with a colon

This will put all of the matches into your quickfix list.

Then a macro to do what you want done.

qa
I:<esc>
f:x
:w|cn<enter>
q

Then test that macro a few times (with @a). Then another macro to run that macro over and over again...

qbq   " this clears out b before starting, very important!
qb@a@bq
@b    " watch in amazement.  :)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!