Since IntelliJ doesn't allow searching multiple directories (related codebases) simultaneously, I have written a portable script on Bash and Zsh to do the job and unlike other source code searchers on shell, it also provides modern-IDE-like features such as opening the source code file from the search result.
This script currently supports Vim, NeoVim (terminal version) and IntelliJ as the editors for the source code files.
- If you want to work completely on the shell, then choose Vim / NeoVim
- If you choose to use IntelliJ, Vim will be used as the pager to show the search result and IntelliJ will be used to open the source code file from the search result.
The code searching function of IntelliJ (Community Edition) has below limitation.
This was really a pain to me since I quite often needed to search multiple related codebases siumltaneously. Therefore, I explored for the alternatives that may run on my IntelliJ's terminal to do the job. Below are some of my expectations to the source code searchers.
- Able to search multiple directories.
- The format of the search result is designed for source code searching, unlike
grep. - Automatically ignores the build folder, library folder, .git/, .idea/ and etc.
- IDE-like feature: Do not only show the lines of code that contain the keywords, but there should also be a separate window to show the list of files that contains the keywords.
- IDE-like feature: May choose to read which file's search result from the above file list.
- IDE-like feature: May instantly open the source code file from the search result.
Unfortunately, so far what I have found only met the first 3 points. Therefore, I wrote the script.
- Copy and paste the script into your bashrc or zshrc file, or
- In your bashrc or zshrc file,
sourcethis script.
- Run below command on your Bash or Zsh
- Examples:
# Search dirA and dirB (in your current directory by default) recursively for patternA or patternB gp 'regex-of-dirA-partial-name\|regex-of-dirB-partial-name' regex-of-patternA\\\|regex-of-patternB # Search the codebases (in your current directory by default) recursively for patternA and at the same time, ignore the *test (in glob pattern) and resources directories, ignore the *.txt (in glob pattern) files gp 'regex-of-codebases-dir-partial-name' 'regex-of-patternA' --exclude-dir={\*test,resources} --exclude=\*.txt # Search all the files and directories (in your current directory by default) recursively for patternA (case-insensitive) gp . 'regex-of-patternA' -i
- Syntax:
gp <parts-of-file-and-directory-names> <pattern> [-i|--ignore-case] [-w|--word-regex] [--include=<files>] [--exclude=<files>] [--exclude-dir=<directories>] - Check this for the details
- Examples:
- The search result will then be rendered on Vim
- Glance at its UI
- Chooes to read which file's preview
- Open the source code file from the search result

- IntelliJ or Vim will be used to load the source code file, depending on your configuration
- Check the (7) in this to learn more about
\gp
- Jump to the next / previous occurrence of the keywords (the
<pattern>specified in your shell command)
- Check the (6)(iii) in this to learn more about
n
- The last searched pattern on Vim are actually highlighted

- Check the (8) in this to learn how to turn off the highlighting
- Check this to learn more about how to use Vim
Modify below configuration in the script if you want or need to.
MAPPED_KEY_TO_NAVIGATE_TO_FILES='<Leader>gp' # change this value if it has a conflict with your Vim setting
ENCODING='utf-8' # change this value if the encoding is not UTF-8## You may temporarily modify below configurable parameters on your shell before executing the search, e.g. `GP_MAXDEPTH=2; gp x x`,
## or permanently by modifying their values in this script
GP_SOURCE_CODE_FILE_EDITOR='vim' # supported values: vim, nvim, intellij
GP_SOURCE_CODE_FILE_EDITOR_LAUNCHER_PATH='' # leave it an empty value if you use Vim / NeoVim
GP_NUM_OF_CTX_LINES=10 # the number of lines to show before and after matches of the keywords
GP_MAXDEPTH=1 # the depth of directories to search for <parts-of-file-and-directory-names>, e.g. 1 value to search only current directory for <parts-of-file-and-directory-names>
GP_EXCLUDE_DIR=('lib' 'libs' 'build' 'bin' '.?*')
GP_EXCLUDE=('png' 'jpeg' 'jpg' 'tif' 'tiff' 'bmp' 'gif' 'eps' 'raw' 'cr2' 'nef' 'orf' 'sr2' 'swo' 'swp' '?*~' 'lib' 'dll' 'a' 'o' 'class' 'jar')
GP_SEARCH_RESULT_FILENAME='GP_SEARCH_RESULT.txt' # the file generated to temporarily store the search resultConfiguration for setting IntelliJ the source code file editor:
- Check this to learn more about how to set
SOURCE_CODE_FILE_EDITOR_LAUNCHER_PATHfor IntelliJ. - If you are a Mac user, you may need to search about how to create the IntelliJ command-line launcher in your environment.
- Examples
- Windows > Git Bash
SOURCE_CODE_FILE_EDITOR_LAUNCHER_PATH='/c/Program\ Files/JetBrains/IntelliJ\ IDEA\ Community\ Edition\ 2021.3.1/bin/idea64.exe'
- MacOS > Zsh
SOURCE_CODE_FILE_EDITOR_LAUNCHER_PATH='/Applications/JetBrains\ Toolbox.app/Contents/idea'- Please note that you need to firstly create your command-line launcher
- Windows > Git Bash
gp <parts-of-file-and-directory-names> <pattern> [-i|--ignore-case] [-w|--word-regex] [--include=<files>] [--exclude=<files>] [--exclude-dir=<directories>]<parts-of-file-and-directory-names>- A string of regex.
- It defines what to search in your current working directory.
<pattern>- A string of regex.
- It defines the pattern to search for.
[-i|--ignore-case]- An option to make the
<pattern>search case-insensitive.
- An option to make the
[-w|--word-regex]- An option to request that the
<pattern>has to match the full (not partial) word to produce a match.
- An option to request that the
[--include=<files>] [--exclude=<files>] [--exclude-dir=<directories>]- This script makes use of
grepto do the search. - The above options will be passed to
grep. - You may check your
grepmanual to learn more about these options. - Please note that the behaviour of
grepin response to these arguments varies with different shells.
- This script makes use of
- A brief idea about normal mode, command mode and insert mode
- Cursor movement
- One-line movement in normal mode
hfor left,jfor downward,kfor upward,lfor right
- Page-wise movement in normal mode
<ctrl-d>for half page downward,<ctrl-u>for half page upward
- One-line movement in normal mode
- Move to another window, e.g. from the file list window to the preview window
<ctrl-w>thenhorjorkorlin normal mode to move to the surrounding window
- Exit Vim
:qato leave Vim:qa!to leave Vim forcefully and your unsaved changes will be abandoned
- Select a file in the file list and show its preview
- In the file list, move your cursor to the file and press
<Enter>
- In the file list, move your cursor to the file and press
- Search through the previews
- Go to the preview window
/<regex-pattern>to search forward,?<regex-pattern>backwardnto repeat the last search,Nthe last search in a reverse order- Check this for more information
- Navigate to the souce code files that you want to learn more
- Go to the preview window
- Go to the line of code you are interested in
- Type
<Leader>gpin normal mode<Leader>in Vim by default means\<Leader>gpmeans\gpby default
- Afterwards, it will bring you to that line of code
- If you use Vim as the source code file editor,
<ctrl-6>in normal mode to go back to the preview
:nohlsearch(or:noh) to turn off the last searched pattern's highlighting
- Jump to the next or previous file's preview when you are in the preview window
{and}in normal mode
- Jump to the 1st line / the last line
ggto the 1st line andGto the last line
- Preview the next or previous file in the file list, regardless of where you are on Vim
:cnext(or:cn) to preview the next file,:cprevious(or:cp) the previous file
- Close and open the file catalouge
:cclose(or:ccl) to close,:copen(or:cope) to open
- Show multiple file previews at the same time
:vsplit(or:vs) in the preview window to create a vertical split window to view the previews:split(or:sp) to make a horizontal split window<ctrl-w>qto close the current window
- Make your current line at the top / center / bottom of your screen
- In normal mode,
ztto make your current line at the screen top,zzthe center,zbthe bottom
- In normal mode,
- Bookmark your preview
- In normal mode,
mto mark and backtick to go back to that marked location - Check this for more information
- In normal mode,
- macOS Monterey version 12.0.1
- zsh 5.8 (x86_64-apple-darwin21.0)
- GNU bash, version 3.2.57(1)-release (arm64-apple-darwin21)
- VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Sep 26 2021 21:11:52), macOS version - arm64, Included patches: 1-3458, Compiled by Homebrew
- NVIM v0.5.0, Build type: Release, LuaJIT 2.1.0-beta3, Compiled by brew@HMBRW-A-001-M1-005.local
- Git Bash
- GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)
- VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Sep 21 2021 16:13:20), Included patches: 1-3441, Compiled by <https://www.msys2.org/>



