2017-09-16 28 views
0

我已經創建了兩個短vim的文件,一些配置之間快速切換,並把他們安置在~/.vim如何從其他目錄運行Vimscript(.vim)文件?

如果我從~/.vim啓動Vim並做:run write.vim:run code.vim,他們的工作很好,但如果我從其他地方開始VIM(例如~/)並嘗試:run .vim/write.vim(從~/)或:run ~/.vim/write.vim(從其他地方),它將不會運行。任何想法爲什麼?

爲了完整起見,我放入了兩個.vim文件。

write.vim

colorscheme pencil 
set background=light 
set colorcolumn=0 
set wrap 
set nonumber 

code.vim

colorscheme tomorrow-night-eighties 
set background=dark 
set colorcolumn=100 
highlight ColorColumn ctermbg=darkgrey 
set nowrap 
set number 

回答

4

你應該閱讀:runtime'runtimepath'文檔 - >:h :runtime:h 'runtimepath'

你會發現你實際上想要執行:so ~/.vim/sub/path.vim:runtime sub/path.vim你的情況。另請參閱:What's the difference between ':source file' and ':runtime file' in vim?

請注意,~/.vim/macros/最適合於此 - 即使沒有人再使用它。甚至更好,你可以把你定義一個函數,並觸發其上的一個鍵綁定

function! s:toggle_settings() abort 
    let s:writing = 1 - get(s:, 'writing', 0) 
    if s:writing 
    colorscheme pencil 
    set background=light 
    set colorcolumn=0 
    set wrap 
    set nonumber 
    else 
    ... 
    endif 
endfunction 

nnoremap µ :call s:toggle_settings()<cr> 

你也可以檢測到當前&filetype is not related to a programming language,然後用自動命令

  1. 到交換機的配置'colorcolumn','wrap''number'處於緩衝級別(:setlocal - 您不想像當前那樣使用:set)。
  2. 如果需要,每次更換窗口時切換colorcheme的配置。但是這種處理方式可能相當積極,開關需求可能更符合人體工程學。
+0

感謝您的信息。我沒有意識到':runtime'是如何工作的。 如果我想稍後改變它,我一定會保留這個標記,但':runtime write.vim'只能在任何地方使用。 –

相關問題