1

After remapping / to something other than search, how can I open the builtin search interface?

Normally in Vim, when you press /, a / appears in the bottom-left (where the command line also would be), and you can type the search pattern there, before pressing enter to submit the search.

If / has been remapped so that it no longer does this, how can I programmatically open this same interface that / normally would?

4
  • Maybe could you tell us what you want to achieve. Do you want to have access to search in normal mode? Do you want to access search in mappings? Mar 2 at 9:11
  • @VivianDeSmedt I rebound / to :echo hi, now how can I actually open the search interface?
    – minseong
    Mar 2 at 9:37
  • Then the solution I proposed could help you? Did it? Mar 2 at 9:58
  • 1
    @VivianDeSmedt It doesn't open the search line, just submits a search itself. I've added a little to my question, I hope that makes it clearer what I mean
    – minseong
    Mar 2 at 10:23

2 Answers 2

4

Remapping

I would use another mapping with nnoremap

nnoremap , /

To search foo you could then do: ,fooEnter

Vimscript

You could use feedkeys with mappings turned off:

call feedkeys('/', 'n')

normal!

You could use the :normal! command:

To search foo you could do :normal! /fooEnter

Search History

You could also use the search history: q/

To search foo you could do q/ifooEnter

Final remark:

In the :normal mode you can use the :normal! version.

:normal! /foo^M
3
  • Oh you're right, I hadn't read this properly earlier, sorry. nnoremap works but norm! does not. Would you know how to open search from the command line? For some reason :norm! / does not work.
    – minseong
    Mar 2 at 10:27
  • 1
    Do you know why :nmap / <NOP> then :norm! / does not open search?
    – minseong
    Mar 2 at 13:56
  • 1
    My understanding of the :normal command is that after the argument an additional <Esc> character is appended. This make that you can use it to search but not to open the search. :norm! / is conceptually equivalent to :call feedkeys("/\<Esc>", 'n') although in my test it is not since :call feedkeys("/\<Esc>", 'n') seems to be the same as :call feedkeys("/\<CR>", 'n') for some reason that I ignore. Mar 2 at 14:19
2

In addition to the solutions already covered by Vivian, you could also use Ctrl-V/. Hitting Ctrl-V will also suppress mappings on the following key(s). See :help c_CTRL-V for details.

It should be noticed that pressing Ctrl-V will also switch to blockwise visual mode. You would have to cancel out of that after your search.

4
  • This is the command-line mapping for inserting characters literally into the command line, e.g. ^M (enter) or ^[ (Esc). I don't think it will help to open search.
    – minseong
    Mar 2 at 16:17
  • 1
    @minseong have you tried? Because I have and it does - at least in original Vim. Maybe the help section is off. But then :help CTRL-V is about visual mode, hmm.
    – Friedrich
    Mar 2 at 16:20
  • 1
    I confirm that it works on both Vim and Neovim. It has one downside that it switch Vim and Neovim to Visual Block mode. Mar 2 at 18:32
  • 1
    @VivianDeSmedt you're right. And I hadn't even noticed. It's probably not what anybody would want (unless you want to search for the end of a visual block). I'll edit.
    – Friedrich
    Mar 2 at 18:39

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.