Searching (and safely replacing) code in scripts can be challenging. Renaming variables, functions, and parameters might break your code if you don’t rename all instances and derived elements correctly. Discover ISESteroids search options, the new semantic search and the editor focus on affected elements – they all turn searching and replacing into an easy and extremely powerful experience.
Quickly Finding Functions and Regions
The navigation bar at the top of the editor pane always shows the name of the function your caret is in. Press ALT+N to toggle the navigation bar.
- Click the “New” icon to insert a new function body. Right-click the “New” icon to open a context menu with choices. You can insert different function snippets, or open new documents of various types, including VBScript.
- Shows the name of the function/configuration/workflow the caret is located in, or “(main)”. Click to drop down a list of all functions/configurations/workflows and collapsible regions.
- Icon is enabled when you select more than one line of code. Turns the code into a function.
- Icon is enabled when the caret is inside a function. Exports the function to a new or existing PowerShell module.
- Enter any text to highlight all instances in real-time
- Click to adjust search options
When you click the function name (marked #2 in the picture above), a combobox drops down and provides you with a script inventory: it list all functions, configurations, workflows, and regions. Just click one to quickly navigate to it.
- Click into Navigator. Drop-down list slides out.
- Currently selected entity is marked gray.
- Click anywhere on the list to navigate elsewhere
Note that the dropdown list uses different icons. In the screenshot, you see all collapsible regions on top, followed by functions. Workflows and configurations use differently colored icons. To change Navigator Settings, right-click into the navigator (make sure your caret is over text):
Finding Functions From References
When you right-click a command in your code that resolves to a function, right-click the command and choose “Go To Definition”. This switches focus to the function code. If the function was defined in another script or module, ISESteroids switches editor pane.
On top of each function definition, ISESteroids shows how often this function is referenced from other parts of your code.
If the function has just one reference, then clicking the reference overlay brings you immediately to the reference. If there are more than one reference, a reference list slides in at the bottom of the editor. Click any reference inside of it to move to the reference, or click into the editor pane to slide in the list again.
- Click the reference adornment
- A reference list slides in. Click any reference to switch focus to that reference
If you’d quickly find a given spot in your script, enter your search phrase into the search field (marked #5 below):
While you type, ISESteroids starts to highlight all matches in your code. Press ENTER to navigate to the next instance, or SHIFT+ENTER to navigate backwards. press ESC to clear the search.
- Enter search phrase, or press CTRL+F to copy selection into this field. Field shows a green border when there are search results, else a red border.
- In real time, all instances are marked. Press ENTER / SHIFT+ENTER to navigate between them.
Press ESC to clear the search field, and remove all markings.
Semantic Variable and Function Rename
Assume you have code like this and would like to rename the variable $name inside the function Test-Something:
With a simple text search&replace, your code would break because this search would miss many spots where changing the variable name also affects other code.
With semantic replace, ISESteroids works different. Click the variable $name inside the function Test-Something (but do not select text). Then press F2. Here is the result:
- Click a variable, then press F2. Semantic replace marks the variable and all affected portions of your code.
- All other variables with the same name in the same scope are also marked.
- All arguments of commands referencing the variable are also marked.
- All parameters affected by the rename are also marked.
- All variables with same name outside the scope are not marked. These are different variables that just happen to share the same name.
Simply type the replace text. As you type, all marked instances are renamed. You can also press ESC to restore the original name, or press ESC again to cancel the replace. Once the replace is applied, you can press CTRL+Z to undo the replace operation.
Safely reneming a variable or function name takes more than just a simple text seach&replace. Semantic replace is an easy yet powerful feature enabling you to refactor variable names. But it can do more.
When you click on any portion of your code and press F2 (without selecting text), you always get a semantic replace. So if you click on an operator, for example, and change casing, only operators of the same kind are marked and affected.
Advanced Search & Replace Dialog
This functionality was added in ISESteroids 18.104.22.168.
When you select text and then press F2, a Search&Replace dialog opens. Note: the dialog appears only when you have selected text. If nothing is selected when you press F2, ISESteroids uses the quick mode and performs a semantic replace.
It looks similar to this:
- Search Term. Taken from your selection but can be adjusted (except in Semantic Mode)
- Search Target: select the parts of your code that are affected by this search
- Search Comments: when checked, searches in comments to (except in “Everywhere” mode where everything is searched anyway)
- Replace Term: Replace findings with this text
- Show only affected lines in editor view until you press ESC or start to edit code
Finding (and replacing) the right things in your code is crucial, so a simple text search can wreck code easily. Instead, ISESteroids offers two types of language-based search:
- “Only when it is…”: limits search on tokens only. Takes the token kind of the currently selected token. Not available if your selection spans more than one token type. Use this to “only replace operators” or “only replace in comments”
- Semantic: available for a limited set of selected items, like variables, function names, parameters. Always auto-detects the replace text which is why the “Search for” group is disabled. Uses advanced logic to identify all relevant instances and necessary replacements to perform a safe replace.
Token-based- and Semantic Replace can both extent their reach into comments when you check “Search in comments, too”. For example, if you rename a function you might also want to update its comment based help, just like with parameters.
Let’s take this code for example:
Now let’s assume you want to rename the function “Test-It” into “Add-User”.
Simply select “Test-It” and press F2. The dialog opens:
- Option “Semantic Search” is preselected. Group “Search for” is disabled because Semantic Search knows already everything it needs.
- Check “Search in comments, too”
- Enter the replacement text: “Add-User”
- Check “Show only affected code in editor”
- Then click “Search”
Here is what ISESteroids selects for you:
- The search term is identified multiple times, including comments
- The editor just shows the lines that are affected, and excludes any other line
You can now do a couple of things:
- press ENTER to accept the replace
- press ESC to undo the replace
- Type text to change the replace term in real-time
- Click one of the marked siblings to selectively undo the replace for this item.
- Click anywhere outside a selected item to accept the replace
Note that the collapsed view (showing only affected lines) is turned off the moment you edit the code in any way. Once you start editing the code, you are brought back to normal view. You can then press POS1 (HOME) to quickly switch to the item being renamed, or press TAB to move to one of the siblings.
The settings for “Search in comments” and “Show only affected code” persist, and also apply to the search performed when you press F2 on a token without any selection (QuickSearch&Replace).
Search in Files
ISESteroids can quickly search for expressions in files and entire folder hierarchies, in case you are looking for scripts containing a certain command or phrase.
Choose “Edit/Advanced Search” (or press CTRL+SHIFT+F). The Advanced Search Addon opens:
- Search phrase to search for
- Root search location. Use command Set-Location to change current location
- Enable recurse search
- Specify semantic search and special search options like Regex support
- List of search results. Click to open in editor