Skip to content

Instantly share code, notes, and snippets.

@wbthomason
Last active May 25, 2021
Embed
What would you like to do?
Neovim: Open help in a floating window
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