0

I need to replace ;\s*\<do\> with \rdo in Vim. However, I also need to make sure ;\s*\<do\> does not get replaced if there is a Fortran comment symbol ! before it, i.e., in the search patter !.*;\s*\<do\>. For example, ; do in the uncommented line

j=2; do i=1, 10

should be replaced as

j=2
do i=1,10

But ; do in the following commented part should not be replaced,

 k=3 ! j=2; do i=1, 10 

How can I do this in vim ? I tried \(!.*\)\@!;\s*\<do\> and it does not work.

4
  • 1
    Can you post an example input file and the expected output? Commented Sep 3, 2017 at 21:16
  • I don't think you should ask this question here, superuser may be a better place Commented Sep 3, 2017 at 21:19
  • 1
    There is also vi.stackexchange.com but vi(m) questions have always been accepted on SO... Commented Sep 3, 2017 at 21:21
  • The question has been updated with an example. Commented Sep 3, 2017 at 22:05

2 Answers 2

3

I edited this answer based on your example:

Try using :g!/<pattern1>/s/<pattern2>/<replacement>/g:

:g!/!.*/s/\v;\s*do/\rdo/g

This does the replacement of pattern2 with replacement only on lines that don't contain pattern1.

Original answer:

The following pattern should do what you requested: /[^*]\{0,1\}\zs;\\s\*\\<do\\>. You can use it to replace it with whatever you want, e.g. %s/<pattern>/\rdo/g.

Sign up to request clarification or add additional context in comments.

4 Comments

note the leading / is the search command, not part of the actual regex.
This does not work. Maybe I do not phrase my question properly. By !.*;\s*\<do\>, I mean a search pattern. See the updated question.
sorry, before you added an example it seemed like you were trying to replace literal search patterns.
Finally I figured out an alternative way to do the match, \c\(!.*\)\@<!;\s*\<do\>
0
:v/!/ s/; /\r/g

:v  .......... global negation (all lines without pattern)
/!/ .......... all lines not containing !
s ............ substitution
; ............ semicolon space
\r ........... Carriage return "Enter"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.