-1

I have to replace a pattern, but I don't want to replace it after a particular word.

This is my program:

:vnoremap   ::silent! call Bib()

function! Bib()
   %s/\s*\n*{\\&}\s*\n*/ /g
   %s/\([A-Z]\)\.\([A-Z]\)\./\1\. \2\./g
   "%s/\(\w*\-\w*\|\w*\),\s*\n*\([A-Z]\)\./\r\\snm{\1}\r\2\./g 

endfunc

I don't want this search pattern after a particular a word: "References"

1

To match "something", but not after a specific "word", you can use \@<!;

/\(word\)\@<! something/

For replacing "something" by "somethingelse", but only if "something" is not after "word":

:%s/\(word\)\@<! something/ somethingelse/


From inside vim, show the description with :help /\@<!:

\@<!    Matches with zero width if the preceding atom does NOT match just
    before what follows.  Thus this matches if there is no position in the
    current or previous line where the atom matches such that it ends just
    before what follows.  |/zero-width| {not in Vi}
    Like "(?<!pattern)" in Perl, but Vim allows non-fixed-width patterns.
    The match with the preceding atom is made to end just before the match
    with what follows, thus an atom that ends in ".*" will work.
    Warning: This can be slow (because many positions need to be checked
    for a match).  Use a limit if you can, see below.
0

Instead of a % for a whole file replace, you could try with a range for the address:

0,/^References$/s/\s*\n*{\\&}\s*\n*/ /g
0,/^References$/s/\([A-Z]\)\.\([A-Z]\)\./\1\. \2\./g
0,/^References$/s/\(\w*\-\w*\|\w*\),\s*\n*\([A-Z]\)\./\r\\snm{\1}\r\2\./g

(Assumes that References is the only word in the line. Modify the regex as needed.)

  • how to replace it after the word References and the remaining part safe – sam Aug 10 '14 at 2:56
  • @sam that is, from the line containing References to the end of the document? The address range would be /References/,$, I think. – muru Aug 10 '14 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.