How do I search for the word under my cursor across all project files using Telescope?
Answer
require('telescope.builtin').grep_string()
Explanation
Telescope's grep_string() function performs a project-wide search for the exact word currently under the cursor, displaying live-previewed results in an interactive picker. This is the Telescope equivalent of pressing * followed by :grep, but with instant visual feedback.
How it works
require('telescope.builtin').grep_string()calls Telescope's built-in grep function- By default, it searches for
<cword>— the keyword under the cursor — usinggreporripgrep - Results appear in a fuzzy-searchable picker with a live file preview
- You can further filter results by typing in the Telescope prompt
- When
ripgrepis available, Telescope uses it automatically for speed
Example
Map it to a convenient leader key in your Neovim config:
vim.keymap.set('n', '<leader>fw', function()
require('telescope.builtin').grep_string()
end, { desc = 'Find word under cursor' })
Or to search for an arbitrary word from a prompt, use live_grep instead:
vim.keymap.set('n', '<leader>fg', function()
require('telescope.builtin').live_grep()
end)
Tips
- Pass
{ search = vim.fn.expand('<cWORD>') }to search for the full WORD (including dots and slashes) instead of the keyword - Add
{ word_match = '-w' }to enforce whole-word matching with ripgrep - In Telescope results,
<C-q>sends all matches to the quickfix list for further processing - Combine with
:cdofor project-wide refactoring