Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(telescope): allow word and selection searches #1096

Merged

Conversation

SameAsMuli
Copy link
Contributor

Changes the current normal-mode mappings <leader>sw / <leader>sW to only display matches surrounded by word boundaries. Also adds the equivalent visual-mode mappings to allow searching for the current selection.

Selection searching does not consider word boundaries, so highlighting a single word and searching for it will produce the same results as the word-under-cursor searching did before this change. This allows the user to easily choose whether to include word boundaries in their search.

Changes the current normal-mode mappings <leader>sw / <leader>sW to only
display matches surrounded by word boundaries. Also adds the equivalent
visual-mode mappings to allow searching for the current selection.

Selection searching does not consider word boundaries, so highlighting a
single word and searching for it will produce the same results as the
word-under-cursor searching did before this change. This allows the user
to easily choose whether or not to include word boundaries in their
search.
@fredrikaverpil
Copy link
Contributor

@SameAsMuli Not sure if this is helpful, but I replaced <leader>/ with nvim-telescope/telescope-live-grep-args.nvim, which makes use of ripgrep, so to gain greater control: https://github.com/fredrikaverpil/dotfiles/blob/main/nvim-lazyvim/lua/plugins/telescope.lua#L17

You can then search for -w something to search with word boundary.

Just figured I'd mention it 😄

@SameAsMuli
Copy link
Contributor Author

That is helpful, I think adding a search option with more fine-grained control would definitely be worth-while! That wouldn't address the original intention behind this change, though, so I'll leave that for a separate PR.

The purpose of this change was that the current "Search Word" functionality chooses its word based on where the cursor is, i.e. it uses the word boundaries around the cursor to determine the search term. But it doesn't then use those word boundaries as part of the search - which felt wrong to me. So if my cursor is in the word vim, and I use the "Search Word" functionality, it'll currently match "vim", "nvim", "neovim", etc.

This change adds word boundaries to the "Search Word" mappings so that, in the previous example, it will now only match "vim". Whilst I was at it, though, I thought I might as well add visual selection searching, given that Telescope supports it. That also had the added bonus that if someone really did want to search for a word without the boundaries surrounding it, they could still do so by typing viw<leader>sw.

These are just handy mappings to allow searches for text immediately around the cursor. As you've suggested, a more configurable search that allows the user to freely type the search term and pass additional arguments to ripgrep would be useful. Although, I believe that would be a complimentary change to this one, rather than a replacement for it.

@folke folke merged commit 6b2c648 into LazyVim:main Jul 22, 2023
@folke
Copy link
Collaborator

folke commented Jul 22, 2023

thanks!

@SameAsMuli SameAsMuli deleted the SameAsMuli/feat/telescope_word_search branch July 23, 2023 19:19
joshmedeski pushed a commit to joshmedeski/LazyVim that referenced this pull request Sep 1, 2023
Changes the current normal-mode mappings <leader>sw / <leader>sW to only
display matches surrounded by word boundaries. Also adds the equivalent
visual-mode mappings to allow searching for the current selection.

Selection searching does not consider word boundaries, so highlighting a
single word and searching for it will produce the same results as the
word-under-cursor searching did before this change. This allows the user
to easily choose whether or not to include word boundaries in their
search.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants