feat: project specific vimrc configuration

This commit is contained in:
Viktor Varland 2022-05-09 18:30:32 +02:00
parent 5c0868f369
commit 6377141608
Signed by: varl
GPG key ID: 7459F0B410115EE8
3 changed files with 100 additions and 0 deletions

68
README.md Normal file
View file

@ -0,0 +1,68 @@
# Project Config
Drop the plugin into `~/.vim/pack/{foo}/start/projectconfig`.
In your ~/.vimrc you need to set up the project directories and the
configuration file to map to:
```
let g:projectconfig_projects = {
\ '~/dev/my-project' : '~/.config/my-project.vim',
\ '~/wiki' : '~/wiki/.vimrc',
\ }
```
And to load the configuration for the file that you opened:
E.g.
```
au BufNewFile,BufRead ~/dev/* call projectconfig#load()
```
# Buffer scope for project configuration
Prefer `setlocal`, `localleader` and `<buffer>` for project
configuration, so that the changes are scoped to a specific buffer and
doesn't override your default, or other project config when you open
various files.
E.g. to override my default from 4 spaces to 2 spaces in a project I
would use:
`~/.vimrc`:
```
# default configuration
set tabstop=4
set shiftwidth=4
```
and in e.g. `~/.config/my-project.vim`
```
# my-project configuration
setlocal tabstop=2
setlocal shiftwidth=2
```
# Caveats
## `('._.)`
- Only tested with VIM 8.2.
## `(;._.)`
- **BE MINDFUL OF WHERE THE CONFIG LIVES**. If you have `vimrc` file
stored in the project repo, changes to it will happily be sourced the
next time you pull. This plugin does not do verification.
## `(╯._.)╯︵ ┻━┻`
- If a project config sets a setting that the base, or next project
config doesn't, then the project config will still be active when
changing to a file in another project. Work around this by customising the `autocmd` and use `setlocal`,
`localleader`, and `<buffer>` in [project configuration](#buffer-scope-for-project-configuration).

View file

@ -0,0 +1,19 @@
function! projectconfig#find() abort
let l:basedir = expand('%:p:h')
for l:project in keys(g:projectconfig_projects)
if l:basedir =~ l:project
return get(g:projectconfig_projects, l:project, '')
endif
endfor
endfunction
function! projectconfig#load() abort
let l:file = projectconfig#find()
if filereadable(l:file)
let l:safe = fnameescape(l:file)
execute 'source' l:safe
return 'Loaded project configuration: ' . l:safe
else
return 'Using default project configuration'
endif
endfunction

13
plugin/projectconfig.vim Normal file
View file

@ -0,0 +1,13 @@
" Title: Project Config
" Description: Load local configuration for a project
" Last Change: 2022-05-09
" Maintainer: Viktor Varland <https://github.com/varl>
if exists("g:loaded_projectconfig")
finish
endif
let g:loaded_projectconfig = 1
" Exposes the plugin's functions for use as commands in Vim.
command! -nargs=0 ProjectConfigFind call projectconfig#find()
command! -nargs=0 ProjectConfigLoad call projectconfig#load()