![]() Here are the steps I took to figure out what I needed to do to get jump-to-grep working. If you move the cursor over one of the links and hit enter, Emacs should open that file and take you directly to that line.Įvery time I spelunk through my Emacs config to get something working, I try to learn a little bit more about how Emacs works and how I can get better at modifying it to suit my needs. You should see results from across your entire Git repo, not just the subdirectory you’re currently in. ![]() Save and exit Emacs, then open up a file in some subdirectory of one of your Git repositories. (global-set-key (kbd "C-x C-g") 'grep-find) Add the following to your Emacs configuration file I’d prefer something shorter, like C-x C-g. I expect to use grep a lot, and I’d rather not have to type M-x grep-find every time. In this case, we’re starting the cursor at the 27th character of the string - right between the '', which is where we’ll type our search expression.Īdd a nicer keybinding than M-x grep-find. 27 rather than just passing the command as a string? That part just tells Emacs where we want our cursor to start when we call M-x grep-find. So $(git rev-parse -show-toplevel || pwd) will be the root directory of the current Git repo if we’re in a Git repo, or the current directory if we’re not. In this case, our second command is pwd, which will print the directory we’re currently in ( pwd is short for Print Working Directory). cmd1 || cmd2 will run cmd1, then run cmd2 if cmd1 returned an error. ![]() We could just leave it there, but in the rare case that I’m searching outside of a Git repo I don’t want this command to return an error.īash (like most shells) allows us to recover from errors using the || operator. If we’re in a Git repo we can use git rev-parse -show-toplevel to get the repo’s root directory, but this command will return an error if we’re not in a Git repo. rg will default to searching the current directory, but I want it to check the entire current Git repo (I want to see results from my whole project, not just the directory of the file I happen to have open). The final argument we supply to rg is the directory to search. In this command, we start with an empty regular expression ( ''), which we will fill in whenever we call M-x grep-find. e tells ripgrep that the next argument should be regular expression to search for. This is important because Emacs will only let us jump to a match if we have the complete file path and line number all together. no-heading tells ripgrep to display the file name on each line, rather than grouping matches by file and only displaying the filename at the top of each group. H tells ripgrep to display the file name for each match. n tells ripgrep to display the line number for each match (the line in the file on which the match appeared). To start, we’re calling rg (this was our main goal: to call ripgrep). The value that we’re passing is this complicated object '("rg -n -H -no-heading -e '' $(git rev-parse -show-toplevel || pwd)". At a high level, we’re using the grep-apply-setting function to tell Emacs what command should be run when we run M-x grep-find. There’s a lot going on here, so let’s unpack it. '("rg -n -H -no-heading -e '' $(git rev-parse -show-toplevel || pwd)".
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |