94

I have a simple vim problem that Google hasn't managed to help me with. Any thoughts are appreciated.

I do the following search and replace:

:s/numnodes/numnodes1/g

On a file containing the following text:

numprocs=0  
numnodes=0

I get

E486: Pattern not found

The position of the green square which indicates where I'd start typing is clearly above the pattern. I tried searching for other short phrases not involving regex, which are also present, which also fail. A simple /numnodes highlights matches as expected. Does anyone have any idea what might be the matter with vim?

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Mark C.
  • 1,773
  • 2
  • 16
  • 26

2 Answers2

167

Try :%s/searchphrase/replacephase/g

Without the % symbol Vim only matches and replaces on the current line.

MasterMastic
  • 20,711
  • 12
  • 68
  • 90
mmccomb
  • 13,516
  • 5
  • 39
  • 46
18

try using this:

:%s/numnodes/numnodes1/g
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
  • 13
    Ack, the number before 's' is the line number, '%" means 'all lines' AFAIK. – Wrikken Mar 13 '11 at 12:21
  • 3
    its a line specification. you could also use (instead of %) .,$ to replace from current line to eof for example – time4tea Mar 13 '11 at 12:29
  • 1
    as a sidenote if you select from visual mode you can then use : and it will automatically fill in the line number stuff for that region so you can replace within a region. – alternative Mar 13 '11 at 12:31
  • 1
    I've been using :s/foo/bar/g forever. This is the first I've heard of this %. – Rick O'Shea Sep 09 '16 at 16:06
  • @alternative you are a life saver, that woks like a charm and should be considered as an answer. – me.nkr Jul 30 '21 at 18:00