如何在模式行上加载 .vim 文件

发布于 2025-01-16 18:41:36 字数 166 浏览 3 评论 0原文

对于一些特定文件,我需要一组映射,但我不想在我的 ~/.vimrc 文件中激活它们。所以我在文件 XXX.vim 中定义了它们。 如何在模式行上加载 XXX.vim 以及它应该放在我的 .vim 目录中的什么位置?

For a few specific files I need a set of mappings, which I don't want to be active in my ~/.vimrc file. So I defined them in a file XXX.vim.
How can I load XXX.vim on a mode line and where should it be placed in my .vim directory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜雨飘雪 2025-01-23 18:41:36

由于显而易见的原因,您无法在模型行中获取脚本。您所能做的就是设置一些选项。

让某些选项/映射/命令可用于某些文件而不是其他文件的规范方法是使用 :help autocommand ,如下所示:

augroup mystuff
    autocmd!
    autocmd BufNewFile,BufRead *.foo nnoremap <buffer> <key> bar
augroup END

您可以将上面的模式用于带有 的所有文件foo 扩展名或类似 foo/bar/* 的内容,用于 foo/bar/ 下的所有文件等。

如果您只想为特定的 文件类型,说ruby,那么最好使用内置的 ftplugin 机制:

  1. 确保您的 vimrc 中有以下任一行>:

    文件类型插件开启
    文件类型插件缩进
    文件类型缩进插件
    
  2. 如果该文件不存在,则创建该文件(以及必要的目录):

    " 类 Unix 系统
    〜/.vim/after/ftplugin/ruby.vim
    
    ” 窗户
    %userprofile%\vimfiles\after\ftplugin\ruby.vim
    
  3. 将缓冲区本地映射添加到该文件:

    nnoremap <缓冲区> <键>某物
    

You can't source a script in a modeline for obvious reason. All you can do is set some options.

The canonical way to have some options/mappings/commands available for some files and not others is to use an :help autocommand like this one:

augroup mystuff
    autocmd!
    autocmd BufNewFile,BufRead *.foo nnoremap <buffer> <key> bar
augroup END

You can use the pattern above for all files with the foo extension or something like foo/bar/* for all file under foo/bar/, etc.

If you want those mappings only for a specific filetype, say ruby, then it is best to use the built-in ftplugin mechanism:

  1. Make sure you have either of the following lines in your vimrc:

    filetype plugin on
    filetype plugin indent on
    filetype indent plugin on
    
  2. Create this file (and the necessary directories) if it doesn't exist:

    " Unix-like system
    ~/.vim/after/ftplugin/ruby.vim
    
    " Windows
    %userprofile%\vimfiles\after\ftplugin\ruby.vim
    
  3. Add your buffer-local mappings to that file:

    nnoremap <buffer> <key> something
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文