-
-
Save andrewpedia/7a37e6e4e9e137e96b15aaaca0f23ed6 to your computer and use it in GitHub Desktop.
Neovim: Open help in a floating window
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scriptencoding utf-8 | |
" This function originates from https://www.reddit.com/r/neovim/comments/eq1xpt/how_open_help_in_floating_windows/; it isn't mine | |
function! CreateCenteredFloatingWindow() abort | |
let width = min([&columns - 4, max([80, &columns - 20])]) | |
let height = min([&lines - 4, max([20, &lines - 10])]) | |
let top = ((&lines - height) / 2) - 1 | |
let left = (&columns - width) / 2 | |
let opts = {'relative': 'editor', 'row': top, 'col': left, 'width': width, 'height': height, 'style': 'minimal'} | |
let top = "╭" . repeat("─", width - 2) . "╮" | |
let mid = "│" . repeat(" ", width - 2) . "│" | |
let bot = "╰" . repeat("─", width - 2) . "╯" | |
let lines = [top] + repeat([mid], height - 2) + [bot] | |
let s:buf = nvim_create_buf(v:false, v:true) | |
call nvim_buf_set_lines(s:buf, 0, -1, v:true, lines) | |
call nvim_open_win(s:buf, v:true, opts) | |
set winhl=Normal:Floating | |
let opts.row += 1 | |
let opts.height -= 2 | |
let opts.col += 2 | |
let opts.width -= 4 | |
let l:textbuf = nvim_create_buf(v:false, v:true) | |
call nvim_open_win(l:textbuf, v:true, opts) | |
au BufWipeout <buffer> exe 'bw '.s:buf | |
return l:textbuf | |
endfunction | |
function! FloatingWindowHelp(query) abort | |
let l:buf = CreateCenteredFloatingWindow() | |
call nvim_set_current_buf(l:buf) | |
setlocal filetype=help | |
setlocal buftype=help | |
execute 'help ' . a:query | |
endfunction | |
command! -complete=help -nargs=? Help call FloatingWindowHelp(<q-args>) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment