简体   繁体   English

vimrc的语法是什么,如何在vimrc中使用变量,函数?

[英]What's the grammar of vimrc, how to use variable, function in vimrc?

For some personal reasons, I'd like to customize my vim very detailed. 由于某些个人原因,我想非常详细地自定义我的vim。 For example, in a specific directory, say "CFxxx", when I create a cpp file, I'd like it to pre-write some template code into my code. 例如,在特定目录中,说“ CFxxx”,当我创建一个cpp文件时,我希望它可以将一些模板代码预写到我的代码中。 But if I was not in that directory, vim works as normal. 但是,如果我不在该目录中,那么vim可以正常工作。

As I found on others' vimrc, they define a function, and use filetype to detect cpp files, and use codes like this to add template code: 正如我在其他人的vimrc上发现的那样,它们定义了一个函数,并使用文件类型检测cpp文件,并使用如下代码添加模板代码:

if (expand("%:e") == 'cpp' || expand("%:e") == 'cc')
    call append(line(".")+6, "#include<bits/stdc++.h>")
    call append(line(".")+7, "using namespace std;")
    call append(line(".")+8, "")
endif

But that's weaker than what I want. 但这比我想要的要弱。 I'd like it to be directory-specific and filetype-specific. 我希望它是特定于目录和特定于文件类型的。 I found there's a function getcwd() as a builtin function in vim, which can be used to get the directory, but I don't know how to use it in vimrc. 我发现有一个函数getcwd()作为vim中的内置函数,可以用来获取目录,但是我不知道如何在vimrc中使用它。 And that's the point. 这就是重点。

So what's the grammar of vimrc? 那么vimrc的语法是什么? Is it a famous programming language? 它是一种著名的编程语言吗? Where can I learn to write the correct code to customize my vim and solve the above problem. 在哪里可以学习编写正确的代码来自定义我的vim并解决上述问题。

You can add to your vimrc: 您可以添加到vimrc中:

augroup cpp_setup
    autocmd!
    autocmd FileType cpp source cpp.vim
augroup END

And then create a cpp.vim file, in wich you put 然后创建一个cpp.vim文件,放入

" General stuff for all your cpp file
let b:com = '//'

inoremap <buffer> <leader>s std::
inoremap <buffer> <leader>str std::string

" specific stuff for your specific dir
if getcwd() ==# 'diryouwant'
    "Do what you want here, such has:
    set wrap
    call setline('1', "#include <cstdlib>")
endif

You will find explanations about those lines (autocommand, conditionnal, function) in this wonderfull tutorial: learnvimscriptthehardway . 在这个精彩的教程中,您将找到有关这些行(自动命令,条件语句 ,函数)的说明: learningvimscriptthehardway Long tuto, but totally worth it. 长途兔,但完全值得。

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

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