My professor asked me to change all p,q variables to i,j variables in my paper. If I just search and replace it will have too many false positives, because it will highlight every word with letter p in it. I want to search only within math mode, but apparently my editor does not support this. Is there some lightweight editor, or maybe even web tool that can search and replace within only math mode? I want to do just this one task and get back to using my old editor.

The answer is very nice, but I can't think to Emacs as a lightweight editor. ;-)
–
egregJan 10 '13 at 13:34

@egreg : Good point, my fanatism made me forget about that. I tend to consider it amongst "lightweight" options when I need to do something, because it is sooo reactive (especially here when compared to graphical editors). Also, it is sometimes pre-installed with the OS (not windows).
–
T. VerronJan 10 '13 at 13:48

I have the same problem; I always use Emacs so this would be perfect, but this solution doesn't work for me. It simply tries to replace every occurrence of the letter, whether in math mode or not.
–
LiamJun 24 '14 at 12:52

@Liam: This is normal: the command tries to replace all p's in the text. If the letter is in a math construct, it replaces it with i, otherwise it "replaces" it with a p (effectively not changing it).
–
T. VerronJun 24 '14 at 13:11

I once wrote a perl script to do just this sort of thing. Unfortunately, dollars are the main thing that it doesn't do. But I also wrote a perl script to convert dollars to their LaTeX counterparts. So you could run it through that script first, and then the replacement program.