Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save josemariagarcia95/d0e8c613859755a63ca79059cfa3ad2a to your computer and use it in GitHub Desktop.
Save josemariagarcia95/d0e8c613859755a63ca79059cfa3ad2a to your computer and use it in GitHub Desktop.
Snippet to fix a space after cross-reference in Word using find and replace.

Edit Word's cross-references using Find and Replace and regex

  1. Select the whole document (Ctrl + E, or triple click in the left side).
  2. Hit Alt + F9, to turn every reference into its respective code.

E.g., This cross reference, [1], after hitting Alt + F9 goes { REF _Ref88888888 \n or something }

  1. Open the Find and Replace dialog.
  2. Click in More and mark the Use wildcards checkbox so we can use *, ?, {}, & and so on.
  3. Write this in the Find box:
REF *{19}

This will look for the appeareance of the expression REF followed by 19 characters (change this at your convenience). In my current case, it will match all the expressions like REF _Ref35723036 \h \r.

  1. In the Replace box, write this:
^&^92^116

^& holds all the value that is referred in the "Find" box. E.g., if REF *{19} finds "REF _Ref35723036 \h \n", ^& will hold that value. ^92^116 is the "\t" flag written with ASCII codes, each code preceded by a "^", since I needed to add a special character to every cross reference so it would remove a space inside them, but you can write regular characters in the replace.

  1. Hit Replace or Replace all depending on your needs and you'll update every cross-reference automatically.
  2. Close the Find and Replace dialog, select all the document again and hit Alt + F9 to turn every reference into regular text again.
  3. With all the text selected, hit F9 to refresh every cross reference so you can see the changes.

Bonus: Crop out a part of a cross refence using Find and Replace

(Start at point 5 of the previous tutorial)

  1. Enclose the part you want to delete or crop out in the expression using brackets. In my example, I want to delete the flags of every cross reference (the \n \h \t part), so I'll change the expression I used before to:
REF (*{12})(*{6})

Doing this, I will enclose the first 12 characters of a reference in a group, which I'll be able to access using \1, and the last 6 characters in a group I'll be able to access with \2.

  1. In the Replace box write:
REF \1

This way I'm replacing every cross reference with themselves but cropping out those last six characters (since I'm leaving out the \2 group). If I had done the replacement with REF \1\2, every reference would have stayed unchanged.

  1. Hit Replace or Replace all depending on your needs and you'll update every cross-reference automatically.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment