V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tracyone
V2EX  ›  Vim

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

  •  
  •   tracyone · 2017-08-19 07:49:20 +08:00 via iPhone · 2230 次点击
    这是一个创建于 2637 天前的主题,其中的信息可能已经有所发展或是发生改变。
    10 条回复    2017-10-19 10:23:20 +08:00
    xiaohanqing
        1
    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
        2
    tracyone  
    OP
       2017-08-25 11:58:15 +08:00
    @xiaohanqing 进度条很有意思。
    xiaohanqing
        3
    xiaohanqing  
       2017-08-26 21:19:08 +08:00 via Android
    @tracyone 贴一些有趣脚本出来给我玩,:D
    tracyone
        4
    tracyone  
    OP
       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
        5
    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
        6
    tracyone  
    OP
       2017-09-22 21:55:13 +08:00
    @yehuohan 给个使用例子呗?
    yehuohan
        7
    yehuohan  
       2017-09-23 12:29:25 +08:00
    @tracyone

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

    <Func> 使用 GetMultiFilesCompletion 作为自动补全
    <tab> 按 Tab 的地方,补全完 Desktop 后,Downloads 能继续补全
    yehuohan
        8
    yehuohan  
       2017-09-23 12:31:39 +08:00
    @tracyone
    我用于 使用 vimgrep 全局搜索时,输入多个特定文件
    yehuohan
        9
    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
        10
    qiqiboy  
       2017-10-19 10:23:20 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1399 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 17:33 · PVG 01:33 · LAX 09:33 · JFK 12:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.