I have a bunch of sass files and my designer used the wrong syntax. I wanted :margin-top 1px
but he did margin-top: 1px
So I easily wrote a sub command:
:rubydo sub! /([\w-]+):/,':\1'
So that works and fixes all the colons and moves them in the place I want. I have about 50 sass files in a stylesheets
directory. How can I run this command on all my files in one shot? I'm not really sure how to go about this in vim
. Maybe something to do with grep
? I couldn't tell ya.
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. :)
来源:https://stackoverflow.com/questions/7126789/vim-run-a-command-on-multiple-files