0

I am using :grep to search for occurences of a string within a set of files:

:grep rainbow *.txt

This result is then shown in the terminal:

doc1.txt:2:saw a rainbow today
doc2.txt:17:it was not a black rainbow
doc3.txt:7:most of those rainbow unicorns
(1 of 3): saw a rainbow today
Press ENTER or type command to continue

To get back to Vim, I need to press Enter. I don't want to have to press Enter. Is there a way to prevent the prompt?

I know I can do this to prevent the prompt:

:silent :grep rainbow *.txt
:redraw!

But that's a lot of extra letters to have to type.

5

If what you're looking for is a way to automate the sequence of commands (:silent grep and redraw!), then consider creating a new user-defined command for that operation.

(In fact, Vim documentation essentially recommends it for this particular use case with their NewGrep example.)

My recommendation would be:

command! -nargs=+ -complete=file Grep
    \ execute 'silent grep! <args>' | redraw! | cfirst

This would:

  • Define :Grep as the new command.
  • Sets up command completion to search for files, which matches the type of completion available for :grep.
  • Use :silent and :redraw! as expected.
  • Use :grep! and jump to the first target explicitly with cfirst. This is done so you'll see the line (1 of 3): saw a rainbow today after jumping to the first result.

If you like to work with the quickfix window most of the time, consider using copen instead of cfirst to open that window directly.

| improve this answer | |
  • 1
    Also consider cwindow instead of copen. cwindow does not open the quickfix window when nothing was found. – Flux Aug 28 '19 at 0:16
1

Well, you need to do a redraw...per :h :!cmd...

This requires a hit-enter prompt, so that you can read any messages. To avoid this use :silent !{cmd}. The screen is not redrawn then....

But if you're simply looking to reduce the number of keys to press perhaps I can interest you in an 89% reduction of the second command. A single key combo...

Ctrl+L

...thus you have to use CTRL-L or :redraw! if the command did display something.

| improve this answer | |

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.