简体   繁体   English

如何加载一个 ~/.vimrc 然后另一个 local.vimrc

[英]How to load a ~/.vimrc then another local .vimrc

I would like to load a ~/.vimrc with general config (color, line number, etc) and then load another.vimrc i have in local file (with specific conf for python file for example).我想加载一个具有一般配置(颜色、行号等)的 ~/.vimrc,然后加载我在本地文件中的另一个.vimrc(例如 python 文件的特定配置)。

Decluttering / structuring an overly long vimrc整理/构建过长的 vimrc

Say, you have a lot of customizations and tweaks, and you'd like a bit more structure.比如说,你有很多自定义和调整,你想要更多的结构。 For :set , :map , and :autocmd , you can just put them in a plugin script like ~/.vim/plugin/personal_customization.vim .对于:set:map:autocmd ,您可以将它们放在像~/.vim/plugin/personal_customization.vim这样的插件脚本中。 For plugin customizations (eg let g:PluginName_Frobnize = 1 ), you'd cause order-of-execution issues (unless you choose a plugin directory that comes early in 'runtimepath' or use a special filename ( 00plugin_customization.vim ) that is read first.对于插件自定义(例如let g:PluginName_Frobnize = 1 ),您会导致执行顺序问题(除非您选择在'runtimepath'运行时路径”早期出现的plugin目录或使用读取的特殊文件名( 00plugin_customization.vim )第一的。

filetype-specific settings特定于文件类型的设置

Vim has you covered here, but some people are not aware of it; Vim 这里有你介绍,但有些人不知道;:help usr_43.txt explains filetype-specific settings.:help usr_43.txt解释了特定于文件类型的设置。

In short, if you only want to enable an option for certain filetypes, use :setlocal option=value , and put the corresponding :setlocal commands into ~/.vim/after/ftplugin/{filetype}.vim , where {filetype} is the actual filetype (eg java ).简而言之,如果您只想为某些文件类型启用选项,请使用:setlocal option=value ,并将相应的:setlocal命令放入~/.vim/after/ftplugin/{filetype}.vim ,其中{filetype}是实际文件类型(例如java )。 (This requires that you have :filetype plugin on ; use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/{filetype}.vim .) (这要求你有:filetype plugin on ;使用after目录允许你覆盖任何由$VIMRUNTIME/ftplugin/{filetype}.vim完成的默认文件类型设置。)

Alternatively, you could define an :autocmd FileType {filetype} setlocal option=value directly in your ~/.vimrc , but this tends to become unwieldy once you have many customizations - this may have just brought you here.或者,您可以直接在~/.vimrc中定义一个:autocmd FileType {filetype} setlocal option=value ,但是一旦您进行了许多自定义,这往往会变得笨拙 - 这可能只是将您带到这里。

file-specific settings文件特定设置

To define special customizations just for a single file, my ModelineCommands plugin can extend modelines beyond the built-in options that can be specified there by default.要为单个文件定义特殊自定义,我的ModelineCommands 插件可以将模式线扩展到默认情况下可以在其中指定的内置选项之外。

For more extensive customizations, it will be better to source an accompanying Vimscript file (eg foo.java.vim ) that lies next to the file ( foo.java ):对于更广泛的自定义,最好获取位于文件旁边的随附 Vimscript 文件(例如foo.java.vim )( foo.java

function! s:AutoSource()
    let l:testedScripts = [expand('<afile>') . '.vim']
    if expand('<afile>:e') !=# 'vim'    " Don't source the edited Vimscript file itself.
        call add(l:testedScripts, expand('<afile>:r') . '.vim')
    endif

    for l:filespec in l:testedScripts
        if filereadable(l:filespec)
            execute 'source' fnameescape(l:filespec)
        endif
    endfor
endfunction
augroup AutoSource
    autocmd! BufNewFile,BufRead * nested call <SID>AutoSource()
augroup END

Alternatively, the following local configuration approaches can also be applied to individual files (by switching on the current filename).或者,也可以将以下本地配置方法应用于单个文件(通过打开当前文件名)。

local / project-specific configuration本地/项目特定配置

This often comes up in the form of using different configurations for work vs. private stuff, different projects, etc. There are different approaches:这通常以使用不同的工作配置与私人的东西、不同的项目等的形式出现。有不同的方法:

Local config - central configuration本地配置 - 中央配置

If it's okay to configure the specific commands / local exceptions centrally, you can put such autocmds into your ~/.vimrc :如果可以集中配置特定命令/本地异常,则可以将此类 autocmds 放入~/.vimrc

:autocmd BufRead,BufNewFile /path/to/dir/* setlocal ts=4 sw=4

It is important to use :setlocal instead of :set , and likewise :map <buffer>... and :command. -buffer...使用:setlocal代替:set很重要,同样使用:map <buffer>...:command. -buffer... :command. -buffer... . :command. -buffer...

On the other hand, if you want the specific configuration stored with the project (and don't want to embed this in all files via modelines ), you have the following two options:另一方面,如果您希望将特定配置与项目一起存储(并且不想通过modelines将其嵌入所有文件中),您有以下两个选项:

Local config with built-in functionality具有内置功能的本地配置

If you always start Vim from the project root directory, the built-in如果总是从项目根目录启动 Vim,内置

:set exrc

enables the reading of a .vimrc file from the current directory.允许从当前目录读取.vimrc文件。 You can place the :set ts=4 sw=4 commands in there.您可以将:set ts=4 sw=4命令放在那里。

Local config through plugin通过插件进行本地配置

Otherwise, you need the help of a plugin;否则,您需要插件的帮助; there are several on vim.org; vim.org上有几个; I can recommend the localrc plugin (especially with my own enhancements ), which even allows local filetype-specific configuration.我可以推荐localrc 插件(尤其是我自己的增强功能),它甚至允许本地文件类型特定的配置。

Note that reading configuration from the file system has security implications;请注意,从文件系统读取配置具有安全隐患; you may want to :set secure .你可能想:set secure

A local config would be loaded with a line like this in your .vimrc :本地配置将在您的.vimrc中加载如下行:

source ~/.vim/local.vim

You could put in that file settings that are particular to your machine, for example.例如,您可以输入特定于您的机器的文件设置。

But settings specific to Python should go into either .vim/syntax/python.vim , or (more likely) .vim/after/syntax/python.vim . But settings specific to Python should go into either .vim/syntax/python.vim , or (more likely) .vim/after/syntax/python.vim . You do not need to source them, they will be sourced automatically whenever you open a Python file, as long as you have at least this in your .vimrc :您不需要获取它们,只要您打开 Python 文件,它们就会自动获取,只要您的.vimrc中至少有这个:

filetype plugin on

(though I suggest you have filetype plugin indent on instead.) (虽然我建议你改用filetype plugin indent on 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM