CSS通过正则表达式在Vim中添加缺少的分号

时间:2017-02-27 17:07:05

标签: regex vim

我正在为我们公司清理一些CSS,并且在许多类中都缺少分号。我已经尝试了许多不同的方法来在Vim中实现这一点,但到目前为止我还没有找到可行的解决方案。下面是一个类的示例,它似乎是此文件中主题的一部分。

我在想什么......如果有\w \s \{ || \w\;那么就不会回复。否则,如果有\:\s\w而没有;在一行的末尾然后返回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
}

2 个答案:

答案 0 :(得分:4)

我会做:g/\v^\s+\S+:.+[^;]$/norm A;之类的事情。

如果你不熟悉这些命令,这里是vim文档:g(:global)和: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>"

答案 1 :(得分:0)

根据您提供的示例,:%s/\(\w*:\s*\w*\)$/\1;正常工作。

由于它不易阅读,请添加一些细节:

  • 找一个单词\w*
  • 后跟:
  • 后跟0到n个空格\s*
  • 后跟单词\w*
  • 后面是行$
  • 的结尾

使用括号expr保存整个表达式(让我们称之为\(expr\)),然后使用\1在秒针中再次使用它。

因此,您可以使用;添加缺失的\1;