Taken together, I can make my default search exactly what I wanted in the first place: case-insensitive, recursive, and ignoring non-project files. ripgrep also supports an option, -smart-case, that ignores case unless there’s an uppercase character in the search pattern. Ripgrep lets me set default flags in a configuration file. But I can go a step further and get something even nicer to use by combining two of ripgrep’s niceties. There’s lots of ways to deal with this problem, such as using an alias (which, to be fair, I could’ve done for Grep). There’s one thing I don’t love about the defaults though, which is that I have to specify a case-insensitive search. Specifying a file type instead of a particular filename pattern.Searching the current directory, by default.$ rg -type =md -files-with-matches -ignore-case 'schema' # shorter, more cryptic version $ rg -lit md 'schema'Īs it comes, ripgrep’s flags and options are very similar to Grep’s, so I didn’t need to learn many new ones. Here’s a few more attempts to work my way to searching only the repository files, case insensitively: I can fix these problems, but the resulting commands get complex. This is by far the most common way I search, but I have to remember to do it every time (or invent an alias, and remember a new command). I have to specify a recursive search on the current directory (the -recursive option, plus the trailing dot). Though it doesn’t show in this output, the results include only files containing schema and not Schema or SCHEMA. The results don’t include some of the files I was most interested in because the defaults assume a case-sensitive search, which I don’t want. In fact, they’re excluded from my repository with a. I don’t care about those files because they’re not my project files. The results include lots of non-project files from my dependencies, which are the lines starting with node_modules. There are a few problems with this search: worktrees/meta/node_modules/ajv/README.md worktrees/meta/node_modules/eslint/CHANGELOG.md worktrees/meta/node_modules/json-schema-traverse/README.md worktrees/meta/node_modules/js-yaml/README.md worktrees/meta/node_modules/js-yaml/CHANGELOG.md node_modules/json-schema-traverse/README.md node_modules/better-ajv-errors/README.md # In practice, I’d probably use the shorter, more cryptic form: $ grep -lR -include = '*.md' 'schema'. # For clarity, I’ve used the long form for all options in this article. $ grep -recursive -files-with-matches 'schema' -include = '*.md'. Historically, I might’ve started with something like this: To demonstrate, I’m going to search a project I contribute to a lot, mdn/browser-compat-data, for the string schema. Here’s an example: suppose I want to get a list of all the Markdown files in a repository containing a specific word. The failures of a traditional code search In particular, I’m a fan of ripgrep, but each offers a similar set of benefits over the traditional tools. Running faster, especially on large Git repositories.While Grep is a general-purpose search tool, these alternatives set themselves apart with conveniences for human users searching code. git-grep ( git grep, within Git repositories only).There are several Grep-like code search tools that are worth your time, including: Wouldn’t it be nice if there was a faster, more convenient, and easier-to-configure code search utility? I’ve got good news: there are several of them that you can try! Try a Grep alternative, such as ripgrep Your skills in one environment might not do you any favors in another. A Mac’s grep (from BSD) is slightly different from a Linux machine’s grep (from GNU). What’s worse, many traditional tools aren’t strictly compatible between systems. It’s a lot to keep in your head for a seemingly simple task. As a mortal, you know that find’s syntax is unusual and searching Grep’s man page for the right flags is tiresome at best. You know there must exist a correct command-line incantation, but you’re not a wizard. Perhaps you think to yourself, I’ll use find and grep to solve this problem. Suppose you need to recursively search your docs-as-code (or other static site) files for some keyword, and you want to exclude some file types or directories. Better-than-Grep tools for writers and developers alike
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |