Created
July 9, 2020 22:23
-
-
Save imvaskii/51db3e397eee1ea01a938798470fdb0e to your computer and use it in GitHub Desktop.
Vim 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
" floating fzf window with borders | |
function! CreateCenteredFloatingWindow() | |
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 | |
call nvim_open_win(nvim_create_buf(v:false, v:true), v:true, opts) | |
au BufWipeout <buffer> exe 'bw '.s:buf | |
endfunction | |
" Files + devicons + floating fzf | |
function! Fzf_files() | |
function! s:files() | |
let l:files = split(system($FZF_DEFAULT_COMMAND), '\n') | |
return l:files | |
" return s:prepend_icon(l:files) | |
endfunction | |
function! s:prepend_icon(candidates) | |
let l:result = [] | |
for l:candidate in a:candidates | |
let l:filename = fnamemodify(l:candidate, ':p:t') | |
let l:icon = WebDevIconsGetFileTypeSymbol(l:filename, isdirectory(l:filename)) | |
call add(l:result, printf('%s %s', l:icon, l:candidate)) | |
endfor | |
return l:result | |
endfunction | |
function! s:edit_file(lines) | |
if len(a:lines) < 2 | return | endif | |
" map passed expect key to command | |
let l:cmd = get({ | |
\ 'ctrl-v': 'vertical split', | |
\ 'ctrl-s': 'split', | |
\ 'ctrl-t': 'tabe', | |
\ }, a:lines[0], 'e') | |
" example arg lines: ['ctrl-s', ' composer.json'] | |
for l:item in a:lines[1:] | |
" replace icon+<space> from 2nd index | |
" let l:filepath = substitute(l:item, '\zs[^ ]* ', '', '' ) | |
let l:filepath = trim(l:item) | |
execute 'silent ' . l:cmd . ' '. l:filepath | |
endfor | |
endfunction | |
" let l:fzf_preview_option = '--preview \"bat --style=numbers,changes --color=always {+1}" ' | |
let l:fzf_files_option = '--expect=ctrl-s,ctrl-v,ctrl-s --multi --bind=ctrl-a:select-all,ctrl-d:deselect-all' | |
call fzf#run({ | |
\ 'source' : s:files(), | |
\ 'sink*': function('s:edit_file'), | |
\ 'options': '-m --reverse ' . l:fzf_files_option, | |
\ 'down': '40%', | |
\ 'window': 'call CreateCenteredFloatingWindow()'}) | |
endfunction | |
nnoremap <silent> <leader>zf :call Fzf_files()<CR> | |
" Buffers + floating fzf | |
function! Fzf_buffers() | |
function! s:buflist() | |
redir => ls | |
silent ls | |
redir END | |
let l:rawlist = split(ls, '\n') | |
let l:cbuffers = [] | |
for l:line in l:rawlist | |
" \zs : anything, sets start of match. | |
" \ze : anything, sets end of match. | |
let l:fpath = matchstr(l:line, '"\zs[^"]\+\ze"') | |
let l:lnu = matchstr(l:line, ' line \zs[0-9]*' ) | |
let l:filepath = expand(l:fpath) | |
if filereadable(l:filepath) | |
let l:cbuffers += [l:filepath . ' ' . l:lnu] | |
" else | |
" echom l:line | |
endif | |
endfor | |
return l:cbuffers | |
endfunction | |
function! s:bufopen(lines) | |
" map passed expect key to command | |
let l:cmd = get({ | |
\ 'ctrl-v': 'vertical split', | |
\ 'ctrl-s': 'split', | |
\ 'ctrl-t': 'tabe', | |
\ }, a:lines[0], 'e') | |
" arg lines: ['ctrl-s', '/home/bhaskar/.config/nvim/init.vim 753'] | |
for l:item in a:lines[1:] | |
" replace icon+<space> from 2nd index | |
let l:filepath =matchstr(l:item, '\zs[^0-9]*') | |
let l:linenum =matchstr(l:item, '\zs[0-9]*') | |
execute 'silent ' . l:cmd . ' '. l:linenum . ' '. l:filepath | |
endfor | |
endfunction | |
let l:fzf_preview_option = '--preview "bat --style=numbers,changes --color=always -r {+2}: {1}" ' | |
let l:fzf_buffer_option = '--expect=ctrl-s,ctrl-v,ctrl-s --multi --bind=ctrl-a:select-all,ctrl-d:deselect-all' | |
call fzf#run({ | |
\ 'source' : <sid>buflist(), | |
\ 'sink*' : function('s:bufopen'), | |
\ 'options': '-m --reverse '. l:fzf_preview_option . l:fzf_buffer_option, | |
\ 'down': '40%', | |
\ 'window': 'call CreateCenteredFloatingWindow()'}) | |
endfunction | |
nnoremap <silent> <Leader><Enter> :call Fzf_buffers()<CR> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment