把你自己写的觉得不错的 vim 脚本贴出来

2017-08-19 07:49:20 +08:00
 tracyone
2076 次点击
所在节点    Vim
10 条回复
xiaohanqing
2017-08-25 10:30:46 +08:00
"vim8.0
"异步 project 搜索
function! AsyncGrep(keyword, ...)
let file = exists('a:1') ? a:1 : '*.*'

if a:keyword!~'\k' "暂时只支持关键字搜索
return 'incorrect a:keyword'
endif
if file!~'\f'
return 'incorrect file'
endif

if len(getqflist())>0
cgetexpr ""
let w:quickfix_title="searching..."
redraw
endif

let job_opt = {}
let job_opt.out_io = 'buffer'
let job_opt.out_mode = 'nl'
let job_opt.out_name = ''
let job_opt.out_msg = ''
let job_opt.out_cb = ''
let job_opt.err_cb = {job, message -> execute("echom ".string(message), "")}
let job_opt.exit_cb = function({kw, fl, job, status -> execute("
\ if exists('g:grepJobtimer')|
\ call timer_stop(g:grepJobtimer)|
\ endif|
\ let buf_line = getbufline(ch_getbufnr(job, 'out'),0, '$')|
\ let buf_line = filter(buf_line, 'len(v:val)>0')|
\ if len(buf_line)<1|
\ echo 'can not find ".kw." in ".fl."'|
\ return|
\ endif|
\ cgetexpr buf_line|
\ copen|
\ let w:quickfix_title='search ".kw."'|
\ echo 'search finish'|
\ ", "")}, [a:keyword, file])

let job_opt.close_cb = ""

if exists('g:grepJob') && type(g:grepJob)==v:t_job && job_status(g:grepJob)=='run' "防止多任务冲突
call job_stop(g:grepJob)
endif
let job_command = [&shell, &shellcmdflag]
"TODO 兼容更多的搜索工具
let re_option=''
if &grepprg=~'grep'
let re_option = '-r'
elseif &grepprg=~'findstr'
let re_option = '/S'
endif
let job_command += [substitute(&grepprg, '\$\*', '"'.a:keyword.'"', 'g').' '.re_option.' '.file]
let g:grepJob = job_start(job_command, extend(g:defaultJobOpt, job_opt))

if job_status(g:grepJob)=='fail'
return 'search fail'
endif

if exists('g:grepJobtimer')
call timer_stop(g:grepJobtimer)
endif
let g:grepJobtimer = ProgressBar('searching', '.', 3)
return 'search start'
endfunction

"进度条
function! ProgressBar(leadingMsg, marker, num, ...)
let interval = exists('a:1') ? a:1 : 700
return timer_start(interval, function({leadingMsg, maker, num, timer -> execute("
\ if mode(1)==#'R' || mode(1)=='n'|
\ let timer_info=timer_info(timer)|
\ echo leadingMsg.repeat(maker, ((9999*num-timer_info[0].repeat)%num)+1)|
\ endif|
\ ", "")}, [a:leadingMsg, a:marker, a:num]), {'repeat': 9999*a:num})
endfunction!
tracyone
2017-08-25 11:58:15 +08:00
@xiaohanqing 进度条很有意思。
xiaohanqing
2017-08-26 21:19:08 +08:00
@tracyone 贴一些有趣脚本出来给我玩,:D
tracyone
2017-08-26 21:32:42 +08:00
"tmux function

function! s:run_tmux(args) abort
let cmd = 'tmux ' . ' ' . a:args
return system(cmd)
endfunction

function! s:reg2tmux(reg) abort
let args = 'set-buffer "' . escape(a:reg, '"$\\') . '"'
silent call s:run_tmux(args)
endfunction

function! s:tmux2reg() abort
let args = "show-buffer"
let @" = s:run_tmux(args)
endfunction

function! te#tmux#reg2tmux() abort
:call <SID>reg2tmux(@")
endfunction

function! te#tmux#tmux2reg() abort
:call <SID>tmux2reg()
endfunction


vnoremap <C-C> y:call te#tmux#reg2tmux()<cr>
inoremap <c-v> <C-o>:call te#tmux#tmux2reg()<cr><C-o>p


最近写的,在 tmux 的 buffer 和 vim 的 uname 寄存器之间拷贝文本,对于字符界面的 vim 来说非常有用。
yehuohan
2017-09-22 10:11:24 +08:00
" 多目录、文件补全(输入一个文件 /目录后,按空格,可以接着补全第二个文件 /目录)
" FUNCTION: GetMultiFilesCompletion(arglead, cmdline, cursorpos) {{{
function! GetMultiFilesCompletion(arglead, cmdline, cursorpos)
let l:complete = []
let l:arglead_list = [""]
let l:arglead_first = ""
let l:arglead_glob = ""
let l:files_list = []

" process glob path-string
if !empty(a:arglead)
let l:arglead_list = split(a:arglead, " ")
let l:arglead_first = join(l:arglead_list[0:-2], " ")
let l:arglead_glob = l:arglead_list[-1]
endif

" glob hidden and not hidden files
let l:files_list = split(glob(l:arglead_glob . "*") . "\n" . glob(l:arglead_glob . "\.[^.]*"), "\n")
if len(l:arglead_list) == 1
let l:complete = l:files_list
else
for item in l:files_list
call add(l:complete, l:arglead_first . " " . item)
endfor
endif
return l:complete
endfunction
" }}}
tracyone
2017-09-22 21:55:13 +08:00
@yehuohan 给个使用例子呗?
yehuohan
2017-09-23 12:29:25 +08:00
@tracyone

:<Func> Des<tab>top Dow<tab>loads

<Func> 使用 GetMultiFilesCompletion 作为自动补全
<tab> 按 Tab 的地方,补全完 Desktop 后,Downloads 能继续补全
yehuohan
2017-09-23 12:31:39 +08:00
@tracyone
我用于 使用 vimgrep 全局搜索时,输入多个特定文件
yehuohan
2017-09-28 14:00:12 +08:00
还有一个,用一个 tab 实现 ZoomWin 功能,不过是使用 tab,所以界面就没 ZoomWin 插件的那样好看了,要是装了 ariline 或不显示 tabline 还好。还有就是因为 ToggleWindowZoom 使用 tab 模拟 ZoomWin,所以退出时,也要用 ToggleWindowZoom 来退出,不然会关错 tab。
let s:is_max = 0
function! ToggleWindowZoom()
if s:is_max
let s:is_max = 0
execute "normal! " . s:last_tab . "gt"
execute "noautocmd " . s:last_winnr . "wincmd w"
silent! execute "tabclose " . s:this_tab
else
let s:is_max = 1
let s:last_winnr = winnr()
let s:last_tab = tabpagenr()
execute "tabedit " . expand("%")
let s:this_tab = tabpagenr()
endif
endfunction
qiqiboy
2017-10-19 10:23:20 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/384095

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX