Introduction
You're probably familiar with the basic find-and-replace. However, large projects with many files across several directories, require a more powerful search tool.
The GAUSS Source Browser is the powerful search-and-replace tool you need. We'll discuss some ways you can use it to make your life easier, including:
- How to find all usages of a function or variable in a file with two clicks.
- How to perform searches across multiple files and directories.
- Advanced search options, such as how to find only variable assignments.
- How to replace search terms across files and directories.
How to View All Uses of a Variable or Function
If an output seems incorrect or is causing an error, you need a quick way to trace the progress of specific variables. GAUSS's Find Usages functionality does exactly that.
Find Usages Basics
As shown in the image below, right-click on the variable you want to search for and select Find Usages.
This will search our current file for every instance of our search variable and display them in the GAUSS Source Browser.
As we can see in the above image, Find Usages has shown us:
- The name and full path of the file.
- The full line of code containing our search term, which:
- Starts with the line number.
- Highlights our search term.
We can double-click on a line in the Source Browser to open it in the GAUSS editor.
How to Search Across Directories
The Find Usages basics alone are great, but the GAUSS Source Browser has much more to offer.
How to Open the Source Browser
In addition to right-clicking and selecting Find Usages, the Source Browser can be opened two ways:
- Select View > Source Browser from the GAUSS main menu.
- Use the
Ctrl+Shift+F
(Command+Shift+F
) keyboard shortcut.
Basic Multi-file Search
The Search Tab
The Search Tab is the starting point for any search. It has three input boxes:
- Search for accepts your search query.
- File pattern allows you to restrict your search to specific file types.
- The default search pattern is
*.*
, where*
is a wildcard character that will match any character or characters. - You can add a comma-separated list of files, or file types to match. For example:
*.gss, estimate.src
. - Click the downward-pointing arrow at the end of the File pattern box to bring up a list of common GAUSS file extensions, such as
*.gss,*.src,*.sdf,*.gau,*.prg
.
- The default search pattern is
- Directory specifies the location to search.
- The default options from the dropdown-list are:
- Current working directory.
- GAUSS Search Paths.
- Project View Locations.
- Current File.
- You can also enter the name of any file or folder on your computer.
- The default options from the dropdown-list are:
Executing the search
Using these tools together can help us quickly locate the information we're looking for. For, example, suppose we want to find uses of the GAUSS generalized method of moments procedure, gmmFit, in internal GAUSS source code.
We can achieve this by:
- Entering
gmmFit
in the Search for box. - Entering
/Users/Research/gauss/src
as our Directory. - Selecting the Search button.
We could also set the File pattern option to *.src
to limit our search to .src
files. However, we left the default option to search all files in this case.
Our search results show that the term gmmFit
is used between 1 and 37 times in each of five files in the specified directory.
Advanced Search Options
The Advanced Tab
The Advanced Tab provides many useful options for further refining our search. We'll discuss some of the most helpful and less common options.
How to Find Variable Assignments
The Assignments checkbox allows us to restrict our search to only the instances when a variable is assigned to. Narrowing the list of uses to assignments only is invaluable when tracking down the cause of an incorrect value. The screenshot above shows our original Find Usages search after adding the Assignments filter.
References is the opposite of Assignments, returning instances where a variable is used, but not assigned to.
How to Search Subdirectories
By default, the Source Browser will search the directory that you specify and all subdirectories. Uncheck the Search Subdirs checkbox to restrict the search to the main search directory only.
How to Replace Across Directories
The Replace Tab
Executing a Replace Across Multiple Files
After we have completed our desired search, we can perform our replace by:
- Selecting the Replace tab in the Source Browser window.
- Entering the new term in the Replace box.
- Clicking the Replace button.
Advanced Multi-file Replace Options
The checkboxes at the start of each line of the search results allow you to quickly control which results are replaced.
Backup Original will create a copy of your files with a .bak
file extension before making the replace. Since there is no undo option for replacements made to files not open in the editor, the .bak
copies allow you to recover the original file if you make a mistake.
Overwrite Backup will copy over any pre-existing .bak
files made by previous searches with Backup Original selected. If this option is unchecked, GAUSS will use .bak2
, .bak3
, etc if pre-existing .bak
files are found.
Conclusion
After reading this post, you now know how to use the advanced search and replace tools in GAUSS to be more productive!
We have learned:
- How to quickly search your current file with Find Usages.
- How to find the exact code you need to see across multiple files and directories.
- How to find only variable assignments or variable references.
- How to replace search terms across files and directories.
Eric has been working to build, distribute, and strengthen the GAUSS universe since 2012. He is an economist skilled in data analysis and software development. He has earned a B.A. and MSc in economics and engineering and has over 18 years of combined industry and academic experience in data analysis and research.
I am looking for the slope heterogeneity test in panel data by Gauss. How to find it in Gauss.
You can directly download the author's code for these tests from here.
For more information on how to use this code you may want to review are previous blogs: