DEV Community

loading...

Vim 2021: Add A Context Menu in You Vim

skywind3000 profile image Linwei ・2 min read

A nice looking context menu is very useful when you have something to do with current word/line under cursor. And it can also remind you when you forget your keymaps:

Alt Text

Setup

With the help of the ui-extension plugin quickui, it can be simply defined like this:

Plug 'skywind3000/vim-quickui'

" define your context menu as a list of (text, command) pairs
let g:context_menu_k = [
            \ ["&Help Keyword\t\\ch", 'echo expand("<cword>")' ],
            \ ["&Signature\t\\cs", 'echo 101'],
            \ ['-'],
            \ ["Find in &File\t\\cx", 'exec "/" . expand("<cword>")' ],
            \ ["Find in &Project\t\\cp", 'exec "vimgrep " . expand("<cword>") . "*"' ],
            \ ["Find in &Defintion\t\\cd", 'YcmCompleter GotoDefinition' ],
            \ ["Search &References\t\\cr", 'YcmCompleter GoToReferences'],
            \ ['-'],
            \ ["&Documentation\t\\cm", 'exec "PyDoc " . expand("<cword>")'],
            \ ]

" map 'K' to display the context menu
nnoremap <silent>K :call quickui#tools#clever_context('k', g:context_menu_k, {})<cr>
Enter fullscreen mode Exit fullscreen mode

Then when you press K, it will display around the cursor:

Context menu is a good place to organize your LSP commands.

Usage

  • navigate the items by j/k or arrow keys.
  • accept an item by Enter or Space or mouse left-click.
  • press ESC to quit.
  • Hot keys can be defined by a &.

The border and color are also customizable, check the quickui doc for more.

Plugin Dedicated Context

Context menu can also be used to enhance plugin experience, you can setup some buffer local keymap in the plugin buffer:

let g:context_menu_git = [
      \ ["&Stage (add)\ts", 'exec "normal s"' ],
      \ ["&Unstage (reset)\tu", 'exec "normal u"' ],
      \ ["&Toggle stage/unstage\t-", 'exec "normal -"' ],
      \ ["Unstage &Everything\tU", 'exec "normal U"' ],
      \ ["D&iscard change\tX", 'exec "normal X"' ],
      \ ["--"],
      \ ["Inline &Diff\t=", 'exec "normal ="' ],
      \ ["Diff S&plit\tdd", 'exec "normal dd"' ],
      \ ["Diff &Horizontal\tdh", 'exec "normal dh"' ],
      \ ["Diff &Vertical\tdv", 'exec "normal dv"' ],
      \ ["--"],
      \ ["&Open File\t<CR>", 'exec "normal \<cr>"' ],
      \ ["Open in New Split\to", 'exec "normal o"' ],
      \ ["Open in New Vsplit\tgO", 'exec "normal gO"' ],
      \ ["Open in New Tab\tO", 'exec "normal O"' ],
      \ ["Open in &Preview\tp", 'exec "normal p"' ],
      \ ["--"],
      \ ["&Commit\tcc", 'exec "normal cc"' ],
      \ ]

function! s:setup_fugitive()
    nnoremap <silent><buffer>K :call quickui#tools#clever_context('g', g:context_menu_git, {})<cr>
endfunc

augroup MenuEvents
    au!
    au FileType fugitive call s:setup_fugitive()
augroup END
Enter fullscreen mode Exit fullscreen mode

When you are using fugitive, you can press K to display a fugitive menu:

Alt Text

No need to remember these seldom used fugitive keymaps, just K is totally enough.

You can also setup a context menu for defx.nvim:

Alt Text

This made defx more user-friendly.

Discussion (1)

pic
Editor guide
Collapse
khoa0319 profile image
Khoa Huỳnh

Great, this is what my vim is missing.