DEV Community

Linwei
Linwei

Posted on

7 1

Vim 2021: Add A Context Menu in You Vim

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.

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (1)

Collapse
 
khoahuynhdev profile image
Khoa Huỳnh

Great, this is what my vim is missing.

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay