如何使用 git grep 命令默认显示行号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11107059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:05:53  来源:igfitidea点击:

How to show line numbers by default with git grep command?

git

提问by jfisk

I know i can use the -n flag in git grep to show the line numbers of the results, i.e

我知道我可以在 git grep 中使用 -n 标志来显示结果的行号,即

git grep -n "some string"

but how could i include this flag by default without resulting to making an alias? I found git config --global grep.lineNumber truebut it doesnt seem to be working for me.

但是如何在默认情况下包含此标志而不导致创建别名?我找到了,git config --global grep.lineNumber true但它似乎对我不起作用。

采纳答案by jfisk

I set my grep.lineNumberusing your command and it worked for me.

我设置了我grep.lineNumber使用你的命令,它对我有用。

Have you considered using ack? It's significantly more programmer friendly and comes in the repositories of most major linux distributions.

你考虑过使用ack吗?它对程序员更加友好,并且出现在大多数主要 linux 发行版的存储库中。

回答by ThorSummoner

From the git grep manual:

从 git grep 手册:

Options:

   -n, --line-number
       Prefix the line number to matching lines.


Configuration:

   grep.lineNumber
       If set to true, enable -n option by default.

To turn on globally: git config --global grep.lineNumber true

全局开启: git config --global grep.lineNumber true

回答by Kit Ho

Under .gitconfig file

在 .gitconfig 文件下

add this section

添加此部分

[grep]                                                                                                                           
  linenumber = true        

line number would be showed if you using git grep

如果您使用 git grep,将显示行号

回答by Even Young

The command you use

你使用的命令

git config --global grep.lineNumber true

is the correct way to enable -n option by default.

是默认启用 -n 选项的正确方法。

But this feature is not available until v1.7.5-rc1. Check your local document for more information.

但是这个功能直到 v1.7.5-rc1 才可用。查看您当地的文件以获取更多信息。

git grep --help

Also if you are interested, here is the commit introduced this feature:

另外,如果您有兴趣,这里是介绍此功能的提交:

grep: allow -E and -n to be turned on by default via configuration

grep:允许通过配置默认开启 -E 和 -n