DEV Community

Cover image for Have you ever seen vim's popup menu?
Simone Gentili
Simone Gentili

Posted on

Have you ever seen vim's popup menu?

popup menu, ...

Is a Vim function that displays a menu in a popup window. Yes, .. Vim has popup windows. The menu can contain a list of items. In the following example I've added just two: open vimrc file and replace current word. I this example, ... You'll see the code to select an item by clicking on it.

The popup_menu() function takes a list of items as an argument, and it returns the index of the selected item. Somewere inthe documentation I sow index starts from 0, .. but not for me: the first item in the list is 1, the index of the second item is 2, and so on.

The popup_menu() function also supports various options that allow you to customize the appearance and behavior of the popup menu. For example, you can specify the title of the menu, the callback function that is called when an item is selected, and the highlight style of the menu.

In this example the menu is "What do you want?". I've also added a this to open the popup menu:

nnoremap <leader><leader> :call Funzione()<CR>
Enter fullscreen mode Exit fullscreen mode

So, ... it si very very simple to open popup. Oh, .. And you can simply ESC to close it without a selection.

" menu stuff
function! SensorarioTools(id, result)
    if a:result == 1
        :e ~/.vimrc
    if a:result == 2
        set nopaste
        let user_input = input('Enter a word:')
        let current_word = expand('<cword>')
        execute '%s/'.current_word.'/'.user_input.'/g'
func! Funzione()
  call popup_menu([
    \ 'open ~/.vimrc',
    \ 'replace current word',
    \ ], #{
    \ title: "What do you want?",
    \ callback: 'SensorarioTools',
    \ line: 25,
    \ col: 60,
    \ highlight: 'Question',
    \ border: [],
    \ close: 'click',
    \ padding: [1,20,1,1],
    \ })

nnoremap <leader><leader> :call Funzione()<CR>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)