Skip to content

girishji/autosuggest.vim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

autosuggest.vim

Autocompletion for Vim's Cmdline Mode (`/`, `?`, and `:` commands).

Key FeaturesUsageRequirementsInstallationConfigurationCommands

demo

Key Features

  • Preview candidates for search pattern autocompletion and command autocompletion.
  • Switch between normal popup menu and flat menu.
  • Autocomplete multiple words during search.
  • Fuzzy search.
  • All Vim idioms work as expected (ex. 'c', 'y', 'd' with pattern).
  • Does not hang or slow down when searching large files or expanding wildcards.
  • Search-highlighting, incremental-search, and history recall work as expected.
  • Written in Vim9script.

Usage

Vim's default keybindings are not altered in any way.

  • / or ? to search forward or backward.
  • : to enter commands.
  • <Tab> and <Shift-tab> (or <Ctrl-N> and <Ctrl-P>) to select menu items.
  • <Ctrl-E> to dismiss popup menu.
  • <Enter> to accept selection.
  • <Esc> to dismiss search.
  • <Ctrl-C> to force close popup menu.

Note

For multi-word search, type the separator character (like <Space>) after the first word to trigger autocompletion for second word. Type \n at the end of the last word in a line to continue to next line. Setting fuzzy search option disables multi-word search.

Note

For insert-mode autocompletion see Vimcomplete.

Requirements

  • Vim >= 9.0

Installation

Install it via vim-plug.

Show instructions
Using vim9 script:
vim9script
plug#begin()
Plug 'girishji/autosuggest.vim'
plug#end()

Using legacy script:

call plug#begin()
Plug 'girishji/autosuggest.vim'
call plug#end()

Install using Vim's built-in package manager.

Show instructions
$ mkdir -p $HOME/.vim/pack/downloads/opt
$ cd $HOME/.vim/pack/downloads/opt
$ git clone https://github.com/girishji/autosuggest.vim

Add the following line to your $HOME/.vimrc file.

packadd autosuggest.vim

Configuration

Default options are as follows:

vim9script
var options = {
    search: {
        enable: true,   # 'false' will disable search completion
        pum: true,      # 'false' for flat menu, 'true' for stacked menu
        maxheight: 12,  # max height of stacked menu in lines
        fuzzy: false,   # fuzzy completion
        alwayson: true, # when 'false' press <tab> to open popup menu
    },
    cmd: {
        enable: true,   # 'false' will disable command completion
        pum: true,      # 'false' for flat menu, 'true' for stacked menu
        fuzzy: false,   # fuzzy completion
        exclude: [],    # patterns to exclude from command completion (use \c for ignorecase)
        onspace: [],    # show popup menu when cursor is in front of space (ex. :buffer<space>)
    }
}

Note

The exclude option mentioned above uses regex pattern match rather than string comparison. For instance, if you would like to exclude :e from autosuggestion but would like to see menu when you type :e<space>, then use the following:

exclude: ['^e$'],  # exclude ':e' but not ':e<sapce>'
onspace: ['e'],    # show menu when 'e:<space>' is typed

Options can be modified using g:AutoSuggestSetup(). If you are using vim-plug use the VimEnter event as follows.

autocmd VimEnter * g:AutoSuggestSetup(options)

It is also possible to verify if the options are correctly set (debugging purposes only):

echo g:AutoSuggestGetOptions()

Highlight Groups

The AS_SearchCompletePrefix highlight group influences the fragment of a menu item that matches the text being searched. The appearance of the popup menu is determined by Vim's highlight groups Pmenu, PmenuSel, PmenuSbar, and PmenuThumb. For command completion, the WildMenu group (refer to :h hl-WildMenu) can be utilized.

Case Sensitive Search

Set ignorecase and smartcase using set command. See :h 'ignorecase' and h 'smartcase'.

Key Mapping

If you defined a keymap that puts text on the command line or waits for input, you may find that the command line may get cleared by the popup. This undesirable outcome can be prevented by one of two methods: specify keywords that should be ignored by the autocompletion mechanism, or disable and enable the plugin within the keymap.

For instance, let's say you have a keymap as follows. First command lists buffers and second one chooses.

nnoremap <leader>b :buffers<cr>:buffer<space>

This will not work because the second buffer command is just text on the command line. It causes the popup to open and clear the output of previous buffers command.

First solution is to simply exclude the word buffer from autocompletion. Include this in your options.

var options = {
    cmd: {
        exclude: ['^buffer']
    }
}

Another solution is to disable and enable.

:nnoremap <leader>b :AutoSuggestDisable<cr>:buffers<cr>:let nr = input("Which one: ")<Bar>exe $'buffer {nr}'<bar>AutoSuggestEnable<cr>

Find Files and Switch Buffers Quickly

You can define some interesting keymappings with the help of this plugin. Here are two examples. First one will help you open file under current working directory. Second mapping switches buffers. Type a few letters to narrow the search and use <tab> to choose from menu.

nnoremap <leader>f :e<space>**/*<left>
nnoremap <leader>b :buffer<space>
autocmd VimEnter * g:AutoSuggestSetup({ cmd: { onspace: ['buffer'] }})

Performance

Care is taken to ensure that responsiveness does not deteriorate when searching large files or expanding wildcards. Large files are searched in batches. Between each search attempt input keystrokes are allowed to be queued into Vim's main loop. Wildcard expansions are first executed in a separate job and aborted after a timeout.

Commands

Enable or disable this plugin:

  • :AutoSuggestEnable
  • :AutoSuggestDisable

Other Plugins to Enhance Your Workflow

  1. devdocs.vim - browse documentation from devdocs.io.

  2. easyjump.vim - makes code navigation a breeze.

  3. fFtT.vim - accurately target words in a line.

  4. scope.vim - fuzzy find anything.

  5. vimcomplete - enhances autocompletion in Vim.

Similar Plugins

Contributing

Pull requests are welcome.