For some personal reasons, I'd like to customize my vim very detailed. 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. But if I was not in that directory, vim works as normal.
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:
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. And that's the point.
So what's the grammar of 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.
You can add to your vimrc:
augroup cpp_setup
autocmd!
autocmd FileType cpp source cpp.vim
augroup END
And then create a cpp.vim file, in wich you put
" 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 . Long tuto, but totally worth it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.