4

There is some way to "extend" autocomplete functionality (like that one supplied by SuperTab plugin) to be used in command line mode so I would autocomplete when searching a text with / command?

Explanation:

When editing a text, in INSERT mode, I use now superTab plugin, so hitting TAB, a menu appear with possible completions items.

I'm looking for a similar feature when searching or editing in command mode (:command) and/or in serach mode (/searchedtext), with autocompletion, retrieving from file text words (ore previous commands!).

E.g. if I have the word validate_first and validate_firstname inside the file, I would like to have an autocompletion menu when hitting /validate, with items:

  • validate_first
  • validate_firstname

enter image description here

4
  • Could you add an example of what you currently do? And what you would like it to do? Commented Apr 6, 2017 at 17:52
  • question updated; more clear now? :) Commented Apr 6, 2017 at 19:55
  • perhaps the searchComplete plugin is what you are looking for? Commented Jan 29, 2020 at 7:06
  • It seems to me not working. What's he supposed to do searchCompleted? I do not see any behavior change when using Tab or /. Nothing happens BTW I'm using vim v.8.0 Commented Jan 29, 2020 at 12:50

5 Answers 5

4

Use Ctrlf from cmdline or open cmdline by prefixing your command with q (e.g., q/) to open the cmdline window, which is a buffer for entering Ex commands. From here, you should be able to use your completion mappings.

:help cmdline-window

You could map a command to open the window when you try to do completion in cmdline:

cmap <Tab> <C-f>a<Tab>

I'm intentionally using cmap instead of cnoremap to allow <Tab> to invoke your completion plugin.

If you use <Tab>, you should also change 'wildchar' so you can still access the incredibly useful wildmenu. See :help wildmenu


If you want completion but your current system doesn't support cmdline-window (or you don't don't want to use it), you can use vim's has built-in vim completion: Ctrlx Ctrlv for vim completion and Ctrln for ins-completion. You can differentiate / from : cmdline with getcmdtype():

cnoremap <expr> <Tab> getcmdtype() == ':' ? '<C-f>A<C-x><C-v>' : '<C-f>A<C-n>'
3
  • Upvoted, thanks. See also: reddit.com/r/vim/comments/euaocz/… I'm now fine with cnoremap <Tab> <C-F>A<C-N> Commented Jan 30, 2020 at 8:29
  • 1
    @GiorgioRobino: If that works for you, then you might want to try my vim completion/ins-completion edit for better completion on vim commands.
    – idbrii
    Commented Jan 31, 2020 at 21:58
  • I'm happy with simple cmap <Tab> <C-n> :) Commented Nov 29, 2021 at 12:39
2

I do not provide an autocompletion solution, but a way of filling your history with terms and then cycling through the terms:

Previous commands can be retrieved with the :his[tory] command

Previous searches can be retrieved with the :his[tory] / command

If you have incsearch enabled, you can search for (e.g.) /valid which will take you to the first match (perhaps validate_firstname). Pressing CTRL-L will add one character from after the current match to the command line. This is not the same as autocomplete with TAB but it does complete the search term.

You can then use :his / to find and repeat previous searches.

Once you have /validate_firstname and /validate in the search history, you can type /val followed by the <Up> and <Down> cursor keys to cycle through the search history.

1

I answer myself to my old question, sharing a solution partially satisfying to myself (just to reciprocate people answers):

  1. searches autocompletion

    I use plugin:

    Plug 'vim-scripts/CmdlineComplete'

    quoting from: https://github.com/vim-scripts/CmdlineComplete :

When editing the command-line ( : / etc.),

press Ctrl-P or Ctrl-N to complete the word before the cursor, using keywords in the current file.

E.g: you want to search for elephant in the buffer, just type /ele and press Ctrl-P. So long as elephant is in the buffer, you will get ele completed into elephant.


  1. Command line commands history (not a real autocompletion)

    I just use Up-Arrow and Down-Arrow to retrieve recent commands history

1

This plugin serves the same purpose (though old one and no development): https://github.com/vim-scripts/sherlock.vim

Plugin-less way is invoking "q:" key map for opening command line window and can leverage all the completion shortcuts (/) just like any other normal window. This window is pre-populated with command line history

And the shortcut to enter search window "q/". This window is populated with search history

1
  • Thanks Naga, Sherlock plugin is nice but a bit cumbersome because it requires three steps: 1: <TAB> 2: select an item from the popup menu 3: <ENTER> Commented Nov 29, 2021 at 12:27
0

Here is a plugin-less solution that I personally use and think works very well:

set wildcharm=<C-z>
cnoremap <expr> <Tab>   getcmdtype() =~ '[\/?]' ? "<C-f>A<C-n>" : "<C-z>"

It makes <Tab> trigger Ctrl-f, enter INSERT mode, and starts vim's builtin completion if in search mode. Otherwise, it triggers completion like usual.

This does not prevent Ctrl-z from working in normal mode.

wildcharm is a bit weird but it allows wildmenu tab completion in mappings/macros : https://vimhelp.org/options.txt.html#%27wildcharm%27

1
  • 2
    wildcharm is specifically for use in mappings/macros.
    – D. Ben Knoble
    Commented Apr 13, 2021 at 20:54

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.