1

I often need to make a case-insensitive search of the current word (under cursor) in a dictionary file which has a simple txt format, one entry per line. One possible solution is to record a macro in a register which will make the grep for me and display the matches in the command-line mode, such as

:!grep -i word ~/path/to/dictfile.txt

where the current word can be substituted by means of ^R^W. This works, but it has some disadvantages.

  • The list of matches disappears once I hit Enter. Keeping the results in a window would be useful. The contents of the window should update when I make a new search.

  • The list of matches provides no highlighting of the search pattern, which would increase the readability.

I have tried to make a script which would output the matches to another file for viewing. However, I was not able to ensure in the script that exactly one window with the matches stays open and refreshes with a renewed grepping.

Another solution might come via the :vimgrep command and displaying the quickfix list via the :cwindow command. But there, I get lots of unwanted information (~/path/to/dictfile.txt, |linenumber col colnumber| and no highlighting.

1
0

You can send the Grep to a split terminal buffer after closing the buffer from the previous Grep (if any).

:sil!bd --color|ter grep --color -i word dictfile
  • grep --color is required to force Grep to use color.

  • bd --color deletes the buffer whose name matches "--color". This is a good keyword because it is unlikely that you will have a filename containing "--color". You could also use "grep" as the keyword, but then the command would fail if you had a buffer named "meagrepart", for example.

  • sil! prevents the command from failing if you had no "--color" terminal buffer yet.

See also vert ter for a vertically split terminal and tab ter for a terminal in a new tab.

5
  • This is a nice solution indeed; I was not aware of the terminal command. I also found in the help I can choose the number of ++rows=<number> to be displayed. Now, I wonder whether it is possible to display the terminal window below the current one, not above as it opens. Also, the terminal window inherits some options from the current window, like foldcolumn=<number> and spell=on, which I would prefer to avoid. Is there a way to change these? – user27145 Feb 11 at 11:14
  • @user27145 set splitbelow in .vimrc makes all splits create the new window below the current one. I cannot help you with the second question, though, because I never use spelling and folding. – Quasímodo Feb 11 at 13:53
  • Yes, indeed, with set splitbelow it works fine. For the options to be set locally, I found the answer from :h terminal. It is enough to add to .vimrc the line: autocmd TerminalWinOpen * setlocal fdc=0 |setlocal nospell – user27145 Feb 11 at 15:06
  • I have noticed another small issue with the grep: Whereas it works perfectly in plain shell, its output to the vim's term window appears to split long lines into chunks of about 100 characters long. I wonder whether there is a way to avoid this. – user27145 Feb 12 at 14:53
  • @user27145 I don't get exactly what you mean. In the plain shell, does your lines ever wrap if they are too long to fit the terminal? A picture could help and maybe you would like to open another question. – Quasímodo Feb 14 at 12:11

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.