25

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.

slm
  • 15,396
  • 12
  • 109
  • 124
Andrew WC Brown
  • 2,240
  • 5
  • 22
  • 24

4 Answers4

33

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
Niloct
  • 9,491
  • 3
  • 44
  • 57
  • +1 for `argdo`, `bufdo` is too general. You can also create an argument list local to the window with `:argl` if you don't want to change your global arg list. – Greg Sexton Aug 21 '11 at 10:37
  • 2
    I found this article on this subject to be very helpful: http://www.ibrahim-ahmed.com/2008/01/find-and-replace-in-multiple-files-in.html – webdesserts Mar 16 '12 at 19:32
9

Here is an example

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

just change your regex to fit your needs

Eric Fortis
  • 16,372
  • 6
  • 41
  • 62
5

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.

sidyll
  • 57,726
  • 14
  • 108
  • 151
3

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.  :)
dash-tom-bang
  • 17,383
  • 5
  • 46
  • 62