forked from PegasusWang/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunixhelp.vim
78 lines (67 loc) · 1.85 KB
/
unixhelp.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
" Unix Help
" ---
" Open man pages for gitconfig, tmux, and sh files
"
" Behaviors:
" - If one of the supported filetypes is loaded, map K to open help window
"
" Options:
" - g:unixhelp_open_with_tmux enable to use tmux splits for help windows
if exists('g:loaded_unixhelp')
finish
endif
let g:loaded_unixhelp = 1
let g:unixhelp_open_with_tmux = get(g:, 'unixhelp_open_with_tmux', 0)
augroup plugin_unixhelp
autocmd!
autocmd FileType gitconfig,tmux nnoremap <silent><buffer> K
\ :<C-u>call <SID>open_man(&filetype, expand('<cword>'))<CR>
augroup END
function! s:open_man(ft, search_word)
let l:mapping = {
\ 'gitconfig': 'git-config',
\ }
let l:ft = a:ft
if has_key(l:mapping, l:ft)
let l:ft = l:mapping[l:ft]
endif
if empty(l:ft)
echoerr 'Sorry, no help supported for "' . l:ft . '" filetypes.'
return
endif
let l:cmd = 'man ' . l:ft
if g:unixhelp_open_with_tmux && ! empty('$TMUX')
call s:man_tmux(l:cmd, a:search_word)
else
call s:man_preview(l:cmd, a:search_word)
endif
endfunction
" Open file-explorer split with tmux
function! s:man_tmux(str, word)
if empty('$TMUX')
return
endif
let l:cmd = 'MANPAGER="less --pattern='.shellescape(a:word, 1).'" '.a:str
silent execute '!tmux split-window -p 30 '.shellescape(l:cmd, 1)
endfunction
function! s:man_preview(str, word)
silent! wincmd P
if ! &previewwindow
noautocmd execute 'bo' &previewheight 'new'
set previewwindow
silent! wincmd P
else
execute 'resize' &previewheight
endif
setlocal buftype=nofile bufhidden=delete noswapfile
setlocal noreadonly modifiable
execute '%delete_'
silent execute '%!MANPAGER=cat '.a:str
setlocal readonly filetype=man
nnoremap <silent><buffer> q :<C-u>set nopvw<CR>:<C-u>bdelete!<CR>
" normal! gg
" silent! execute 'normal! /'.a:word."\<CR>"
" let @/ = a:word
" noautocmd wincmd p
call feedkeys('/'.a:word."\<CR>", 'nt')
endfunction