본문 바로가기

O/S/Linux-CentOS

VIM 스터디 (NOVA 스터디 발표용) - 필수유틸리티 Chapter 02 Section05,06

CentOS 리눅스에서 VIM 환경 설정을 해보았다.
# vim
으로 실행하면 된다. 간혹 vi로 하는 경우가 있는데 최신 버전이 실행 안될 수도 있다.
/root 디렉토리에 .bashrc 를 vim으로 연다음 alias vi='vim' 을 추가한다.
#cd /root
#vim .bashrc




VIM명령어이다. 자주 쓰이고 간단한 명령어는 검색을 통해 사용하면 된다.
VIM을 왠만큼 사용할줄 알면 알아두면 좋은 단축키 이다.
자세한 명령어를 보고 싶으면 http://ehkx.tistory.com/158 참고하길 바란다.

1. 파일 탐색 기능 활용
:vs   세로로 추가
:20vs ./
:vs test.cpp
:sp   가로로 추가

2. 빠른 괄호 이동 및 빠른 탐색
%             { 기호에서 누르면 쌍을 이루는 {또는 }로 이동한다.
* 또는 #     찾고자 하는 문자열 커서에 놓고 누르면 찾는다.

3. 폴딩 기능
zf        블록 잡은 다음 실행시 폴딩 됨
v]}      { 커서 위에서 실행시 쌍을 이루는 곳까지 블록 잡는다.

v]}zf    {  } 블록을 잡고 폴딩한다.

zo        언폴딩 한다.

4. 함수의 원형과 지역변수의 선언 부분으로 바로 이동
[i       함수의 원형          
gd      지역변수의 선언된 위치  

5. 여러 행 탭 끼워넣기
[N]>> 또는 [N]<<
블록을 잡고 > 또는 < 하면 된다.

6. 탭싸이즈 조정
set ts=8            '/t' 문자일때     tabstop
set sw=4          >> 또는 << 할때나, 들여쓰기에서 자동 줄맞춤일때
set sts=4          키보드에서 tab 키 누를때

7. 흐트러진 소스 정렬
=      
gg=G

8. 행 변호 보이기
:set nu

9. ^M 문자 제거
:%s/^M//g
ctrl+v Enter
10. 파일에서 달라진 부분 비교하기
vimdiff file1 file2
11. 헤더 파일 바로 읽어오기
Ctrl+wf
12. 자동완성 기능 이용하기
ctrl+n   또는 ctrl+p





 
VIM은 실행 시키게 되면  /root에 .vimrc 의 내용에 맞게 세팅하게 된다.
#cd /root
#vim .vimrc
아래는 .vimrc 파일의 내용이다.(파일 첨부)
ctags와 cscope를 단축키로 간단하게 사용할수 있게 만들어 놨다.
그리고 플러그인도 추가 하였다.
#mkdir -p /root/.vim/plugin

/root/.vim/plugin 폴더에 플러그인을 추가하면 된다.


플러그인은 http://www.vim.org/scripts 사이트에 가만 수많은 플로그인이 있다.
아래 내용중 빨간 글자는 단축키이다. 한번씩 써보면 쉽게 사용 할수 있을 것이다.


 

"<기본적인 VIM설정>

syn on "편집 파일 형태에 따라 구문의 색을 다르게 표시
set nu "숫자
set autoindent "자동 들여쓰기
set cindent "C프로그래밍 자동 들여쓰기
set smartindent "좀더 똑똑한 자동 들여쓰기
set wrap
set nowrapscan "검색할때 문서의 끝에서 다시 돌아가지 않음
set nobackup "자동 백업 기능 없애기
set visualbell "비쥬얼벨
set ruler "하단에 라인수/커서위치 표시하기
set title "창에 파일명 표시
set tabstop=4 "탭 사이즈 결정
set shiftwidth=4 "자동탭 크기 결정
set history=999 "히스토리 999라인까지 저장
set autowrite "버퍼를 옮길때나 외부 명령 실행할때 자동으로 저장시켜주는 기능
"set path=$PATH:/usr/src/kernels/linux-2.6.30




"<ctags설정>

set tags=./tags,../tags,../../tags,/usr/src/kernels/linux-2.6.30/tags,/usr/include/tags,/root/work/bsp/kernel/tags
"set tags=./tags,../tags,/XHYPER320_TKU/Bootloader/blob/tags
"set tags=/work/qtx/include/tags "tags파일 설정
if version >= 500 "버전 5.0이상일때만 적용
func! Sts()
let st = expand("<cword>")
exe "sts ".st
endfunc
nmap ,st :call Sts()<cr>

func! Tj()
let st = expand("<cword>")
exe "tj ".st
endfunc
nmap ,tj :call Tj()<cr>
endif




if 0
" if 1로 하면 cscope 설정을 하게 된다.
"============cscope설정================

"s : Find this C symbol
"g : Find this definition
"d : Find functions called by this function
"c : Find functions calling this function
"t : Find assignments to
"e : Find this egrep pattern
"f : Find this file
"i : Find file #includeing this file

"cs find s start_kernel


"mkscope.sh 파일 내용 /usr/local/bin/mkcscope.sh 에 복사후 사용한다.
"#!/bin/sh
"rm -rf cscope.out cscope.files
"find . \( -name '*.c' -o -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.s' -o -name '*.S' \) -print > cscope.files
"cscope -i cscope.files


set csprg=/usr/bin/cscope


"Check cscope for definition of a symbol before checking ctags:
"set to 1, if you want the reverse searh order.
set csto=1
set cst
set nocsverb

if filereadable("./cscope.out")
cs add cscope.out
else
cs add /usr/src/kernels/linux-2.6.30/cscope.out
endif
set csverb

func! Css()
let css = expand("<cword>")
new
exe "cs find s ".css
if getline(1) == " "
exe 'q!"
endif
endfunc
nmap ,css : call Css()<cr>

func! Csc()
let csc = expand("<cword>")
new
exe "cs find c ".csc
if getline(1) == " "
exe 'q!"
endif
endfunc
nmap ,csc : call Csc()<cr>


func! Csd()
let csd = expand("<cword>")
new
exe "cs find d ".csd
if getline(1) == " "
exe 'q!"
endif
endfunc
nmap ,csd : call Csd()<cr>

func! Csg()
let csg = expand("<cword>")
new
exe "cs find g ".csg
if getline(1) == " "
exe 'q!"
endif
endfunc
nmap ,csg : call Csg()<cr>
endif



"<man 설정>
func! Man()
let sm = expand("<cword>")
exe "!man -S 2:3:4:5:6:7:8:9:tcl:n:l:p:o ".sm
endfunc
nmap ,ma :call Man()<cr><cr>


"<KEY MAPPING>
map <F1> v]}zf "폴딩
map <F2> zo "언폴딩
map <F3> <C-W><C-W> "화면이동
map <F4> :Tlist<cr><C-W><C-W>   "tagList
map <F5> :!./a.out<cr>
map <F7> :w!<cr>:!gcc %<cr>
map <F8> :MiniBufExplorer<cr>
map <F11> :set nu<cr> :set autoindent<cr> :set nocindent<cr> :set smartindent<cr>
map <F12> :set nonu<cr> :set noautoindent<cr> :set nocindent<cr> :set nosmartindent<cr>
map ,ch :s/old/new/gc

map ,tags :set tags=/usr/src/kernels/linux-2.6.30/tags,/usr/include/tags,/root/work/bsp/kernel/tags

"<파일 버퍼간 이동> 여러 파일 관리 할때 유용하다. ex) vim a.c b.c c.c 로 열었을때
map ,1 :b!1<CR>
map ,2 :b!2<CR>
map ,3 :b!3<CR>
map ,4 :b!4<CR>
map ,5 :b!5<CR>
map ,6 :b!6<CR>
map ,7 :b!7<CR>
map ,8 :b!8<CR>
map ,9 :b!9<CR>
map ,0 :b!10<CR>
map ,w :bw<CR>  "현재 파일 버퍼 닫음


"<^M문자 제거>
"set fileformat=dos
"
"
"
"
"
" 문자열 치환하기
"s/old/new/g
":%s/
//g   @ ctrl+v, Enter
"
"


참고 : 유닉스.리눅스 프로그래밍 필수 유틸리티 (한빛미니어) 백창우