I wanted to reformat the W3.CSS file to make it more readable and study it.

I looked for an easy way to do this but couldn't find it (in vim-only way, obviously, the web are plenty of solutions and software).
I found this answer, but in my case the result is rather disappointing.

So I refreshed my notions of substitution in Vim and I did it as follows:

1. For each rule I break it in rows and put all properties in one line:

Then this:

1| html,body{font-family:Verdana,sans-serif;font-size:15px;line-height:1.5}html{overflow-x:hidden}


1| html,body{
2|     font-family:Verdana,sans-serif;font-size:15px;line-height:1.5
3| }
4| html{
5|     overflow-x:hidden
6| }

Note the blank row at the end. I want it because there's multiple consistent rules per line.

2. Split properties one per line:

Then I get what I wanted:

1| html,body{
2|     font-family:Verdana,sans-serif;
3|     font-size:15px;
4|     line-height:1.5
5| }
6| html{
7|     overflow-x:hidden
8| }

The difficulties come from the fact that:

  1. There are multiple rules per row, such as those for headers, or those for tables, etc. In the example above there's two rules for html.
  2. In the declaration block the last declaration is not terminated by a semicolon.

For this I needed two commands.

Who can do better? Is it possible to do everything with a single substitution?

  • 2
    Why would you do that when you can run %! css-beautify or %! prettier --parser css? If you need to do this often, set formatprg=prettier\ --parser\ css and run it with gq<motion> or via visual mode. Remember that vim was designed to work well with other unix tools (e.g. makeprg, grepprg, equalprg, formatprg, keywordprg and so on)
    – r_31415
    Jul 22, 2022 at 8:35
  • Because on Windows (at work) I don't have all this stuff, and if there exists often I can't arbitrarily install software. Also, Vim is a powerful tool and should allow me to work with text very efficiently (I know that in pipes to AWK or Perl I can do great things with little effort but, as I said before, sometimes these tools are not available).
    – Antonio
    Jul 22, 2022 at 10:39
  • And, last but not least, I really enjoy learning new things, finding out how to accomplish a task in multiple ways and evaluating its efficiency, in short, a little bit of healthy hacking never hurts. If we don't do it here, in a group about Vim,.. ;-)
    – Antonio
    Jul 22, 2022 at 10:39
  • 1
    That's fair, however, in this case, formatting based on substitutions is really prone to errors. It is good to know many solutions for a single problem, as long as all of them are good, reasonably equivalent solutions. Having said that, if it is just for fun, then that's okay ;)
    – r_31415
    Jul 22, 2022 at 15:36
  • 2
    @filbranden It is certainly a better solution, however, OP described additional constraints. If you cannot actually copy a binary or install the executable, then it is difficult to suggest an external tool as an alternative.
    – r_31415
    Jul 22, 2022 at 19:18

1 Answer 1


I propose you to compose the two commands using the | operator:

  • 1
    Note that this is not a unix pipe. It’s a command separator, and there are cases where using it gets tricky (e.g., with !). So really, I would avoid the word pipe altogether.
    – D. Ben Knoble
    Jul 21, 2022 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.