# Swapping phrases/codes in TeXshop or TeXWorks

Is there a fast way to swap phrases in TeXshop? What I'm looking for in steps is

1. Type in two phrases
2. TeXShop searches through the file/selection for the two phrases and swaps the two.

I use this sort of manipulation often and what I currently do is to replace "phrase 1" with a temporary phrase, "temp", then replace "phrase 2" with "phrase 1" and finally replace "temp" with "phrase 2".

Is there a way to create or code a shortcut that does this?

I would eventually also like to do this on TeXWorks.

• TeXShop is an editor and MikTeX is a distribution so your question is a bit of a category error. But this isn't really a TeX question. TeXShop supports regex search and replace so perhaps you should learn how to do that. – Alan Munn Aug 15 '17 at 23:02
• @AlanMunn I did use the tags miktex and texshop. What are those tags for if not for these sort of questions? If not here, can you tell me where I should place this question? Thanks! – Prahar Aug 15 '17 at 23:04
• The tags aren't the issue. But you're asking about how to do search and replace which is an editor question. So the texshop tag makes sense but not the miktex tag since it's not an editor. But your question is off topic. Search for 'regular expressions' and learn how to use them. I'm sure you can find and answer already on StackOverflow. – Alan Munn Aug 15 '17 at 23:09
• @AlanMunn - I just realized I meant to use tag TeXWorks not miktex. That's obviously wrong. I will search this. Thanks. – Prahar Aug 15 '17 at 23:11

## TeXShop

Since regular expression matching is not part of the standard Apple find panel, I'll use this to explain how to use regular expression search and replace in TeXShop. Along the way, I'll also answer your specific question, although how to form regular expressions is strictly speaking off topic.

In the TeXShop preferences, you need to select the OgreKit Find panel:

Then in the search and replace, make sure the Regular Expressions box is selected.

To switch "foo bar" into "bar foo", you can use the search and replace pattern shown in the image:

 Search: (foo) (bar)
Replace: \2 \1


The parentheses form groups which are numbered \1, \2, etc. from left to right. In the replace box we then just need to switch the order by entering \2 \1.

## TeXworks

In TeXworks, regular expression matching is available in the standard search and replace panel; just make sure the box is clicked to use them.

• This is a really great thing I have learnt today! So I tried this and it works for regular expressions, but it doesn't work on math expressions. So to replace $\mu$ and $\nu$ indices on $T_{\mu\nu}$ I tried typing Search: (\mu) (\nu) and Replace: \2 \1 and it did not work. How can I make this work? PS - I removed the regular expressions options and it still does not work. – Prahar Aug 15 '17 at 23:54
• As I said, you need to learn how to use regular expressions. Just like TeX there are reserved symbols which need to be escaped if you want to search for them. \$ and \ are two of these. Try this site for learning more: regexone.com – Alan Munn Aug 15 '17 at 23:57
• BTW, I realize from your comment that you have misunderstood the term 'regular expression'. It has nothing to do with 'regular vs math' but refers to the formal language type that the expression syntax conforms to, namely that of the Regular Language. (Although strictly speaking most regex engines are a bit more powerful.) – Alan Munn Aug 16 '17 at 18:07