0

Using atom I can CtrlShiftf while a word is selected in order to start a multi file search for a matching string.

I would like to emulate this flow as much as possible with Vim.

I know I can yank the text, type: :grep -F and then paste it using <ctrl+r>"

Is there a way to do this in fewer keystrokes?

1

If you are on a *nix system you can use the Ags plugin that uses the Ag external program.

Calling :Ags without arguments will search in all the files the word under the cursor.

The downfall of Ags is that it search only in the path of the currently opened file, but, if you want to search all files in the directory root (like a git repo) you can add the vim-rooter plugin.

  • Here is the [vim-rooter] (github.com/airblade/vim-rooter) plugin – Atropo Oct 19 '16 at 9:27
  • That's not quite true. You can tell the Ag Plugins to always search in the root directory of your project by setting let g:ag_working_path_mode="r". – Philipp Moers Nov 18 '16 at 0:00
  • I second this plugin and Ag itself, they do a great job. I configured it so I can search for the selected text across my whole project with one keystroke: xnoremap / y:<c-u>Ag <C-R>=shellescape(expand(@"),1)<CR><CR> – Philipp Moers Nov 18 '16 at 0:05
0

You can reference the word/WORD under the cursor with:

<C-r><C-w>
<C-r><C-a>

So instead of yanking, you can directly have:

:grep -F <C-r><C-w>

Or even shorter with a mapping:

nnoremap <C-f> :grep -F "<C-R><C-W>"

Now if you want to search for a visually selected part of your file you can automate the process also:

vnoremap <C-f> "ay:grep -F "<C-r>a"

Note: In those mapping I've not put a <CR> at the end so you can see the result (and maybe change it) before starting the search. But if you want, adding a <CR> at the end of the mapping will run the command automatically.

see :h c_CTRL-R_CTRL-W

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.