4

I've created the following simple mapping in Normal mode, since I don't want to enter \v for "very magic" regex parsing, every time I use the / search command:

nnoremap / /\v

However, after having added the mapping, pressing / moves the cursor one character to the left, like it would do if I'd entered /\v manually.

Is there a way to make nnoremap / /\v behave like standard /? I want to search without moving the cursor one character to the left (and highlightning it).

8
  • 3
    This is a long-standing issue where \v causes incremental search to match everywhere, if I recall correctly
    – D. Ben Knoble
    Sep 26, 2020 at 16:10
  • 2
    The displacement unit is a character. Apparently the cursor moves one character to the right. "Slightly" does not make much sense. I don't mean to be pedantic, it is just that the more accuratedly you can describe your problem, the better for other people to know if they can reproduce it or not.
    – Quasímodo
    Sep 26, 2020 at 21:06
  • 1
    I can't reproduce this case. Your mapping works fine for me. Try start vim with NONE config file and try map again.
    – Alex Kroll
    Sep 28, 2020 at 6:25
  • 1
    please provide an example of what you mean with displaced cursor Sep 28, 2020 at 7:29
  • 2
    @ChristianBrabandt with vim --clean, add something to a buffer, put your cursor on s, then /\v -- your cursor is now on o
    – Maxim Kim
    Sep 28, 2020 at 10:21

1 Answer 1

1

One option is for you to only really initiate the /\v once you've actually started to search for something. You can do that by using a function that uses that uses getchar() to read one character and then feedkeys() to actually push the /\v plus the read character into the search command-line.

Something like the following should work:

function! SearchBox()
    " Replace the prompt:
    echon '/'

    " Get an initial search character.
    let c = nr2char(getchar())

    " TODO: Check whether it's a special character, whether
    " we would like to handle it differently. For example:
    if c ==# "\<CR>"
        " Repeat last search.
        call feedkeys('/'.c, 'n')
        return
    elseif c ==# "\<Esc>" || c ==# "\<C-c>"
        " Cancel search.
        call feedkeys("/\<C-c>", 'n')
        return
    endif

    " When we're happy, actually start a search:
    call feedkeys('/\v'.c, 'n')
endfunction

nnoremap <silent> / :call SearchBox()<CR>

You might need better handling of corner cases. I also didn't cover searching backwards with ?. You might want to cover using / from Visual mode or operator-pending mode.

But you get the idea. Something like this should address adding \v by default to searches, while not doing so too early, and also possibly helping with some of the corner cases that using the nnoremap / /\v mapping doesn't cover, such as repeating the last search with /.

4
  • I am not sure, if this is actually a problem or not, but perhaps it would be possible to map /\v to first move the cursor one char to the left and then use the /\v? Sep 30, 2020 at 7:36
  • 1
    @ChristianBrabandt the general issue is that, for some reason, \v on it’s own matches like . does—if that were fixed, there would be no other issue (the issue OP describes is even more egregious with hlsearch set)
    – D. Ben Knoble
    Sep 30, 2020 at 12:13
  • 1
    @D.BenKnoble yes it matches everythin, this is similar to \zs or other asserts. Therefore the cursor moves. Vim has some logic builtin to prevent hlsearch highlighting everything. Sep 30, 2020 at 12:48
  • @ChristianBrabandt ah didnt realize the hlsearch thing had been fixed. That’s nice
    – D. Ben Knoble
    Sep 30, 2020 at 12:56

Your Answer

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

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