Skip to content

Instantly share code, notes, and snippets.

@wbthomason
Last active August 19, 2024 19:57
Show Gist options
  • Save wbthomason/5e249439b5fc5738cb4b44419e302f68 to your computer and use it in GitHub Desktop.
Save wbthomason/5e249439b5fc5738cb4b44419e302f68 to your computer and use it in GitHub Desktop.
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>)
@adigitoleo
Copy link

adigitoleo commented Aug 19, 2024

There are now a couple of plugins for this sort of thing. One of them is mine (shameless plug) and also does floating terminals, the other one offers more configuration of the window position/size etc.

However, these are both NeoVim only (Lua) so won't help for pure vim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment