How to replace all instances of text in Google Colab
Google Colab has a built-in Find and Replace tool that works like the one in most text editors. Press Ctrl+H on Windows or Cmd+H on Mac, type the text you want to find, type what you want to replace it with, then click "Replace All". The tool searches through the entire notebook at once, not just the cell you're currently in.
This is faster than manually editing each instance, especially when you're renaming a variable that appears dozens of times across multiple cells. The Find and Replace dialog stays open until you close it, so you can run multiple replacements in one session without reopening the menu.
Key Takeaways
- Press Ctrl+H (Windows) or Cmd+H (Mac) to open the Find and Replace dialog in any Colab notebook.
- Type the text to find in the first field and the replacement text in the second field, then click "Replace All" to change every instance at once.
- The search covers your entire notebook, including all cells and code comments, not just the active cell.
- You can undo a replacement by pressing Ctrl+Z immediately after, which reverses all changes from that single Replace All action.
- Replace All is case-sensitive by default, so searching for "name" will not find "Name" or "NAME" unless you enable case-insensitive search.
Opening the Find and Replace dialog
The keyboard shortcut is the fastest way to open Find and Replace. On Windows and Linux, press Ctrl+H. On Mac, press Cmd+H. A small dialog box appears at the top right of your notebook editor.
If the keyboard shortcut does not work, you can also access it through the menu. Click the three vertical dots (the menu icon) in the top right corner of the notebook, then select "Find and replace" from the dropdown. This method is slower but works if your keyboard is not responding to the shortcut.
Entering your search and replacement text
The Find and Replace dialog has two main text fields. The top field is labeled "Find" — type the exact text you want to search for here. The bottom field is labeled "Replace" — type what you want to replace it with.
Be precise with your search term. If you type "data" in the Find field, it will match "data", "database", and "metadata" all at once. If you only want to replace the word "data" by itself, you may need to include spaces or punctuation around it, like " data " (with spaces on both sides), depending on how the word appears in your code.
Clicking Replace All instead of Replace One
The dialog shows two buttons: "Replace" and "Replace All". The "Replace" button changes only the first instance it finds, then moves to the next one. The "Replace All" button changes every instance in the entire notebook in a single action.
Always use "Replace All" when you want to rename a variable or function throughout your notebook. Using "Replace" one at a time is much slower and easy to miss instances, especially in long notebooks with many cells. After you click "Replace All", Colab shows a message telling you how many replacements were made.
Understanding case sensitivity and exact matching
By default, Find and Replace is case-sensitive, meaning it treats uppercase and lowercase letters as different. If you search for "Count", it will not find "count" or "COUNT". If you want to find all variations regardless of capitalization, look for a button or checkbox labeled "Match case" or "Case sensitive" in the dialog and click it to turn off case sensitivity.
Colab also does not use regular expressions in the basic Find and Replace tool, so you cannot search for patterns like "variable_[0-9]" to find "variable_1", "variable_2", and so on. You have to search for the exact text as it appears in your code.
Undoing a Replace All action
If you make a mistake and replace the wrong text, press Ctrl+Z (Windows or Linux) or Cmd+Z (Mac) immediately after. This undoes the entire Replace All action at once, reverting all the changes you just made. You can undo multiple times to step back through your recent edits.
Undo works best when you catch the mistake right away. If you close the Find and Replace dialog, run more code, or make other edits to the notebook, undoing becomes harder because Colab's undo history may not go back far enough. For this reason, it is a good idea to double-check your search term before clicking "Replace All" on something you are unsure about.
Common mistakes to avoid
The most common mistake is forgetting that Find and Replace searches the entire notebook, not just the current cell. If you have a variable named "x" and you want to rename only the "x" in one cell, Replace All will rename every "x" in every cell, including ones you did not intend to change. In that case, undo and instead manually edit just the cells you need.
Another mistake is not accounting for spaces or punctuation. If you search for "data" without spaces, it will match "data", "dataset", "metadata", and "data_frame" all at once. If you only want to replace the standalone word "data", include a space before or after it in your search term, or use punctuation like "data," or "data)" to match only the instances you want.
Searching for common words like "if", "for", or "in" can also cause unintended replacements. These words appear in comments, variable names, and function names throughout your code. Before you Replace All with a short word, scan the preview to make sure you are not changing something you need to keep.
Frequently Asked Questions
Can I search for text in comments only, or code only?
No, the basic Find and Replace tool in Colab searches everything — code, comments, output, and cell markdown. If you need to replace text only in code or only in comments, you will have to manually edit those cells or use a different approach like exporting your notebook and using a text editor with more advanced search options.
What if I want to replace text in only some cells, not the whole notebook?
The Find and Replace tool always searches the entire notebook. If you want to replace text in only certain cells, manually edit those cells one at a time, or copy the cells to a separate notebook, run Replace All there, then copy them back. Another option is to undo the full replacement and then manually fix the cells you did not want to change.
Does Replace All work on output that has already been printed?
No, Replace All only changes the code and text in your notebook cells, not the output that has already been generated. If you replace a variable name and then re-run the cells, the new output will use the new name. Old output above those cells will not change unless you clear it and run the code again.
Can I use wildcards or patterns in Find and Replace?
The standard Find and Replace in Colab does not support wildcards or regular expressions. You have to search for the exact text. If you need pattern matching, you can export your notebook as a Python file, use a text editor or command-line tool with regex support, make your changes, and then re-upload it to Colab.
What happens if I replace text and then save the notebook?
Your changes are saved automatically in Colab. If you replace text and then close the notebook, the replacements stay. If you realize you made a mistake after saving, you can still undo by opening the notebook again and pressing Ctrl+Z, as long as you have not closed and reopened the browser tab or waited too long. For safety, undo immediately after a Replace All if you are not certain about the change.