probie
probie

Reputation: 89

CSS add missing semicolons in Vim via regex

I'm cleaning up some CSS for our company and in many of the classes there are missing semicolons. I've tried a number of different ways to make this happen in Vim, but so far I haven't found a viable solution. Below is an example of one class that seems to be part of a theme in this file.

What I'm thinking... if there's a \w \s \{ || \w\; then don't return true. Otherwise if there's a \:\s\w and no ; at the end of a line then return true.

.ribbon_table,
.tabbed_interface_section_table,
table#monthview_table,
table#weekview_table_table {
    border-collapse: collapse
}

.btn.active.focus,
.btn.active:focus,
.btn.focus,
.btn:active.focus,
.btn:active:focus,
.btn:focus,
a:focus,
button:focus,
form:focus,
input:focus,
select:focus,
textarea:focus {
    outline: 0;
}

blockquote {
    font-size: 14px;
    border: 0;
}

.caret {
    border-top-style: solid
}

Upvotes: 1

Views: 383

Answers (2)

Pierpo
Pierpo

Reputation: 41

On the example you gave, :%s/\(\w*:\s*\w*\)$/\1; works fine.

Since it is not very readable, let's add some details:

  • Look for a word \w*
  • Followed by a :
  • Followed by between 0 and n spaces \s*
  • Followed by a word \w*
  • Followed by the end of a line $

You save this whole expression (let's call it expr) using parenthesis \(expr\), and use it again in the second hand using \1.

Thus, you can add the missing ; using \1;.

Upvotes: 1

Travis
Travis

Reputation: 435

I would do something like :g/\v^\s+\S+:.+[^;]$/norm A;.

In case you're unfamiliar with these commands, here's the vim documentation for :g (:global) and :norm (:normal).

                                            :g :global E147 E148
:[range]g[lobal]/{pattern}/[cmd]
                    Execute the Ex command [cmd] (default ":p") on the
                    lines within [range] where {pattern} matches.



:norm[al][!] {commands}                                 :norm :normal
                    Execute Normal mode commands {commands}.  This makes
                    it possible to execute Normal mode commands typed on
                    the command-line.  {commands} are executed like they
                    are typed.  For undo all commands are undone together.
                    Execution stops when an error is encountered.

                    If the [!] is given, mappings will not be used.
                    Without it, when this command is called from a
                    non-remappable mapping (:noremap), the argument can
                    be mapped anyway.

                    {commands} should be a complete command.  If
                    {commands} does not finish a command, the last one
                    will be aborted as if <Esc> or <C-C> was typed.
                    This implies that an insert command must be completed
                    (to start Insert mode, see :startinsert).  A ":"
                    command must be completed as well.  And you can't use
                    "Q" or "gQ" to start Ex mode.

                    The display is not updated while ":normal" is busy.

                    {commands} cannot start with a space.  Put a count of
                    1 (one) before it, "1 " is one space.

                    The 'insertmode' option is ignored for {commands}.

                    This command cannot be followed by another command,
                    since any '|' is considered part of the command.

                    This command can be used recursively, but the depth is
                    limited by 'maxmapdepth'.

                    An alternative is to use :execute, which uses an
                    expression as argument.  This allows the use of
                    printable characters to represent special characters.

                    Example: 
                            :exe "normal \<c-w>\<c-w>"

Upvotes: 4

Related Questions