Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Linwei
Linwei

Posted on

     

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 pluginquickui, it can be simply defined like this:

Plug'skywind3000/vim-quickui'" define your context menu as a list of (text, command) pairsletg: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 menunnoremap<silent>K:call quickui#tools#clever_context('k',g:context_menu_k,{})<cr>
Enter fullscreen modeExit fullscreen mode

Then when you pressK, it will display around the cursor:

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

Usage

  • navigate the items byj/k or arrow keys.
  • accept an item byEnter orSpace or mouse left-click.
  • pressESC to quit.
  • Hot keys can be defined by a&.

The border and color are also customizable, check the quickuidoc 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:

letg: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>endfuncaugroup MenuEventsau!auFileType fugitivecalls:setup_fugitive()augroup END
Enter fullscreen modeExit fullscreen mode

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

Alt Text

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

You can also setup a context menu fordefx.nvim:

Alt Text

This made defx more user-friendly.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
khoahuynhdev profile image
Khoa Huỳnh
Software engineer
  • Location
    Ho Chi Minh City
  • Work
    always a student
  • Joined

Great, this is what my vim is missing.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Coding since 1992.
  • Location
    Shenzhen, PRC
  • Joined

More fromLinwei

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp