15

Is it possible to use Ctrl+n style auto-completion when doing searches and substitution commands in vim?

What I mean is: say I have a variable named myNumber in my current file. If I enter insert mode and type myNu and then hit Ctrl+n, it will autocomplete the word to myNumber (assuming there are no other words that start with those letters).

Is there any way to get this same functionality on the command line? Is there a setting or plugin that will allow me to type /myNu and then hit a key to complete my text search to /myNumber ? Or, more realistically: let me type :s/myN and complete that to :s/myNumber so I can more quickly type out substitution commands?

3rf
  • 1,154
  • 7
  • 15
  • 1
    Yes, there are at least two plugins for that. Vim.org's script part has a search feature. Use it. – romainl Nov 01 '13 at 16:12
  • 1
    I tried to search for it there but it was hard to know what keywords to use, so I kept getting results for other kinds of autocomplete plugins. Thanks for being so helpful though!! – 3rf Nov 01 '13 at 17:03
  • You want something that does "completion" in the "command line" so you search for "command line completion" and you skim through the descriptions until you find a plugin that does what you want (one on the second page and another one on the third page). The whole process shouldn't take more than a couple of minutes. – romainl Nov 01 '13 at 17:31
  • 1
    @romainl: Maybe it 'shouldn't', but sometimes it does—plugins don't always play nicely with each other. And I think the OP was about whether vim supports this out of the box, an expectation which, IMHO, is quite reasonable... unlike the tone in your comment. – Michael Scheper Oct 14 '16 at 18:38

3 Answers3

30

While in command mode, press Ctrl+f — you will enter command line window. There you can edit your command like in vim. Auto-completion should work too.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • 2
    You can also get there from normal mode with `q:`/`q/` – Kevin Nov 01 '13 at 17:25
  • nice combination of features here! – JonnyRaa Oct 06 '17 at 14:26
  • 1
    @Kevin, `q:` will actually give you command history from normal mode, while `q/` — search history from normal mode (to search forward) and `q?` — search history from normal mode (to search backward). `Ctrl + f` will open command line window with a command history from command mode. So `q:`, `q/` and `Ctrl + f` are not quite the same. –  May 24 '19 at 09:34
  • Why can't I get C-f to work? I hit `:%s` then `C-f` but get nothing – 0fnt Oct 26 '21 at 21:09
  • So looks like some sort of config issue. My unconfigured nvim instance processes C-f just fine – 0fnt Oct 27 '21 at 05:01
4

Looks like the plugin http://www.vim.org/scripts/script.php?script_id=2222 does more or less exactly what I wanted. Upvoting Kent though, because that's really nifty.

3rf
  • 1,154
  • 7
  • 15
1

This plugin also enables auto-completion in command line.

https://github.com/vim-scripts/sherlock.vim

Naga Kiran
  • 8,585
  • 5
  • 43
  • 53