String Substitution in VIM

November 9th, 2006 liewsheng Posted in vim | Hits: 19706 | 5 Comments »

VIM is a powerful text editor, it have a lot of feature like tag, syntax highlighting…and many more. Here I only show some useful vim command.

To start the VIM editor:

vim <filename>

<filename> is a option. If no <filename> specific, VIM will open a empty buffer for you. After you have edit, If you want to save the text, you can do this:

:save <filename>

Now go to the main topics – string substitution ;p Let say you want to search a word – “help” and substitute by “pleh”, this you can simply enter:

:s/help/pleh/

What if you want to substitute a word which in line 2 until line 20:

:1,20s/help/pleh/

Next you may want to swap order of two different word, for example “help” and “me” to become “me help” :

:s/\(help\) \(me\)/\2 \1/

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 first \( and \) pair will represent by \1, the second \( and \) is \2 and so on…

You can easy to capitalize characters in line by enter:

:s/[a-z]/\u&/

following command will capitalize only the first charater of every words:

:s/\S*/\u&/

For more info about command can be use, you can visit this page: VIM Trick

5 Responses to “String Substitution in VIM”

  1. thats inefficient to capitalize all strings on a line…
    In command mode enter:
    Shift+V U (as in capital U)

    Shift+V selects the line
    U uppercases it.

    Reverse is to use lowercase u which downcases it.

  2. Thank you :)

  3. to aizatto

    Thank you!

  4. Ciekawe naklejki na cian to dobra ozdoba do kazdego domu! naklejki na cian naklejki cienne dekoruj

  5. Nice post there. Will check more soon! You’ve have a good day!

Leave a Reply