Using Find and Replace
Since the earliest word processors Find and Replace has been a standard feature expected by users editing almost any content. You can score extra points with the programming crowd if you support Regular Expressions, the swiss army knife mini-language of Find and Replace operations. Of course, TextMate 1 had both.
TextMate 2 adds some subtle refinements to this already strong collection of features.
In this article will discuss:
- Keyboard usage of Find operations
- New visual feedback provided for these operations
- How to speed up your searches
- Enhancements to the Find dialogs
The Three Shortcuts You Should Already Know
Before we get into the Find enhancements, I need to make sure you’ve taken the time to learn three very important Find shortcuts.
Personally, I’m pretty lax on keyboard shortcuts. I don’t claim you need to know them all. Sometimes a trip into the menus is totally acceptable. In fact, there are shortcuts I recommend people don’t learn for various reasons. However, there are exceptions. Some keyboard shortcuts are more important than others. Some are almost required knowledge for efficient editing. Three of those relate to Find operations:
- ⌘E (Use Selection for Find)
- ⌘G (Find Next)
- ⇧⌘G (Find Previous)
Together, these three key combos form a super fast means to navigate Find operations without even needing to stop off at a dialog first. Just select something, throw it on the Find clipboard with ⌘E, and start jumping to matches with ⌘G and/or ⇧⌘G.
The real reason to learn these keys though is that they are Mac OS X standards. This isn’t just about TextMate. Any well behaved Mac program with Find should honor these same keys. Learn them once. Use them everywhere. I literally mean everywhere too, because the Find clipboard is shared between all applications.
It’s ⌘G Everywhere
That brings us to the first couple of TextMate 2 tweaks.
TextMate has always had a slightly hidden feature on ⌃S. It’s called Incremental Search, which is just a fancy way of saying that it starts matching as you type. The problem with the TextMate 1 version of this feature is that it used its own shortcuts for moving to the next or previous match.
TextMate 2 brings this feature inline with the rest of the OS by switching to ⌘G and ⇧⌘G for moving to the next and previous matches respectively.
Along the same lines, you can now walk through Find in Project (⇧⌘F) matches using ⌘G. Even if the next match is another file, TextMate 2 will take you there.
Now you can just remember that ⌘G means Find Next everywhere.
The Good and Bad of the Other Keyboard Shortcuts
Now that we’ve talked about the holy trinity of keyboard shortcuts, it’s worth noting that TextMate adds one very handy Find shortcut of its own. You might want to consider devoting brain power to learning this as well:
- ⇧⌘E (Use Selection for Replace)
That pairs beautifully with ⌘E and can save you a dialog visit for operations that need a Replace as well Find.
Of course, TextMate adds a ton of other shortcuts for Find operations. One I find myself letting go of in TextMate 2 is Replace All (⌃⌘F). I used it quite a bit with TextMate 1 where I would often grab a search, press ⌘E, grab the desired replacement, use ⇧⌘E to set that, and finally Replace All (⌃⌘F). With TextMate 2’s Find All (⌥⌘F) I prefer to shorten that to select, ⌘E, ⌥⌘F, and edit. See the article on Multiple Carets for a more thorough discussion of just how great Find All can be.
Earning Tips
Another tweak keyboard Find users are likely to enjoy is the addition of tooltip feedback for a few operations. If you ⌘G/⇧⌘G past the last match, a tooltip tells you that.
Also, you get a tooltip with match counts during Find All or Replace All operations. That’s handy since they can involve more matches than you can see on the screen.
Finally, you will see little stars marking matches in the gutter as you are working through a Find in Project searches. This can help you see where your caret will go next and/or how many matches remain.
These are simple but appreciated enhancements.
The Need for Speed
TextMate has always had a Find in Project (⇧⌘F) feature and it’s terrific to be able to scan a huge set of files or use a Regular Expression to do some clever mass editing. I’ll be honest though and say that this feature took some heat during the TextMate 1 era for being on the slow side. A lot of users resorted to using tools like grep or ack to achieve similar results faster.
It you have turned your back on the feature in the past, give it another shot after upgrading to TextMate 2.
First, it’s just plain faster now. That’s a great start all by itself. If you really need faster searching though, it’s often attainable using Find in Project’s new In and matching fields to restrict a Find to certain folders and/or to a “glob” of matching files respectively. For example, *.{html,css}
is a great glob for renaming a CSS class inside of your HTML and CSS files.
Remember Your History
Ever been in that situation where you thought, “I had that regex just a moment ago!”. You’ve always been able to get those old searches back in TextMate by opening the Find dialog and using the history pop-up there.
Keeping with our skip-the-dialog theme though, TextMate 2 now allows you to use old searches straight from the keyboard. Just invoke Find History (⌃⌥⌘F), arrow down to the desired search string, and press ↩ to restart that search.
This can be pretty handy if you are alternating between a small number of Find operations.
Better Dialogs
While I’ve been pretty hard on the Find dialogs in this article, there are good reasons to access them at times. When you do, you’ll be treated to some new features there as well.
First, the unification of the Find and Find in Project dialogs means that you can get all of the matches from a single document presented in a list (as Find in Project has always done). To see this list, use the Find All button (note that this is different from the Find All menu command).
There are also the new Full Words and Ignore Whitespace checkboxes. Full Words hasn’t been implemented yet, but Ignore Whitespace allows the amount of whitespace used to separate content to differ between the search string and matched content. A typical use case for this would be if you have multiple lines of code that you want to find. For example, say that I have five lines of code that I am almost certain is copied from elsewhere and I want to refactor it into a shared function. A problem with finding the other use of that code is that it might be indented differently. Use Ignoring Whitespace though and TextMate 2 will still find it.
In the enhanced Find in Project dialog of TextMate 2, you may also enjoy knowing that ⌘1, ⌘2, … ⌘N will open that number of file with matches in it. You can see which number is attached to which file by clicking of the result list header, for a pop-up. You can also use the collapse all arrow to the left of this header to make it easy to count the files, as well as see the number of matches each contains. This can save you a trip to the mouse when you know just what you are hunting for.
It’s probably obvious that the result list also allows you to control what does and doesn’t get updated, via the checkbox next to each match. There are some hidden features here worth noting. First, ⌥-clicking a checkbox will toggle all of the checks for that file. You can also copy content from the result list, complete with a file and line reference. Just click on the desired entry and trigger Edit → Copy (⌘C).
Replacements are also smarter in TextMate 2. Remember that they are Format Strings, with all of the new abilities that entails. The match list will also reflect changes as you are editing a replacement string in TextMate 2. It’s nice to be able to preview the changes you are about to make, before actually making them. This is especially helpful when you are using clever Format String replacements.
None of these changes are major new features, but together they really round out and add to the robustness of TextMate 2’s Find and Replace operations.