首頁  >  問答  >  主體

vimrc - vim如何根據檔案的後綴名來進行按鍵的對應?

例如在編輯.cpp檔案時,想把<F5>對應為:call CompileCpp()<CR>.
在編譯.html檔時,想把<F5>映射為:call RunHtml()<CR><Spcae>.
請問可以做到嗎?

某草草某草草2712 天前586

全部回覆(1)我來回復

  • 世界只因有你

    世界只因有你2017-05-16 16:38:29

    大概使用這種

    autocmd FileType vim  call RunHtml()

    但是建議 autocmd FileType 時設定makeprg,然後F5的時候直接make就行了。這樣可以顯示錯誤訊息到quickfix
    這是我設定的其它語言的

    
    augroup make_autocmd
        autocmd Filetype javascript setlocal makeprg=jsl\ -nologo\ -nofilelisting\ -nosummary\ -nocontext\ -conf\ /etc/jsl.conf\ -process\ %
        autocmd FileType json setlocal makeprg=
        autocmd FileType php
                    \ setlocal makeprg=php\ -l\ -n\ -d\ html_errors=off\ % |
                    \ setlocal errorformat=%m\ in\ %f\ on\ line\ %l
        autocmd BufWritePost * call Make()
        " auto close quickfix if it is the last window
        autocmd WinEnter * if winnr('$') == 1 && getbufvar(winbufnr(winnr()), "&buftype") == "quickfix" | quit | endif
    augroup END
    
    function! Make()
        if &modified | silent write | endif
        if &makeprg == 'make' | return | endif
        let regname = '"~'
        let old_pos = getpos('.')
        silent make
        execute 'cw'
        if !has('gui_running') | redraw! | end
        call setpos('.', old_pos)
    endfunction
    

    回覆
    0
  • 取消回覆