A multi-color grep with convenient features
cxpgrep wraps up grep with some frequently used options (to save typing), and highlights different patterns in different colors, and organizes the output in a way convenient for anchoring at any matching line in the file opened in Vim. It has the following features:
- By default includes the following
grepoptions:-sPHInr --exclude=*~ --exclude-dir=.vim --exclude-dir=.git --exclude-dir=.repo --exclude-dir=.svn
- 1 color for each pattern - So a pattern (Perl regular expression) 'a|b' will have a and b in different colors.
- Easy to construct command lines like
vim myfile +1270to go to line 1270 of myfile.
Doing the following should NOT spoil your existing environment - You will only get 3 new commands h/cxpgrep/cxpgrep2 in the form of Bash function.
In ~/.bashrc:
- source the script h, which is a multi-color pattern highlighter.
- source the script cxpgrep, which uses the command
h. The commandscxpgrep/cxpgrep2will be available for use.cxpgrep2just displays the output in a format different fromcxpgrep.
- The command is like:
cxpgrep 'PAT1|PAT2|...' FILE-OR-DIRECTORYPatterns will be highlighted in different colors in the output.
- It accepts other options of
grep. - To construct a Vim command line to jump to a matching line of a file, double click the file pathname and Shift + single click the line number in the
cxpgrep/cxpgrep2output.
I designed a bunch of test cases to verify the implementation of cxpgrep and cxpgrep2. Cases can be added to the files cases_cxpgrep and cases_cxpgrep2, respectively. And there are 2 ways to use the cases: automated and interactive.
Automated:
source ch_tests_automated.sh cases_cxpgrepInteractive:
source ch_tests_interactive.sh cases_cxpgrepIn both methods, one needs to compare the output of cxpgrep and grep -P. Certainly grep doesn't support multi-color highlighting.
Also, note that these 2 scripts are NOT to be run directly. They need to be sourced into the current shell environment. See above.
- 1.0
- First working code
- 1.1
- README and source code header comment updates
This software (cxpgrep) is distributed under the MIT license.
