최근 C로 하는 과제들이 있어서 vi를 사용하는 시간이 꽤 많아졌는데, vim에서도 자동완성 플러그인들이 있는지 검색했다가 꽤 활용도가 높아보여 환경을 설정하게 되었다.
이러한 설정 과정들을 간략하게 정리해보려고 한다.
https://github.com/VundleVim/Vundle.vim#about
GitHub - VundleVim/Vundle.vim: Vundle, the plug-in manager for Vim
Vundle, the plug-in manager for Vim. Contribute to VundleVim/Vundle.vim development by creating an account on GitHub.
github.com
https://github.com/ycm-core/YouCompleteMe?tab=readme-ov-file#autocommands
GitHub - ycm-core/YouCompleteMe: A code-completion engine for Vim
A code-completion engine for Vim. Contribute to ycm-core/YouCompleteMe development by creating an account on GitHub.
github.com
위 2개를 적용하여 vim에서의 자동완성 기능을 설정할 수 있었는데 절차가 꽤 복잡하기도 하고 해서 조금 시간이 걸렸다.
Vundle 설치
먼저 vim 폴더 안에 새로운 폴더를 하나 생성해주고 위 Vundle 레포지토리를 클론해준다.
Vundle은 vim 플러그인 매니저라고 보시면 된다.
mkdir ~/.vim/bundle
$ cd ~/.vim/bundle
$ git clone https://github.com/VundleVim/Vundle.vim
그런 다음 .vimrc 설정파일에 들어가서 Vundle을 사용하여 플러그인들을 설치할 수 있도록 내용을 추가해준다.
vim ~/.vimrc
Vundle 플러그인만 설치하려면 " Vundle 설정내용 이라는 주석 아래로만 써도 무방하다.
vimrc 파일에서는 주석을 "(띄어쓰기) 내용 식으로 표기한다고 하네요.
" 검색어 하이라이트
set hlsearch
" 검색시 대소문자 구분
set smartcase
" 현재 커서 위치 표시
set ruler
" 자동으로 들여쓰기
set autoindent
set cindent
" 왼쪽에 몇번째 줄인지 출력
set nu
" Tab 너비 값
set ts=2
" 자동으로 들여쓰기할 너비 값
set shiftwidth=2
" 접근했을때 마지막으로 수정한 곳에서 커서를 위치합니다.
au BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "norm g`\"" |
\ endif
" 신텍스 켬
syntax on
" Vundle 설정내용
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
" The following are examples of different formats supported.
" Keep Plugin commands between vundle#begin/end.
" plugin on GitHub repo
Plugin 'tpope/vim-fugitive'
" plugin from http://vim-scripts.org/vim/scripts.html
" Plugin 'L9'
" Git plugin not hosted on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" Install L9 and avoid a Naming conflict if you've already installed a
" different version somewhere else.
" Plugin 'ascenator/L9', {'name': 'newL9'}
" 자동완성을 지원하는 플러그인
Plugin 'Valloric/YouCompleteMe'
Plugin 'nathanaelkane/vim-indent-guides'
" Fuzzy search 지원 - 파일 탐색을 도와주는 플러그인
Plugin 'ctrlpvim/ctrlp.vim'
" 자동완성 설정 편하게 해주는 역할
Plugin 'rdnetto/YCM-Generator'
" snippet 입력 도와주는 플러그인
Plugin 'SirVer/ultisnips'
" cscope으로 c 분석 도와주는 플러그인
Plugin 'ronakg/quickr-cscope.vim'
Plugin 'junegunn/vim-easy-align'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
해당 파일을 저장 한 번 해주고 다시 들어가서 :PluginInstall을 해주면 위 플러그인들이 설치가 된다.
위 플러그인들이 설치되면 bundle 폴더에 아래와 같이 플러그인 폴더들이 생긴다.
YouCompleteMe 설정
자, 그러면 vim에서의 자동완성 설정이 목적이었으니 이를 도와주는 YouCompleteMe 플러그인 설정을 해야할 차례이다.
레포지토리 설명을 보고 처리하긴 했는데 몇가지 문제가 있어서 조금 복잡했었다.
여튼 CMake나 등등은 다 설치되어있는 상태였어서 스킵하고 (설치안되어있으면 꼭 설치해주기)
ycm은 다양한 언어의 자동완성을 설정해주는데 C나 Python 외에는 당장 사용할 일이 없어 관련된 것들만 설치해줬다.
cd ~/.vim/bundle/YouCompleteMe
./install.py --clangd-completer
설치가 정상적으로 완료된 후, YouCompleteMe 폴더 내의 third_party 아래에 있는 .ycm_extra_conf.py 파일을 ~/.vim 으로 복사해준다.
.ycm_extra_conf.py 파일은 시스템에 설치된 표준헤더들에 대해 자동완성을 수행하나 개발자가 임의로 선언한 클래스, 변수 등에 대한 자동완성 기능은 지원하지 않는다고 한다. 그래서 사용자 선언 함수/변수들의 자동완성은 CMake로 개발 중인 프로젝트에 한해 적용이 가능하다고 해서 이에 대한 설정도 같이 해보기로 했다.
cp third_party/ycmd/.ycm_extra_conf.py ~/.vim/
그래서 위 기능을 활성화하기 위해 사용하고자 하는 폴더 아래에 아래와 같은 옵션을 추가해준다.
그럼 해당 폴더 안에 compile_commands.json이라는 파일이 생성된다.
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1
이후, 아까 복사해둔 ~/.vim/.ycm_extra_conf.py 파일을 해당 폴더(이 폴더를 루트로 설정)에 복사하고 생성된 compile_commands.json 파일을 링크 파일로 세팅한다.
cp ~/.vim/.ycm_extra_conf.py (~/복사할 폴더)
cd /(CMake 설정해둔 폴더)
ln -s ./build/compile_commands.json ./
이제 경로를 설정해줄 차례이다. CMake 프로젝트를 수행할 폴더에서 .ycm_extra_conf.py 파일을 열어보자. 그리고 compilation_database_folder 변수에 compile_commands.json 링크 파일이 있는 경로를 입력한다.
위 과정들을 거쳐서 vim에서의 자동완성 기능을 사용할 수 있게 되었다.