From 9e2e7e1ee0ea0e914808a3804f0a000778dd38d1 Mon Sep 17 00:00:00 2001 From: bretello Date: Sat, 9 Jan 2021 23:44:48 +0100 Subject: [PATCH] vim: add Man and Cmd functions - `:Man ` opens the man of `cmd` in a new buffer - `:Cmd ` executes `cmd` and shows the output in a new scratch buffer --- vim/functions.vim | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/vim/functions.vim b/vim/functions.vim index 19844d0..4147daf 100644 --- a/vim/functions.vim +++ b/vim/functions.vim @@ -90,3 +90,61 @@ function! GitStatus() let [a,m,r] = GitGutterGetHunkSummary() return printf('+%d ~%d -%d', a, m, r) endfunction + + +" Redirect command into scractch buffer. Stolen from romainl +function! Redir(cmd, rng, start, end) + for win in range(1, winnr('$')) + if getwinvar(win, 'scratch') + execute win . 'windo close' + endif + endfor + if a:cmd =~ '^!' + let cmd = a:cmd =~' %' + \ ? matchstr(substitute(a:cmd, ' %', ' ' . expand('%:p'), ''), '^!\zs.*') + \ : matchstr(a:cmd, '^!\zs.*') + if a:rng == 0 + let output = systemlist(cmd) + else + let joined_lines = join(getline(a:start, a:end), '\n') + let cleaned_lines = substitute(shellescape(joined_lines), "'\\\\''", "\\\\'", 'g') + let output = systemlist(cmd . " <<< $" . cleaned_lines) + endif + else + redir => output + execute a:cmd + redir END + let output = split(output, "\n") + endif + vnew + let w:scratch = 1 + setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile + call setline(1, output) +endfunction + +command! -nargs=1 -complete=command -bar -range Redir silent call Redir(, , , ) + +" Gets the manul page for the given command +function! Man(cmd) + let output = system('env PAGER="bat -p" man ' . a:cmd) + vnew + let w:scratch=1 + setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile + setlocal nonumber norelativenumber + call setline(1, split(output, '\n')) +endfunction + +command! -nargs=1 -complete=command Man silent call Man() + +" Execute the given command and print it in a scratch buffer +function! Cmd(cmd) + let output = system(a:cmd) + vnew + let w:scratch=1 + " setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile + setlocal buftype=popup bufhidden=wipe nobuflisted noswapfile + setlocal nonumber norelativenumber + call setline(1, split(output, '\n')) +endfunction + +command! -nargs=1 -complete=command Cmd silent call Cmd()