Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a css file with lines like this:

.exhibit-page-nav {list-style:none; float: left; padding: 1em 0; width: 100%; border-top: 1px solid #c7b5a2;}

but I want it to look like this:

.exhibit-page-nav {
    float: left; 
    padding: 1em 0; 
    width: 100%; 
    border-top: 1px solid #c7b5a2;

Is there a way to write a vim macro to do that?

share|improve this question
If you want to use vim, this may solve your problem.… – user137369 Aug 23 '12 at 17:13
up vote 2 down vote accepted

@user137369's comment is spot on but you asked for a vim macro…

A macro is not needed if you only want to format this single line: a simple substitution is enough. But you may have to do the same exercise again in the future; in this case, you may want to record it as a macro or even save it as a command or a mapping.

So… let's say you want to keep it as a macro:

qx              " start recording in register x
:s/[;\{]/\0\r/g " the substitution that does the formatting
=i{             " indent the whole thing nicely
q               " stop recording

After that, you can apply the macro by placing your cursor on a ligne and hitting:


The substitution means "replace any ; or { ([;\{]) with itself (\0) followed by a carriage return (\r)".

Now suppose you have a block of a dozen of such lines, applying your macro to all of them is as simple as selecting them and typing:

:'<,'>norm @x

See :help :normal.

If you have a lot of such lines across the file and are able to identify a common pattern, say ;}, another option (which I like a lot) would be to use the :global command:

:g/;\}/norm @x

See :help :global.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .