[Vim] 자동 코드 포맷팅
Updated:
설명
- https://github.com/google/vim-codefmt
- 자동 코드 포맷팅을 위한 유틸리티
설치
curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
vim ~/.vimrc
augroup autoformat_settings autocmd FileType bzl AutoFormatBuffer buildifier autocmd FileType c,cpp,proto,javascript,arduino AutoFormatBuffer clang-format autocmd FileType dart AutoFormatBuffer dartfmt autocmd FileType go AutoFormatBuffer gofmt autocmd FileType gn AutoFormatBuffer gn autocmd FileType html,css,sass,scss,less,json AutoFormatBuffer js-beautify autocmd FileType java AutoFormatBuffer google-java-format autocmd FileType python AutoFormatBuffer yapf " Alternative: autocmd FileType python AutoFormatBuffer autopep8 autocmd FileType rust AutoFormatBuffer rustfmt autocmd FileType vue AutoFormatBuffer prettier augroup END call plug#begin() Plug 'google/vim-maktaba' Plug 'google/vim-codefmt' Plug 'google/vim-glaive' call plug#end()
vim 실행 후 :PlugInstall 입력
C++ formatter 등록
dnf install git-clang-format
vim ~/.vimrc
... call plug#end() call glaive#Install() Glaive codefmt clang_format_executable='/usr/bin/clang-format'
vim ~/.clang-format
ColumnLimit: 100 IndentWidth: 4 TabWidth: 4 IndentAccessModifiers: true UseTab: Always
Python formatter 등록
pip install yapf
Java formatter 등록
- google-java-format releases에서 google-java-format-xxx-all-deps.jar 다운로드
vim ~/.vimrc
... call plug#end() call glaive#Install() Glaive codefmt google_java_executable="java -jar /home/chp/library/google-java-format-1.15.0-all-deps.jar"
비고
- set tabstop=4가 설정되어 있는 경우 set shiftwidth=4도 설정해주어야 자동 들여쓰기가 두번되지 않음
확인
- 코드 작성 후 저장 시 자동 포맷팅
- 작성
#include <iostream> using namespace std; class test { private: int i; public: int j; int func() { return 0; } }; int main(void) { int i = 0; if (i == 0) { cout << i << endl; } return 0; }
- 저장
#include <iostream> using namespace std; class test { private: int i; public: int j; int func() { return 0; } }; int main(void) { int i = 0; if (i == 0) { cout << i << endl; } return 0; }