您的位置:首页 > 其它

vim a.vim如何使用_我如何使用ripgrep使VIM的全项目搜索变得无缝

2020-08-20 13:41 746 查看

vim a.vim如何使用

Yes, I ditched grep & the_silver_searcher(ag) for ripgrep.

是的,我放弃了grep和the_silver_searcher(ag)作为ripgrep。

Whether you're forced to use VIM at your workplace or you're a mad VIM fan like I am who obsesses about productivity, the project-wide keyword search is a basic requirement every developer needs in their editor's arsenal. And we expect it to be blazing fast. ⚡️

无论您是被迫在工作场所中使用VIM,还是像我这样疯狂的VIM爱好者,都对生产力着迷,整个项目范围的关键字搜索是每个开发人员在其编辑机构中都需要的基本要求。 我们希望它能快速发展。 ⚡️

I've been using VIM for about 3 years now. And coming from a Sublime background, the need for project-wide search was essential.

我已经使用VIM大约3年了。 由于具有崇高的背景,因此在整个项目范围内进行搜索至关重要。

ripgrep and ack.vim were things I adopted early on after my unfulfilling experiences with grep and the_silver_searcher(ag). I haven't looked back since then.

ripgrep 一个 ck.vim是我的事情后我用grep和the_silver_searcher(AG)不过瘾经验提早采纳。 从那以后我再也没有回头。

This article is the result of experimenting with different search tools and incremental improvements I made over a period of time until it felt just right.

本文是在一段时间内尝试使用不同的搜索工具和逐步改进的结果,直到感觉还不错。

为什么选择ack.vim和ripgrep? (Why ack.vim & ripgrep?)

  1. Fast: I've worked on Symfony and JavaScript projects with thousands of files and it is just blazing fast. Here's a quick comparison with other search tools.

    快速:我从事过数以千计的文件的Symfony和JavaScript项目,而且发展很快。 这是与其他搜索工具的快速比较

    My benchmark for speed is, "it should never feel slow". I noticed a tremendous improvement after I moved from grep, the_silver_searcher and ack.

    我对速度的基准是“永远不要感到慢”。 从grep,the_silver_searcher和ack移出后,我注意到了巨大的进步。

  2. Quick navigation: ack.vim takes care of populating the Quickfix list, which lets you conveniently move through all those search results across different files.

    快速导航 : ack.vim负责填充“ 快速修复”列表,该列表使您可以方便地在不同文件中浏览所有这些搜索结果。

  3. Sensible defaults: ripgrep by default considers gitignore and automatically skips hidden files/directories and binary files.

    合理的默认值: ripgrep默认情况下考虑gitignore并自动跳过隐藏的文件/目录和二进制文件。

总览 (Overview)

ack.vim is a VIM plugin that acts as a wrapper to search keywords and populate the Quickfix list for navigating the results.

ack.vim是一个VIM插件,用作搜索关键字和填充Quickfix列表以浏览结果的包装器。

ripgrep (rg) is a command-line tool that ack.vim will internally use to perform the actual project-wide search.

ripgrep(rg)是一个命令行工具, ack.vim将在内部用于执行实际的项目范围内的搜索。

脚步 (Steps)

步骤1 :安装ripgrep (Step 1: Install ripgrep)

If you prefer homebrew like I do, run the following to install rg:

若你宁可 像我一样做自制程序 ,运行以下命令安装rg:

brew tap burntsushi/ripgrep https://github.com/BurntSushi/ripgrep.git
brew install burntsushi/ripgrep/ripgrep-bin

Here's an automated script which I use as part of my dotfiles.

这是一个自动化脚本 ,我将其用作dotfiles的一部分。

If you prefer any other mode of installation, refer to ripgrep's official installation section.

如果您喜欢任何其他安装方式,请参阅ripgrep的官方安装部分。

步骤2 :安装ack.vim (Step 2: Install ack.vim)

To install ack.vim using the vim-plug package manager, add the following in your vimrc:

要使用vim-plug软件包管理器安装ack.vim,请在vimrc中添加以下内容:

Plug 'mileszs/ack.vim'

or refer to the ack.vim's installation section.

或参考ack.vim的安装部分。

步骤3 :将ack.vim配置为使用rg (Step 3: Configure ack.vim to use rg)

Add the following configuration in your vimrc:

在vimrc中添加以下配置:

" ack.vim --- {{{

