1

The following is the functionality I am attempting to implement.

1. What works (normal mode):

  • Search/highlight the word the cursor is on.
  • Replace the word the cursor is on.
    • Note that the word doesn't get erased when I get prompted to replace it, allowing me to make a small correction to the existing word. I obviously have a choice to erase it and input a desired replacement.
vim.keymap.set("n", "<leader>s", "*N")
vim.keymap.set("n", "<leader>r", [[:%s/\<<C-r><C-w>\>/<C-r><C-w>/gI<Left><Left><Left>]])

2. What somewhat works (visual mode):

  • Replace all other instances of selected text.
    • Here, the word gets erased when I get prompted to replace it. How can I change that so that the behavior is just like in my command for normal mode? I want this behavior in case I just want to make a small modification to a word such as appending something or changing a letter.
vim.keymap.set("v", "<leader>r", [["hy:%s/<C-r>h//g<Left><Left>]])

EDIT

The solution to this part was offered by Vivian De Smedt. The keymap that leads to the desired result is:

vim.keymap.set("v", "<leader>r", [["hy:%s/<C-r>h/<C-r>h/gI<Left><Left><left>]])

3. What doesn't work (visual mode):

  • Search/highlight all other instances of selected text.
    • Here it seems like my command from normal mode gets activated, and only the word the cursor is on gets highlighted instead of whatever was selected in visual mode. How can I fix this?
vim.keymap.set("v", "<leader>s", "*N")

The GIF below illustrates this problem. Doing <leader>s while in normal mode searches for the word the cursor is on (the first two demos in the GIF). That is the desired behavior. In visual mode, however, instead of performing the search on the whole selection, it still only searches the word the cursor is on (the third demo in the GIF). That is not what is desired. In that particular example, it should have searched for vim.key not keymap.

enter image description here

EDIT

The solution to this part was also offered by Vivian De Smedt. The keymap that leads to the desired result is:

vim.keymap.set("v", "<leader>s", [["hy:let @/=@h<CR>nN]])

1 Answer 1

1

In order to mimic what you have done for normal mode I would do:

vim.keymap.set("v", "<leader>r", [["hy:%s/<C-r>h/<C-r>h/gI<Left><Left><left>]])

It seems to me that you could be interested to the vim-visual-start-search plugin that offers a similar functionality.

For the *N functionality the problem is that the mapping doesn't provides the desired functionality. The * key make Neovim goes to the next occurrence of current word and not the next occurrence of the current selection.

If it is only to highlight the search you could do:

vim.keymap.set("v", "<leader>s", [["hy:let @/=@h<CR>nN]])

The same technique could also be applied in normal mode:

vim.keymap.set("n", "<leader>s", [["hy:let @/=@h<CR>nN]])
11
  • @Vladimir, does that answer helps you? What remains open in your question? Dec 12, 2023 at 16:13
  • 1
    This solves my first question. Do you know how to fix my keymap in the second question? Doing *N on a selected text produces the desired behavior in visual mode. However, when I try to map that to <leader>s in visual mode it does not...
    – Vladimir
    Dec 12, 2023 at 19:03
  • I can't reproduce your problem. Could you give us the result of the command: echo "'" . g:mapleader . "'" and the command: verbose vmap <leader>s? Dec 13, 2023 at 8:59
  • 1
    I have updated the solution to quit visual mode and also added nN to force the hlsearch mode. I can't reproduce your it force me to press enter to confirm effect. Maybe you could elaborate on that such that I can reproduce it myself. Dec 18, 2023 at 7:00
  • 1
    This works: vim.keymap.set("v", "<leader>s", [["hy:let @/=@h<CR>nN]]) for visual mode. For normal mode I will still keep "*N". Thank you a lot. You've answered everything I needed to know. :)
    – Vladimir
    Dec 19, 2023 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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