1 분 소요

설명


설치

  1. curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  2. 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()
  1. 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 등록

	...
	call plug#end()

	call glaive#Install()
	Glaive codefmt google_java_executable="java -jar /root/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;
    }