Today I’ll give a tour of TextMate’s features related to finding (and replacing) text.
Here’s the Find Dialog, accessible through Edit → Find → Find… or
⌘F. A few notes:
- You can press the button with the arrow next to the Replace field to change to multi-line input (Note that you still need to hold Option to insert a tab or newline into the text fields – i.e. press ⌥↩ to insert a newline).
- The sigma (Σ) button next to the Find field will display how many matches there are in the current document – handy before doing a big Replace All.
- The 3 checkboxes can be toggled with the keyboard by using
⌥⌘and the first letter of the option title (e.g.
⌥⌘Rtoggles regular expression mode)
- Holding either Shift or Option will toggle the Replace All button to In Selection Mode
However, I rarely use the Find dialog (mainly just for regular expression searches) because TextMate offers key equivalents for common operations. You can view the list in the Edit → Find menu, but here’s a brief rundown:
⌘Eto use the current selection for the text to search for
⇧⌘Eto use the current selection to replace with (or use an empty selection)
⌘Gto go to the next occurrence of the find string (or ⇧⌘G for the previous occurrence)
⌥⌘Fto replace the current selection and move to the next occurrence – this is useful when you want to review what you are replacing, you can use
⌥⌘Fto do a replace or
⌘Gto skip to the next one
- After using
⇧⌘Eto set both a find and replace string then you can use
⌃⌘Fto replace all instances or
⌃⇧⌘Fto replace all in the selection.
These are really handy and will save you a lot of time if you can get used to them – I would recommend learning
⌘G at least, since they are the most common.
Note: If you find that you’re able to move the caret beyond line endings then you’ve accidentally pressed
⌥⌘E – Edit → Mode → Freehanded Editing. Press it again (or use the menu item) to disable it.
For quick one-off searches you can use the incremental search feature instead, activated with
⌃S. The selection will move as you type in a query – you can move to the next occurrence by pressing
⌃S again (or the previous occurrence with
TextMate also offers Find in Project functionality (
⇧⌘F) on the same menu, which can do a global search in your project’s files. You can also perform a mass replace: first enter your query and run the find, then once you are happy with the results you can enter your replace string and use “Replace All” (or you can select individual occurrences and use “Replace Selected” – selecting a filename will replace all the occurrences inside that file).
However I rarely, if ever use the project find as it can be quite slow, and is blocking. Generally I use
grep, but the Grep in Project command from over on the The Pug Automatic is nice when I want a quick search.
9 Responses to “TextMate Tip – Find and Replace”