]> localhost Git - dotfiles.git/commitdiff
Add commands to vim for autoformat
authorMisomosi <[email protected]>
Sun, 3 Nov 2024 21:13:35 +0000 (16:13 -0500)
committerMisomosi <[email protected]>
Sun, 3 Nov 2024 21:13:35 +0000 (16:13 -0500)
.local/bin/git-vim-format [new file with mode: 0644]
.vimrc
README.md

diff --git a/.local/bin/git-vim-format b/.local/bin/git-vim-format
new file mode 100644 (file)
index 0000000..b0a18b0
--- /dev/null
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+# Shell script for auto-formatting on BufWritePost in vim
+# I don't want to run this on all files, just those in git
+# repos which have a strict code style.
+BUFNAME="$1"
+if git config --get clangformat.style >/dev/null ; then
+       exec clang-format -i --style="$(git config --get clangformat.style)" "$BUFNAME"
+fi
diff --git a/.vimrc b/.vimrc
index 8ef5a28f53c07978d129384135a1614f41305462..db92c932663a3f9bf3d6047733226f8e76a39380 100644 (file)
--- a/.vimrc
+++ b/.vimrc
@@ -1 +1,7 @@
 source ~/.exrc
+set autoread
+function! GitVimFormat(filename)
+       call system('git vim-format ' . shellescape(a:filename))
+       e
+endfunction
+au BufWritePost *.c call GitVimFormat(bufname())
index 2032f2a52f64159dac90c4b7e87ac59d7a4e6cfa..0ba49accd5cf58054bf1554d97f307f2de42c0e1 100644 (file)
--- a/README.md
+++ b/README.md
@@ -22,6 +22,25 @@ set wrapscan
 ## .vimrc
 ```
 source ~/.exrc
+set autoread
+function! GitVimFormat(filename)
+       call system('git vim-format ' . shellescape(a:filename))
+       e
+endfunction
+au BufWritePost *.c call GitVimFormat(bufname())
+```
+
+## git-vim-format
+```
+#!/bin/sh
+
+# Shell script for auto-formatting on BufWritePost in vim
+# I don't want to run this on all files, just those in git
+# repos which have a strict code style.
+BUFNAME="$1"
+if git config --get clangformat.style >/dev/null ; then
+       exec clang-format -i --style="$(git config --get clangformat.style)" "$BUFNAME"
+fi
 ```
 
 ## .tmux.conf