【问题标题】:Different vim files for different languages and task不同语言和任务的不同vim文件
【发布时间】:2020-01-16 17:50:10
【问题描述】:

我想在 vim 中为不同的任务创建不同的 vim 文件。我知道你可以创建不同的 vim 文件,这些文件可以根据文件的扩展名动态加载。我的问题是我正在使用 vundle 来维护插件,我真的不知道如何将这些插件分隔在不同的文件中。

我搜索了有关分离 vim 的信息,发现您可以使用 ftplugin,例如 ftplugin/python.vim 或 ftplugin/matlab.vim。但我不知道我应该在每个 .vim 文件中编写 vundle 部分,还是所有内容都应该在一个 vim 文件中。 如果您需要更多信息,请告诉我。下面是我当前的 .vimrc 文件。

" Configuration file for vim
set modelines=0     " CVE-2007-2438

" Normally we use vim-extensions. If you want true vi-compatibility
" remove change the following statements
set nocompatible    " Use Vim defaults instead of 100% vi compatibility
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

"===================================================================
"Plugins 
" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'

" For autocomplete
Bundle 'Valloric/YouCompleteMe'

" For folding 
Plugin 'tmhedberg/SimpylFold'

" For indent python
Plugin 'vim-scripts/indentpython.vim'

" For syntax
Plugin 'w0rp/ale'
" Check Python files with flake8 and pylint.
let b:ale_linters = ['flake8', 'pylint']
" Fix Python files with autopep8 and yapf.
let b:ale_fixers = ['autopep8', 'yapf']
" Disable warnings about trailing whitespace for Python files.
let b:ale_warn_about_trailing_whitespace = 0
syntax on

" For color Schemes
"Plugin 'jnurmine/Zenburn'
Plugin 'flazz/vim-colorschemes'
Plugin 'morhetz/gruvbox'

" For PowerLine
"Plugin 'powerline/powerline', {'rtp': 'powerline/bindings/vim/'}
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'

"For the nerd tree 
Plugin 'scrooloose/nerdtree'

" add all your plugins here (note older versions of Vundle
" used Bundle instead of Plugin)

" ...

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

"===================================================================
" For UTF-8
set encoding=utf-8

"System Clipboard
if has('mac')
    set clipboard=unnamed
elseif has('unix') 
    set clipboard=unnamedplus
endif

"set Line Numbering
set nu

"to handle the backspace problem
set bs=2

"Set up mouse 
set mouse=a 

"For Highlighting searched text
set hlsearch

"For confirming before exit (save)
set confirm

"Maping Ctrl+A for select all 
map <C-a> <esc>ggVG<CR>

"===================================================================
" Mapping NERDtree toggling 
nmap <F6> :NERDTreeToggle<CR>

"===================================================================
"Few settings for plugins 

" colorscheme 
colorscheme py-darcula

" to see the docstrings for folded code
let g:SimpylFold_docstring_preview=1
let mapleader=" "

"The first line ensures that the auto-complete window goes away when you’re
"done with it, and the second defines a shortcut for goto definition (second
"one I need to learn)
let g:ycm_autoclose_preview_window_after_completion=1
let g:ycm_min_num_of_chars_for_completion = 1
"map <leader>g  :YcmCompleter GoToDefinition<CR>

"To handle vitural env for YCM 
let g:ycm_python_binary_path = 'python3'

【问题讨论】:

  • 你想在这些文件中做什么?它是否设置了一些选项,如间距和缩进?添加特定于语言的功能?还有什么?你可能根本不需要任何单独的文件,这取决于你想要做什么。
  • 我想为不同的文件使用不同的插件。例如,在 python 文件中,我想使用插件进行缩进,如“vim-scripts/indentpython.vim”。但是对于文本文件,我想使用不同的插件来做笔记。我想将它们分开,所以我的缩进不会弄乱。另外这样做我希望我只会加载特定于文件的插件(在这种情况下我的假设可能是错误的。)
  • 这些插件中的大多数只会影响特定的语言。例如,Python 插件可以在您的.vimrc 中加载,并且它们应该仅在您编辑 Python 时生效。同样,笔记插件通常只会影响某些文本格式(如果它们影响多种格式,您通常可以配置哪些)。

标签: vim vim-plugin vim-syntax-highlighting


【解决方案1】:

可以使用单独的配置文件(您可以通过-u {vimrc} 命令行参数覆盖默认的.vimrc;shell 别名可以使这变得非常容易)。但是,对于大多数用户来说,这种复杂性应该不是必需的。

特别是,Vim 以 buffer-local optionsfiletype 插件 的形式内置了不同类型文件的设置范围。因此,如果您想要 Python 和文本文件的不同缩进,那应该是开箱即用的。即使您正在全局安装像vim-scripts/indentpython.vim 这样的插件(在您的情况下通过Vundle),它只会在:setlocal filetype? 返回python 的文件上激活。 (顺便说一句,该插件最后一次更新是在 9 年前,现在 Vim 附带了一个积极维护的 $VIMRUNTIME/indent/python.vim。)

对于全局插件,始终安装它们通常并没有什么坏处,即使您可能只在某些项目中使用它们。 Vim 的autoload 机制根据需要动态包含主要插件功能,以缩短 Vim 启动时间。激活不同的插件集只对极少数高级用户有意义(这些用户已经对 Vim 的插件机制有非常深入的了解,不需要问这样的问题)。

您可以在:help filetypes:help local-options 找到更多相关信息。

【讨论】:

    【解决方案2】:

    据我了解,现有插件工作正常,但您想知道编写自己的插件的正确方法。我说的对吗?

    如果是这样,那么您不必编写与 Vundle 或任何其他插件管理器有任何关系的插件(或其他 vim 文件)。您只需要:

    • 阅读:help rtp
    • 根据需要为您的文件创建一个文件夹。
    • 为新文件夹设置runtimepath 选项。通常,它只是使用 += 运算符添加新路径。
    • 了解用于搜索运行时文件的目录列表,创建您想要获取的功能。

    还有一次:如果您正在编写自己的 vim 文件,请离开 Vundle。文德尔, 与任何其他插件管理器一样,它只是从 github 存储库中检索代码并根据新数据设置 rtp 选项的便捷工具。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 2018-06-05
      相关资源
      最近更新 更多