2014-02-11 72 views
3

有没有办法通过命令在两个.vimrc设置之间切换?使用命令在两个.virmc设置之间切换?

说我有我的vimrc:

* Settings 1 
    setlocal formatoptions=1 
    setlocal noexpandtab 
    map j gj 
    map k gk 

    * Settings 2 
    setlocal formatoptions=2 
    map h gj 
    map l gk 

而且我希望能够设置1和2之间变化,说通过键入:S1:S2

这样做的原因是,我想有我使用的设置,而编码和另一组,而写作。

完成此操作的最佳方法是什么?

回答

6

您可以使用:h :command创建:S1:S2命令。将这些命令键入功能并确保设置互相取消。例如...

command! S1 call Settings1() 
command! S2 call Settings2() 

fun! Settings1() 
    setlocal formatoptions=1 
    setlocal noexpandtab 
    silent! unmap <buffer> h 
    silent! unmap <buffer> l 
    nnoremap j gj 
    nnoremap k gk 
endfun 

fun! Settings2() 
    setlocal formatoptions=2 
    setlocal expandtab 
    silent! unmap <buffer> j 
    silent! unmap <buffer> k 
    nnoremap h gj 
    nnoremap l gk 
endfun 

如果你不想进行设置抵消,最简单的解决方案可能是重新启动VIM有不同的配置文件。您还可以使用set option!切换选项,并使用mapclear命令清除映射。但是,您必须针对无法切换的选项(如formatoptions)进行特定设置。您可以使用set option&将它们重置为默认值。

但是,您可以将所有选项重置为默认值:set all&。例如,使用此功能,您可以拨打Settings1()致电:set all&source $MYVIMRC。然后Settings2()也可以调用它们,然后设置各种选项。例如...

" tons of settings 

command! S1 call Settings1() 
command! S2 call Settings2() 

fun! Settings1() 
    set all& 
    mapclear 
    source $MYVIMRC 
endfun 

fun! Settings2() 
    set all& 
    mapclear 
    setlocal formatoptions=2 
    setlocal expandtab 
    nnoremap h gj 
    nnoremap l gk 
endfun 
+0

谢谢!有没有办法取消功能而不是个人设置?因为我的编码设置太多了,我不得不寻找每一个,并找出如何逐个取消它。 – alexchenco

+0

我不知道没有重新启动vim的简单方法,但我更新了答案以反映这一点。 – Conner

+0

非常感谢!我会试试这个。我想知道colorcheme会发生什么,我认为它不会改变? – alexchenco