使用 pss 提升你的代码搜索能力
pss 代码 搜索
![](/data/attachment/album/201712/28/213536kijg800zzz8rjvrz.jpg)


搜索代码库是开发者每天都要做的事情。从修改 bug 到学习新代码，或者查看如何调用某个 API，能快速在代码库中导航的能力都是一大助力。幸运的是，我们有专门的工具来搜索代码。[pss](https://github.com/eliben/pss) 就是其中一个工具，让我们来看看如何安装和使用它吧。


### 什么是 pss？


`pss` 是一个帮你在源代码文件中进行搜索的命令行工具。`pss` 递归地在目录树中进行搜索，它能自动根据文件名和后缀判断哪些文件需要搜索，哪些文件不需搜索，并且会自动跳过那些你不会想搜索的目录（比如 `.svn` 和 `.git`），还能用色彩渲染输出以方便人们阅读，以及其他很多功能。


### 安装 pss


使用下面命令在 Fedora 上安装 `pss`：



```
 $ dnf install pss

```

安装好后就能在终端调用 `pss` 了：



```
 $ pss

```

不带参数调用 `pss` 或者带上 `-h` 标志会输出详细的使用说明。


### 使用案例


现在你安装好 `pss` 了，下面来看一些例子吧。



```
 $ pss foo

```

该命令只是简单的搜索 `foo`。你也可以限制 `pss` 让它只在 python 文件中搜索 `foo`：



```
 $ pss foo --py

```

还能在非 python 文件中搜索 `bar`：



```
 $ pss bar --nopy

```

而且，`pss` 支持大多数常见的源代码文件类型，要获取完整的支持列表，执行：



```
$ pss --help-types

```

你还能指定忽略某些目录不进行搜索。默认情况下，`pss` 会忽略类似 `.git`，`__pycache__`，`.metadata` 等目录。



```
$ pss foo --py --ignore-dir=dist

```

此外，`pss` 还能显示搜索结果的上下文。



```
$ pss -A 5 foo

```

会显示匹配结果的后面 5 行内容。



```
$ pss -B 5 foo

```

会显示匹配结果的前面 5 行内容。



```
$ pss -C 5 foo

```

会显示匹配结果的前后各 5 行内容。


如果你想知道如何使用 `pss` 进行正则表达式搜索以及它的其他选项的话，可以在[这里](https://github.com/eliben/pss/wiki/Usage-samples)看到更多的例子。




---


via: <https://fedoramagazine.org/improve-code-searching-skills-pss/>


作者：[Clément Verna](https://fedoramagazine.org) 译者：[lujun9972](https://github.com/lujun9972) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Searching a code base is a part of every day developer activities. From fixing a bug, to learning a new code base or checking how to call an api, being able to quickly navigate your way into a code base is a great skill to have. Luckily, we have dedicated tools to search code. Let’s see how to install and use one of them – [ pss](https://github.com/eliben/pss).

## What is pss?

**pss** is a command line tool that helps searching inside source code file. ** pss **searches recursively within a directory tree, knows which extensions and file names to search and which to ignore, automatically skips directories you wouldn’t want to search in (for example

or

), colors its output in a helpful way, and much more.

## Installing pss

Install **pss** on Fedora with the following command:

$ dnf install pss

Once the installation is complete you can now call **pss **in your terminal

$ pss

Calling **pss** without any argument or with the *-h *flag will print a detailed usage message.

## Usage examples

Now that you have installed **pss** let’s go through some Usage examples.

$ pss foo

This command simply looks for

. You can be more restrictive and ask pss to look for

only in python files:

$ pss foo --py

and for

in all other files:

$ pss bar --nopy

Additionally,** pss **supports most of the well known source file types, to get the full list execute:

$ pss --help-types

You can also ignore some directories. Note that by default, **pss** will ignore directories like .git, __pycache__, .metadata and more.

$ pss foo --py --ignore-dir=dist

Furthermore,** pss **also gives you the possibility to get more context from your search using the following :

$ pss -A 5 foo

will display 5 line of context after the matching word

$ pss -B 5 foo

will display 5 line of context before the matching word

$ pss -C 5 foo

will display 5 line of context before & after the matching word

If you would like to learn how to use **pss ** with regular expression and other options, more examples are available [here](https://github.com/eliben/pss/wiki/Usage-samples).

## Joao Rodrigues

So… it’s like grep

$ dnf install pss

$ dnf install grep (no need to install grep; it’s already installed)

$ pss (or $ pss -h)

$ man grep ( or $ info grep)

$ pss foo

$ grep -nR foo .

$ pss foo –py

$ grep -nR –include=’*.py’ .

$ pss foo –nopy

$ grep -nR –exclude=’*.py’ .

$ pss foo –py –ignore-dir=dist

$ grep -nR –include=’*.py’ –exclude-dir=dist .

$ pss -A 5 foo

$ grep -A 5 -nR foo .

## Clément Verna

Hi Joao,

Indeed it very similar to grep, and also inspired by ack. One of the extra feature is that pss will nicely format the output making it easy to interpret the search result.

Luckily for developers there is a great choice of tools available to search code. The link below lists some of the more popular.

https://beyondgrep.com/more-tools/

## Cody

And since it’s in Python it’ll be a lot slower too, yes? Of course I don’t have an example of the formatting but one can do that with other tools too (and as for git there is

).

Not saying this tool is useless but the examples shown here are nothing

can’t do and it’s quite similar too (as someone else showed; I was going to say the same only I wasn’t first).

Just a suggestion: show something that can’t be done in

or that is done much more easily than with

? Would give people a better idea as to why it might be adopted (although I suspect many old timers will never go for it in the long run – I include myself in that group – there are many who might actually appreciate it but only if they can see its power).

## Cody

Seems I messed up the formatting too. Maybe you can fix it? Not even sure what triggered that (I suppose something to do with the grave accent but I don’t know for sure as I don’t have what I actually typed).

## Clément Verna

Hi Cody,

I have edited your comment it should be correctly formatted now 🙂

## Guus Bonnema

Pss seems almost identical to ack. However, it has no man page. For me manpages are a lifesaver while developing. This looks like a serious bug to me.

Will pss ever have a non-trivial manpage, describing its functions, options and error codes?

## Robert

Looks nice, but I just use my text editor such as Atom, Sublime Text, Visual Code, etc

## Rasmus Kaj

For anyone wanting something similar to pss, but extremely fast (even faster than grep in most cases), I’d like to recommend ripgrep (a.k.a.

).

https://github.com/BurntSushi/ripgrep/

Available in the ripgrep package from the rust-sig repo on Fedora.

## Stephen Hill

Reading the title I had hoped it was more like an evolution of cscope or the GNU “ID utilities” ( http://www.gnu.org/software/idutils/ ).

Personally I found cscope to be just as vital as air in working on a very large project for many years. I had hoped to find something like that for home use on Linux ( cscope was not free at the time, though I could have “borrowed” the source ) and eventually found the ID utils. It was not nearly as friendly as cscope, but still useful to me.

The pss tool sounds like it would be useful too, if you don’t have things like cscope or idutils installed.

## Nauris

I work for a software company almost 18 years. Worked with tons of code and different compilers. Today I just smile when a developer asks me to install 16GB of RAM on his computer just to get Visual Studio working. However, I’m wondering does somebody nowadays still writes his code on terminal and stores source code locally in directory structure? Graphical IDE just has so much advantages to be more productive on daily basis.

I understand that sometimes you have to connect to a remote server and build something on specific HW/kernel and it may require patching code directly, however even so, there is a text mode tools for that, so you don’t have to grep or pss anything.

Or am I just another windows nerd? 😀

## Andy Lester

If that’s how you work, sure. For many many many of us, working with command line tools is much more powerful.

## Creak

Comparing

and

(two competitors to

), I think I still prefer

: https://geoff.greer.fm/ag/

It’s written in C, with performance in mind. And it is, incredibly, fast.

And the killer argument: “the command name is 33% shorter than ack” 😀

## Andy Lester

I’d like to emphatically say that ag and pss are not competitors to ack. There is no competition. I just wrote a blog post about this last night: http://blog.petdance.com/2018/01/02/the-best-open-source-project-for-someone-might-not-be-yours-and-thats-ok/

Anyone wanting to see a list of many other code-searching tools, see https://beyondgrep.com/more-tools/

## Jeff C

another old but powerful tool is cscope, which is more language-aware for c/c++ projects. It feels fairly old but is more powerful than just regex searching