vimrc - vim如何根据文件的后缀名来进行按键的映射?

查看:580
本文介绍了vimrc - vim如何根据文件的后缀名来进行按键的映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

比如在编辑.cpp文件时,想把<F5>映射为 :call CompileCpp()<CR>.
在编译.html文件时,想把<F5>映射为:call RunHtml()<CR><Spcae>.
请问可以做到吗?

解决方案

大概使用这种

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

这篇关于vimrc - vim如何根据文件的后缀名来进行按键的映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