Some Trick for VIM

September 4th, 2006 liewsheng Posted in vi, vim | Hits: 19249 | 2 Comments »

When I converting a e-code to SystemVerilog syntax, I realize that I don’t like the format created by the script. The script creating port which group them under a line, but I like them if they separte in different line for different port. So I need to replace some special character with a new line.

Here is part of the example code converted by the script:

module abcd ( ab, cd, de, ef, fg);


to become:

module abcd ( ab,


so to have a new line after each port name (ab, cd …), this simple command can be used:

:s/(s*S*,) /\1r/g

where the \s\S mean will match all the character except space character like ” ” or “new line”. the * mean match 1 or more of the character. So if you wan match the space character, \S\s can be used.
(Thanks for mysurface comment).

\( and \) will tell vim to remember the word inside them and \1 will be the 1st match of the \(, \) pair. If more that 1 \(, \) pair then subsequent word will be \2, \3 ….

How about if we want to delete the empty line? Here is the command :


where :g/ mean match a regular expression(RE) while ^ mean starting of a line and $ is end of line. the /d behind mean delete the RE matched.

2 Responses to “Some Trick for VIM”

  1. :s is search the pattern will be
    /g at the back is actually ask vi to check it globally and replace all it found.

    Therefore, \s*, is a match keyword and ,\r is replace keyword.
    \s means it will match white-space character like space and tab, (you have quote it wrong)
    * means it matches any numbers of character, can be zero too. So \s*, means it matches any white-space character (can be zero) + comer (,).
    This will be replace by ,\r. \r actually means or enter or new line or carry return.
    \S match any non-white-space character.
    :g is to execute commands that based on the regular expression it matches
    ^ means first character (beginning of a line)
    $ means last character (end of a line)
    ^$ means nothing in the line ( nothing within ^ and $, blank line)
    d at the back is actually command to execute, d stand for delete.
    Therefore, :g/^$/d means delete blank line. I am not sure did it runs globally or not.

    To make sure what is the command use for you can simply type :h follow by the keyword, such as
    :h :g
    means you want to check what is :g all about.

  2. […] actually the (help) (me) can be replace by (S*) (S*), where S* is mean any character but not a space character (which have intro in Some Trick For VIM). \( and \) pair will store in buffer. The 1st \( and \) pair will represent by \1, the 2nd \( and \) is represent by \2 and so on….. […]

Leave a Reply