git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/2] Add project-wide .vimrc configuration

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Dec 9, 2020, 06:55 UTC
Message-ID
<20201209065537.48802-2-felipe.contreras@gmail.com>
In-Reply-To
<20201209065537.48802-1-felipe.contreras@gmail.com>

It's not efficient that everyone must set specific configurations in all their ~/.vimrc files; we can have a project-wide .vimrc that everyone can use.

There's different ways to load this configuration, for example with vim-addon-local-vimrc [1], but we don't need much of the complexity of these solutions.

Instead I created a simple loader that is in the contrib area, which can be installed with:

  cp -aT contrib/vim ~/.vim/pack/plugins/start/git
Then, add the location of the Git repository to your ~/.vimrc:
  let g:gitvimrc_whitelist = [ expand('$HOME') . '/dev/git' ]

Then the project-wide configuration will be loaded, which sets the correct filetype for the documentation, and also the default indentation of c, sh, perl, and asciidoc files.

These default configurations can be overridden in the typical way (by adding the corresponding file in ~/.vim/after/ftplugin).

We could add the vim modelines at the bottom of every file, like other projects do, but this seems more sensible.

[1] https://github.com/MarcWeber/vim-addon-local-vimrc
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 .vimrc                          | 22 ++++++++++++++++++++++
 contrib/vim/plugin/gitvimrc.vim | 21 +++++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 .vimrc
 create mode 100644 contrib/vim/plugin/gitvimrc.vim
diff --git a/.vimrc b/.vimrc
new file mode 100644
index 0000000000..602c746477
--- /dev/null
+++ b/.vimrc
@@ -0,0 +1,22 @@
+" To make use of these configurations install the git plugin provided in
+" the contrib section:
+"
+"   cp -aT contrib/vim ~/.vim/pack/plugins/start/git
+"
+" Then whitelist the location of this directory to your ~/.vimrc:
+"
+"   let g:gitvimrc_whitelist = [ expand('$HOME') . '/dev/git' ]
+"
+" You can add multiple locations, or specify a regexp pattern.
+"
+
+augroup git
+	au BufRead,BufNewFile */Documentation/*.txt set filetype=asciidoc
+
+	au FileType c setl noexpandtab tabstop=8 shiftwidth=0 cino=(s,:0,l1,t0
+	au FileType sh setl noexpandtab tabstop=8 shiftwidth=0
+	au FileType perl setl noexpandtab tabstop=8 shiftwidth=0
+	au FileType asciidoc setl noexpandtab tabstop=8 shiftwidth=0 autoindent
+augroup END
+
+" vim: noexpandtab tabstop=8 shiftwidth=0
diff --git a/contrib/vim/plugin/gitvimrc.vim b/contrib/vim/plugin/gitvimrc.vim
new file mode 100644
index 0000000000..c3946e5410
--- /dev/null
+++ b/contrib/vim/plugin/gitvimrc.vim
@@ -0,0 +1,21 @@
+let s:gitvimrc_whitelist = get(g:, 'gitvimrc_whitelist', [])
+
+function LoadGitVimrc()
+  let l:top = trim(system('git rev-parse --show-toplevel'))
+  if l:top == '' | return | endif
+  let l:file = l:top . '/.vimrc'
+  if !filereadable(l:file) | return | endif
+
+  let l:found = 0
+  for l:pattern in s:gitvimrc_whitelist
+    if (match(l:top, l:pattern) != -1)
+      let l:found = 1
+      break
+    endif
+  endfor
+  if !l:found | return | endif
+
+  exec 'source ' . fnameescape(l:file)
+endf
+
+call LoadGitVimrc()
-- 
2.29.2
Previous: Felipe ContrerasNext: Christian Brabandt
Message 2 of 22 in “vim: configuration and sharness syntax”
  1. 0/2 vim: configuration and sharness syntaxFelipe Contreras, Dec 9, 2020
  2. 1/2 Add project-wide .vimrc configurationFelipe Contreras, Dec 9, 2020
  3. Christian BrabandtDec 9, 2020
  4. Felipe ContrerasDec 9, 2020
  5. Christian BrabandtDec 9, 2020
  6. Jeff KingDec 9, 2020
  7. Felipe ContrerasDec 10, 2020
  8. Jeff KingDec 10, 2020
  9. Felipe ContrerasDec 11, 2020
  10. brian m. carlsonDec 10, 2020
  11. Felipe ContrerasDec 11, 2020
  12. brian m. carlsonDec 11, 2020
  13. Felipe ContrerasDec 11, 2020
  14. Jeff KingDec 15, 2020
  15. Felipe ContrerasDec 15, 2020
  16. Jeff KingDec 15, 2020
  17. Felipe ContrerasDec 15, 2020
  18. 2/2 contrib: vim: add sharness syntax fileFelipe Contreras, Dec 9, 2020
  19. Eric SunshineDec 9, 2020
  20. Felipe ContrerasDec 9, 2020
  21. Jeff KingDec 9, 2020
  22. Felipe ContrerasDec 10, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.