vim: add Man and Cmd functions

- `:Man <cmd>` opens the man of `cmd` in a new buffer
- `:Cmd <cmd>` executes `cmd` and shows the output in a new scratch
  buffer
feature/symbol-search
bretello 2021-01-09 23:44:48 +01:00
parent 229a97856e
commit 9e2e7e1ee0
Signed by: brethil
GPG Key ID: 876AAC6290170FE7
1 changed files with 58 additions and 0 deletions

View File

@ -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(<q-args>, <range>, <line1>, <line2>)
" 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(<q-args>)
" 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(<q-args>)