" Use ripgrep for searching ⚡️
" Options include:
" --vimgrep -> Needed to parse the rg response properly for ack.vim
" --type-not sql -> Avoid huge sql file dumps as it slows down the search
" --smart-case -> Search case insensitive if all lowercase pattern, Search case sensitively otherwise
let g:ackprg = 'rg --vimgrep --type-not sql --smart-case'

" Auto close the Quickfix list after pressing '<enter>' on a list item
let g:ack_autoclose = 1

" Any empty ack search will search for the work the cursor is on
let g:ack_use_cword_for_empty_search = 1

" Don't jump to first match
cnoreabbrev Ack Ack!

" Maps <leader>/ so we're ready to type the search keyword
nnoremap <Leader>/ :Ack!<Space>
" }}}

" Navigate quickfix list with ease
nnoremap <silent> [q :cprevious<CR>
nnoremap <silent> ]q :cnext<CR>

Note:

let g:ackprg
defines the command which ack.vim will internally run. Also note that we're using
rg
here with some options. Look at the
man rg
to modify the options that may meet your requirements.

注意:

let g:ackprg
定义ack.vim将在内部运行的命令。 还要注意,我们在这里使用
rg
和一些选项。 查看
man rg
来修改可能满足您要求的选项。

To explore options for ack.vim, look into the following documentation.

要探索ack.vim的选项,请查看以下文档

用法 (Usage)

Now that we're up and ready, here are the most common use-cases:

现在我们已经准备就绪,这是最常见的用例:

在光标下寻找一个词 (Look for a word under the cursor)

Press <leader>/ followed by enter.Since we've set

let g:ack_use_cword_for_empty_search = 1
, Ack falls back to the current word under the cursor for the search, so no need to type that word.

按下<leader> /然后输入Enter 。

let g:ack_use_cword_for_empty_search = 1
我们已经设置了
let g:ack_use_cword_for_empty_search = 1
,那么Ack会退回到光标下方的当前单词进行搜索,因此无需键入该单词。

单词搜索 (Word search )

Press <leader>/  followed by the word (without any quotes) & enter.Since we're using smart case with ripgrep, that'll do a case insensitive search if the word is all lowercase, and a case sensitive search otherwise.

按<leader> /后跟单词(不带引号)并输入。 由于我们在ripgrep上使用了智能大小写,因此如果单词全都为小写,则将执行不区分大小写的搜索,否则执行区分大小写的搜索。

正则表达式搜索 (Regex search)

Press <leader>/ followed by a regex pattern in quotes & enter.

按<leader> /,然后在引号中输入正则表达式并输入。

浏览结果 (Navigation through the results )

Ack.vim populates the results in the Quickfix list, which opens up as a separate bottom window. There are multiple ways to navigate the results list:

Ack.vim将结果填充在“快速修复”列表中,该列表作为一个单独的底部窗口打开。 有多种方法可浏览结果列表:

  • You can navigate the Quickfix list using

    j/k
    and press
    enter
    to close the Quickfix list. VIM will take you to the exact location of the found word.

    您可以使用

    j/k
    浏览“快速修复”列表,然后按
    enter
    键以关闭“快速修复”列表。 VIM将带您到找到的单词的确切位置。

  • You can also use the hotkeys

    ]q
    or
    [q
    . VIM will move the cursor to the next/previous result and will open the file in a new buffer if required.

    您也可以使用热键

    ]q
    [q
    。 VIM将光标移至下一个/上一个结果,并在需要时在新缓冲区中打开文件。

    To close the Quickfix list once you're done, you can either go to the bottom Quickfix window and close it or just run

    完成后要关闭Quickfix列表,您可以转到底部的Quickfix窗口并关闭它,也可以运行

    :cclose

    :cclose

  • To open the Quickfix list back up, run

    :copen

    要备份打开的快速修复列表,请运行

    :copen

结语 (Closing Note)

And there you have it, a seamless search and navigation for your next project-wide keyword search!

在那里,您可以进行无缝搜索和导航,以进行下一个项目范围的关键字搜索!

If you're stuck anywhere, look for the respective ack.vim and ripgrep docs/issues in their respective repositories, or send me a message. Share the configuration you're proud of, so it can help others improve theirs.Here are my dotfiles.

如果您被困在任何地方,请在各自的存储库中查找各自的ack.vim和ripgrep文档/问题,或给我发送消息。 共享您引以为豪的配置,这样可以帮助其他人改善配置 。这是我的dotfiles

翻译自: https://www.freecodecamp.org/news/how-to-search-project-wide-vim-ripgrep-ack/

vim a.vim如何使用

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: