Search the web
Sign In
New User? Sign Up
vim · Vim (Vi IMproved) text editor users list
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Hear how Yahoo! Groups has changed the lives of others. Take me there.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
use the position of a word as an argument in S&R   Message List  
Reply | Forward Message #105604 of 109212 |
Re: use the position of a word as an argument in S&R



Hi,

Jeri Raye wrote:
>
> Can you use the position of a word in a search and replace as an argument
>
> For example
>
> if the line has the word INPUT in it, then the last word should have
> () around it
>
> So:
>
> start: INPUT s1 , s2
> INPUT s3 , s4
>
> must become
>
> start: INPUT s1 , (s2)
> INPUT s3 , (s4)

g/\<INPUT\>/s/\(.*\)\(\<\w\+\>\)/\1(\2)/

or

g/\<INPUT\>/s/.*\zs\(\<\w\+\>\)/(\1)/

do this. The :global-command restricts the following substitution to
lines containing the word INPUT. The :substitute-commands first try to
match as many characters as possible in a line, but then they backtrack
to be able to match the right-most single word. The first command then
replaces everything that was matched with the part before the word and
the word itself in parentheses. The second command uses \zs to reset the
start of the match and replaces only the word with itself in parentheses.

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---




Mon Jul 6, 2009 7:44 am

jottkaerr@...
Send Email Send Email

Forward
Message #105604 of 109212 |
Expand Messages Author Sort by Date

Hi, Can you use the position of a word in a search and replace as an argument For example if the line has the word INPUT in it, then the last word should have ...
Jeri Raye
jeri.raye@...
Send Email
Jul 5, 2009
2:25 pm

Hi, ... g/\<INPUT\>/s/\(.*\)\(\<\w\+\>\)/\1(\2)/ or g/\<INPUT\>/s/.*\zs\(\<\w\+\>\)/(\1)/ do this. The :global-command restricts the following substitution to ...
Jürgen Krämer
jottkaerr@...
Send Email
Jul 6, 2009
7:45 am

... The OP's request sounds like school homework, so I'd recommend to read ... and what comes before and after them. Just this time, to show that Vim solutions...
Tony Mechelynck
antoine.mechelynck@...
Send Email
Jul 8, 2009
5:29 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help