I wrote this function to popup a simple menu with each opened buffer as entry:

function! PopupBufferList()
   let menuName = "BuffersList"
   silent! exec ":aunmenu ".menuName
   " All 'possible' buffers that may exist
   let b_all = range(1, bufnr('$'))
   " Unlisted ones
   let b_unl = filter(b_all, 'buflisted(v:val)')
   for nbuff in b_unl
      let menuitem = substitute(bufname(nbuff), '\.', '\\.', 'g')
      :exec ":amenu ]".menuName.".".menuitem." :b ".string(nbuff)."<CR>"
   :exec ":popup ]".menuName

Is there a way to know, from vimscript, when this menu is visible (i.e. pumvisible()) ? Because I would like to map <Tab> to <Down> when this menu is visible.

  • You might want to consider using the newer popup functions instead of the menu commands (which IIUC have a very different appearance in graphical Vim). With the new-style popups, you can also have custom key-handlers.
    – D. Ben Knoble
    Commented May 21, 2023 at 20:58
  • I know but I am using Vim 8.1 (and 7 on another machine where I am not the admin of it)
    – xyx
    Commented May 21, 2023 at 21:10

1 Answer 1

  1. There is no equivalent of pumvisible() for GUI menus.
  2. Opening a GUI menu doesn't trigger any event but it is probably possible to use something like :doautocommand User menuFooBar.
  3. Which would be useless in this case, because Vim mappings don't affect the GUI anyway.

Using :help popup-window, as suggested in the comments, would be a better idea for the reason mentioned but also because it works the same in the GUI and the TUI.

If you can't use that feature, I'm afraid you are left with few options:

  • use a fuzzy search plugin like fzf.vim and configure its keybindings,
  • populate the quickfix list with your buffers and make filetype-specific mappings,
  • or use :help inputlist(), which is not really interactive.

FWIW, here is a quickfix variant of your snippet:

function! MakeQFItem(key, val)
    return {'bufnr': a:val}
let b_all = range(1, bufnr('$'))
let b_unl = filter(b_all, 'buflisted(v:val)')
let b_map = map(b_unl, function('MakeQFItem'))
call setqflist(b_map)

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.