skywind3000 / vim-quickui

The missing UI extensions for Vim 9 (and NeoVim) !! :sunglasses:
https://www.vim.org/scripts/script.php?script_id=5845
MIT License
1.1k stars 35 forks source link

[tips & tricks] #62

Open leo-fp opened 2 years ago

leo-fp commented 2 years ago

This page is used to collect tips and tricks that can inspire novice or advanced users.

If you think this plugin is useful or fun, just show us how you use it. The idea comes from here.

note: The user manual is highly recommended to read.

Rules

leo-fp commented 2 years ago

The Menu and Context menu are the two widgets i use most.

I usually can't remember how to use some infrequently used plug-ins, so as long as these plug-ins are allowed to be used by functions or commands, I will put them in these two widgets.

The true trick i think is adding an item for some functions that needs selected text.

For example, if you have openbrowser installed, select a text, then press \q, and execute the item, your browser will be opened and search the selected text automatically.

usage: put this in your vimrc

note: if your leader key has not been modified, it should be "\"

" from https://stackoverflow.com/questions/1533565/how-to-get-visually-selected-text-in-vimscript/6271254#6271254
function! Get_visual_selection()
    " Why is this not a built-in Vim script function?!
    let [line_start, column_start] = getpos("'<")[1:2]
    let [line_end, column_end] = getpos("'>")[1:2]
    let lines = getline(line_start, line_end)
    if len(lines) == 0
        return ''
    endif
    let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)]
    let lines[0] = lines[0][column_start - 1:]
    return join(lines, "\n")
endfunction
function! Demo()
    let l:content = [
                \ ["search", "call openbrowser#search(Get_visual_selection(), \"baidu\")"],
                \ ]
    " set cursor to the last position
    let opts = {'index':g:quickui#context#cursor}
    call quickui#context#open(content, opts)
endfunction
vnoremap <leader>q :<c-u>call Demo()<CR>