Using exclusion patterns when grepping
05 Mar 2018Git
I like Git. A lot. After years of use it has
really grown on me. It's (mostly) fast, (often) reliable, and (always)
distributed. For me, all properties are important, but being able to
do git init
to start a new project in seconds is the best feature.
When it comes to working with Git day-to-day, a nice GUI can really make a difference. In Emacs world, of course it's Magit. Outside of Emacs (brr), git-cola looks to be the most promising one. If you're aware of something better, please share - I'm keeping a list of suggestions for my non-Emacs using colleagues.
Ivy integration for Git
The main two commands in ivy that I use for Git are:
counsel-git
: select a file tracked by Gitcounsel-rg
: grep for a line in all files tracked by Git, using ripgrep as the backend.
There are many alternatives to counsel-rg
that use a different
backend: counsel-git-grep
, counsel-ag
, counsel-ack
,
counsel-pt
. But counsel-rg
is the fastest, especially when I have
to deal with Git repositories that are 2Gb in size (short explanation:
it's a Perforce repo with a bunch of binaries, because why not; and
I'm using git-p4 to interact with
it).
Using .ignore
with ripgrep
Adding an .ignore
file to the root of your project can really speed
up your searches. In my sample project, I went from 10k files to less
than 500 files.
Example content:
/TAGS
*.min.js*
/Build/Output/
/ThirdParty/
As you can see, both file patterns and directories are supported. One
other nifty thing that I discovered only recently is that you can use
ripgrep
as the backed for counsel-git
in addition to
counsel-rg
. Which means the same .ignore
file is used for both
commands. Here's the setting:
(setq counsel-git-cmd "rg --files")
And here's my setting for counsel-rg
:
(setq counsel-rg-base-command
"rg -i -M 120 --no-heading --line-number --color never %s .")
The main difference in comparison to the default
counsel-rg-base-command
is -M 120
which means: truncate all lines
that are longer than 120 characters. This is really helpful when Emacs
is accepting input from ripgrep
: a megabyte long line of minified JS
is not only useless since you can't see it whole, but it will also
likely hang Emacs for a while.
Outro
I hope you found these bits of info useful. Happy hacking!