Forum Moderators: open

Message Too Old, No Replies

VIM - Search and Replace?

How is this done?

         

Nick_W

7:35 pm on Jun 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

in Vim, how do you do a search and replace? I can't find it in the help menu and it's driving me nuts.

If we have any vim experts, could someone walk me through it (assuming it's complcated and regex scary ;))?

Many thanks

Nick

bird

7:41 pm on Jun 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



:help :s

:[range]s[ubstitute]/{pattern}/{string}/[&][c][e][g][p][r][i][I] [count]

    For each line in [range] replace a match of {pattern} with {string}.
    {string} can be a literal string, or something special; see ¦sub-replace-special¦.
    When [range] and [count] are omitted, replace in the current line only.
    When [count] is given, replace in [count] lines, starting with the last line in [range]. When [range] is omitted start in the current line.
    Also see ¦cmdline-ranges¦.
    See ¦:s_flags¦ for the flags.

Nick_W

8:05 pm on Jun 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wonderfull, thanks alot!

I can't remember how many times I've looked for this then given up ;)

<added>Erm..... would you mind giving me a quick example? In this particlular case I wnat to change "s->" for "t->" in the whole file? Thanks!</added>

Nick

drbrain

8:16 pm on Jun 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



:%s/s->/t->/g<CR>

In command mode (where <CR> is your return key)

Be aware of patterns (:help pattern)

I have a map in my .vimrc like this:

map gs :%s

Because :%s is so hard to type. To substitute globally you would, in command mode, do gs/foo/bar/g<CR>

drbrain

8:22 pm on Jun 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oh, the '%' range is whole buffer. :help :range

You can browse help by hitting ^] on text in [brackets] or {braces} or *stars* (or any other text, but that's not likely to be useful).

[edited by: drbrain at 8:24 pm (utc) on June 11, 2003]

Nick_W

8:23 pm on Jun 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Awsome!

Is there anywhere where they explain stuff like this for dummies like me? I didn't even know you'd have to have a '%' before the 's'!

Thanks drbrain!

Nick

drbrain

8:33 pm on Jun 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Vim's help is very good but has a high barrier to entry (like vi/vim itself). To browse the helpfiles, place your cursor on a word ([bracketed] {curly-braced} or *starred* words link best) and use ^] to add to the tag stack, and ^T to go backward in the tag stack (like browser history).

Check out [vim.org...] also, it has load of interesting scripts, tips, plugins and other goodies that can help you out.

Hrm... it would be interesting to write an HTML ftplugin that would automatically validate your HTML with :make

bird

9:39 pm on Jun 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there anywhere where they explain stuff like this for dummies like me?

Would you be surprised? ;)

:help tutor

Actually, that points you to the program "vimtutor", which you should start from the shell. The s[ubstitute] command is demonstrated in lesson 4.4.