From cc4c89f99575f5a386b1cd2cb2f9fa6c03ec7e0e Mon Sep 17 00:00:00 2001 From: Viktor Varland Date: Mon, 9 May 2022 18:30:32 +0200 Subject: [PATCH] feat: project specific vimrc configuration --- README.md | 68 ++++++++++++++++++++++++++++++++++++++ autoload/projectconfig.vim | 19 +++++++++++ plugin/projectconfig.vim | 13 ++++++++ 3 files changed, 100 insertions(+) create mode 100644 README.md create mode 100644 autoload/projectconfig.vim create mode 100644 plugin/projectconfig.vim diff --git a/README.md b/README.md new file mode 100644 index 0000000..fa012d4 --- /dev/null +++ b/README.md @@ -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 `` 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 `` in [project configuration](#buffer-scope-for-project-configuration). diff --git a/autoload/projectconfig.vim b/autoload/projectconfig.vim new file mode 100644 index 0000000..3dce213 --- /dev/null +++ b/autoload/projectconfig.vim @@ -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 diff --git a/plugin/projectconfig.vim b/plugin/projectconfig.vim new file mode 100644 index 0000000..0236e74 --- /dev/null +++ b/plugin/projectconfig.vim @@ -0,0 +1,13 @@ +" Title: Project Config +" Description: Load local configuration for a project +" Last Change: 2022-05-09 +" Maintainer: Viktor Varland + +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()