Post

Vim 노트

Vim 에디터 사용법 및 이용 팁등을 기록합니다.

Vim 노트

Vim을 사용한 인코딩 변경하기

  1. 현재 보이는 문자의 인코딩을 수정
    • :e ++enc=utf-8
    • :e ++enc=euc-kr
  2. 현재 파일의 인코딩을 수정
    • :set fileencoding=utf-8
    • :set fileencoding=euc-kr

Neovim 으로 vim 변경하고 환경 설정하기

  1. neovim 설치
    1
    
    brew install neovim
    
  2. vim-plug 설치
    1
    2
    
    sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \
        https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
    
  3. nvim 설정 파일을 생성하고 아래 내용을 추가.
    1
    2
    3
    4
    5
    6
    7
    
    // ~/.config/nvim/init.vim
    call plug #begin('~/.vim/plugged')
    Plug 'preservim/nerdtree' #plugin list
    ...
    call plug #end()
    set number # custom plugin list
    ...
    
  4. vim-plug로 plugin list 설치
    1
    
    :PlugInstall
    

Vim을 IDE로 사용하기 위한 자동완성 플러그인

vim으로 IDE의 자동완성 기능을 사용하고 싶어 여러 유명 플러그인을 붙여보았지마 만족스러운 결과를 계속 얻지 못 했다. 그러다 vim -> neovim 으로 변경하면서 coc를 사용하게 되었고 만족스럽게 사용할 수 있었다.

설치 순서는 저장소 https://github.com/neoclide/coc.nvim 에 잘 나와있다.

  1. nodejs >= 12.12 설치
    1
    
    curl -sL install-node.vercel.app/lts | bash
    
  2. vim-plug 에 추가
    1
    
    Plug 'neoclide/coc.nvim', {'branch': 'release'}
    
  3. 설치
    1
    
    :PlugInstall 
    
  4. CoC 언어별 플러그인 설치
    1
    2
    3
    
    :CocInstall coc-json coc-tsserver
    :CocCommand coc-clangd
    # 위 명령어 정상 실행 안 되면 >> :CocCommand clangd.install
    

기타 추가 설정법은 위의 저장소 README를 참조


neo vim swap file error 생길 때

모종의 이유로 에디터가 비정상 종료되어, 임시 파일이 삭제 되지 않고 남아있을 때 나타나는 현상. 임시 파일을 강제로 삭제 해주면 해결됨

  • ~/.local/share/nvim/swap 폴더 아래의 모든 파일을 삭제
  • rm -rf ~/.local/share/nvim/swap/*

vim 옵션 정리

옵션기능
set nocompatible오리지널 vi와호환되지 않음
set autoindent자동 듷여쓰기
set cindentC 프로그래밍용 자동들여쓰기
set smartindent스마트한 들여쓰기
set wrap 
set nowrapscan검색할 떄 문서의 끝에서 처음으로 돌아감
set nobackup백업파일을 안만듬
set visualbell키를 잘못눌렀을 때 화면 표시
set ruler화면 우측 하단에 현재 커서의 위치(줄, 칸) 표시
set shilftwidth=4자동 들여쓰기 4칸
set number행번호 표시, set nu가능
set fencs=ucs-bom,utf-8,euc-kr,latin1한글 파일은 euc-kr로, 유니코드는 유니코드로
set fileencoding=utf-8파일 저장 인코딩
set tenc=utf-8터미널 인코딩
set expandtab탭 대신 스페이스
set hlsearch검색어 강조, set his도 가능
set ignorecase검색 시 대소문자 무시, set ic도 가능
set tabstop=4탭을 4칸으로
set lbr 
set incsearch키워드 입력시 점진적 검색
syntax on구문강조 사용
filetype indent on파일 종류에 따른 구문강조
set background=dark하이라이팅 light / dark
colorscheme desertvi 색상 테마 설정
set backspace=eol,start,indent줄을 끝, 시작, 들여쓰기에서 백스페이스시 이전종료
set history=1000vi 편집기록 기억갯수 .viminfo에 기록

문단 모양

>%, >ib, >at

  • 중소괄호 구간, 소괄호 내부, 태그 구간 들여쓰기

여러 파일 편집

<C-w> + x

  • 창 바꾸기

<C-w> + _

  • 가로 분리일 때 zoom in

<C-w> + |

  • 세로 분리일 때 zoom in

<C-w> + =

  • 창 크기 동일하게 조정

자동완성

<C-p> <C-n>

  • vim 기본 자동완성 insert 모드에서 ctrl + p, ctrl + n을 누르면 vim의 complete 옵션에서 지정한 키워드를 기반으로 자동완성해줌

<C-x>

  • insert 모드에서만 지원하는 자동완성
  • <C-x><C-l>: 라인단위 자동완성
  • <C-x><C-f>: 파일명, 경로 자동완성

abbreviate

  • 축약어를 등록할 수 있다. 오타교정, 자동완성등의 용도로 유용하게 사용할 수 있다.
  1. abbr: insert, command 모드 모두 작동
  2. iabbr: insert 모드에서만 작동
  3. cabbr: command 모드에서만 작동
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//.vimrc 파일에 다음과 같이 등록해놓고 사용할 수 있다.
//오타교정
abbr consolee console
abbr coment comment

//자동완성
abbr cns console.log()
abbr cmt /* */

//<Esc>를 활용해 normal 모드 복구
abbr cmt /* */<Esc>hhi

//기타 
//<expr>을 사용하면 스크립트 실행 결과로 완성
iabbr __email abcd@efgh.com
iabbr <expr> __time strftime("%Y-%m-%d %H:%M:%S")
iabbr <expr> __file expand('%:p')
iabbr <expr> __name expand('%')
iabbr <expr> __pwd expand('%:p:h')
iabbr <expr> __branch system("git rev-parse --abbrev-ref HEAD")

참조: https://johngrib.github.io/wiki/vim-auto-completion/

This post is licensed under CC BY 4.0 by the author.