Auto-Reload Your Vimrc

本文转载至:http://www.bestofvim.com/tip/auto-reload-your-vimrc/

If you're going to master Vim, you're going to spend some time tweaking & tailoring your.vimrc file. And when you're doing that you'll want to see the effects of any changes as quickly as possible.

It's easy to get Vim to reload the .vimrc file whenever it changes. Just add this to your.vimrc file:

augroup reload_vimrc " {
    autocmd!
    autocmd BufWritePost $MYVIMRC source $MYVIMRC
augroup END " }

Save, then try it out. Put set number at the bottom of your .vimrc & save. Then change it to set nonumber and save again. Line numbering should pop in an out. Instant reloading.

Breakdown

Let's take a look at what's happening. The bulk of the work's done by the line:

autocmd BufWritePost $MYVIMRC source $MYVIMRC

$MYVIMRC is the platform-independent location of your .vimrc file. So this command says that whenever that file is written, source (reload) it immediately afterwards.

The rest of the code, the wrapping, is a pattern you'll see a lot in .vimrc files:

augroup somename " {
    autocmd!
    ...
augroup END " }

This is housekeeping. Putting the commands in a group, and starting with autocmd! clears out any previous autocommands Vim has registered for that group. If we didn't do this, Vim would append a new autocommand every time we wrote the .vimrc file. Pretty quickly we'd have dozens of source $MYVIMRC commands queued up for every single write.

See :help $MYVIMRC:help source and especially :help autocmd for more.

Plus: Steve Losh's excellent Learn Vimscript The Hard Way has a great explanation of how autocommand groups work.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值