Skip to content

Instantly share code, notes, and snippets.

@onmoving
Last active January 3, 2019 02:36
Show Gist options
  • Save onmoving/4544537 to your computer and use it in GitHub Desktop.
Save onmoving/4544537 to your computer and use it in GitHub Desktop.
"======================================================================
"Override setting according to file extention
"======================================================================
filetype on
filetype plugin indent on
au FileType perl,ruby,clojure,objc,python,java,c,cpp,cs,html,css,php,php4,js,javascript,css,jsp,sh set nu
"======================================================================
"vim-ruby Setting
"======================================================================
compiler ruby
au FileType ruby set tabstop=2
au FileType ruby set shiftwidth=2
au FileType ruby set softtabstop=2
au FileType ruby map <F2> : !ruby %
"======================================================================
"vimClojure Setting
"======================================================================
let clj_highlight_builtins = 1
let clj_highlight_contrib = 1
let clj_paren_rainbow = 1
let clj_want_gorilla = 1
let vimclojure#NailgunClient="/home/khmirage/Util/clojure/vimclojure/ng"
"======================================================================
"Key Mapping Setting
"======================================================================
"Whitespace Line Insert Mapping
"======================================================================
map .j o
map .k O
map .ti i w
"======================================================================
"To HTML
"======================================================================
map .th : TOhtml<CR>
"======================================================================
"Shell Script Running
"======================================================================
au FileType sh map <F2> : !chmod +x %
au FileType sh map <F3> : !./%
"======================================================================
"C Compiler Mapping
"======================================================================
au FileType c set tabstop=4
au FileType c set noexpandtab
au FileType c set shiftwidth=4
au FileType c set softtabstop=4
au FileType c map <F2> : !gcc -W -Wall -o %< %
au FileType c map <F3> : !./%<
"======================================================================
"C++ Compiler Mapping
"======================================================================
au FileType cpp set tabstop=4
au FileType cpp set noexpandtab
au FileType cpp set shiftwidth=4
au FileType cpp set softtabstop=4
au FileType cpp map <F2> : !g++ -W -Wall -o %< %
au FileType cpp map <F3> : !./%<
"======================================================================
"Mono Compiler Mapping
"======================================================================
au FileType cs map <F2> : !mcs %
au FileType cs map <F3> : !mono %<.exe
"======================================================================
"Python Mapping
"======================================================================
au FileType python map <F2> : !python %
au FileType python set tabstop=8
au FileType python set shiftwidth=4
au FileType python set softtabstop=4
"======================================================================
"Javascript Mapping
"======================================================================
au FileType javascript map <F2> : !node %
au FileType javascript set tabstop=2
au FileType javascript set expandtab
au FileType javascript set shiftwidth=2
au FileType javascript set softtabstop=2
"======================================================================
"Java Compiler Mapping
"======================================================================
au FileType java map <F2> : !javac %
au FileType java map <F3> : !java %<
au FileType java map <F4> : !appletviewer %<
"======================================================================
"JSP -> Firefox Launch Mapping
"======================================================================
au FileType jsp map <F2> : !firefox http://localhost:8180/lecture/%
"======================================================================
"Objective C Compiler Mapping
"======================================================================
au FileType objc map <F2> : !gcc -Wall -o %< % -lobjc
au FileType objc map <F3> : !./%<
"======================================================================
"Perl excute Mapping
"======================================================================
au FileType perl map <F2> : !perl -w %
"======================================================================
"cscope settings
"======================================================================
set csprg=/usr/bin/cscope
set nocsverb
cs add /home/jin/jin.study/vim/linux-3.7.1/cscope.out
set csverb
set csto=0
set cst

##cscope 사용하기

mkcscope.sh 작성

#!/bin/sh
rm -rf cscope.file 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

mkcscope.sh 실행

$ chmod 755 mkcscope.sh
$ mv mkcscope.sh /usr/local/bin

cscope.out 생성이 완료되면 +d로 빠져나옴

~/.vimrc에 csope를 사용하기 위한 설정을 함

"======================================================================
"cscope settings
"======================================================================
set csprg=/usr/bin/cscope
set nocsverb
cs add /home/jin/jin.study/vim/linux-3.7.1/cscope.out
set csverb
set csto=0
set cst 

ctags 사용하기

ctags: 프로그램 소스코드에서 태그(전역변수 선언, 함수 정의, 매크로 선언)들의 데이터베이스(tags 파일)를 생성하는 유닉스 명령이다.

ctags의 설치 여부 확인

$ ctags --help

설치 되지 않았다면 설치

http://ctags.sourceforge.net

http://rpm.pbone.net

태그 데이터베이스 만들기

$ ctags -R

위 명령을 수행하면 tags 파일이 만들어짐

.vimrc에 설정할 내용

:set tags=./tags,other/path/tags,another/path/tags

예)

:set tags=./tags,../tags,../../tags,/usr/include/tags,/usr/local/include/tags

태그명령 요약

  • :ta [tag] or +], :ts[tag] or :tj [tag]: 스택을 사용하여 [tag]로 점프
  • :po or +t: 스택의 내용을 pop하고 그 위치로 점프
  • :sts [tag], :stj [tag]: 창을 수평 분할 하여 새로 생성된 창에 표시
  • :tn: tj, ts의 다음 태그로 점프
  • :tp: tj, ts의 이전 태그로 점프
  • 🇹🇷 tj, ts의 처음 태그로 점프
  • :tl: tj, ts의 마지막 태그로 점프
  • :pts [tag]: 창을 수평 분할 하여 새로 생성된 창에 표시. 커서는 현재 위치에 유지함.
  • :ptj [tag]: 미리보기 윈도우에 태그가 정의된 형식을 보여줌.
  • :ptn: ptj, pts의 다음 태그로 점프
  • :ptp: ptj, pts의 이전 태그로 점프
  • :ptr: ptj, pts의 처음 태그로 점프
  • :ptl: ptj, pts의 마지막 태그로 점프

태그로 바로 이동하여 vim 실행 하기

	$ vi -t start_kernel

####편리하게 사용하기 위한 내장 스크립트 작성

아래 내용을 .vimrc에 추가한다.

if version >= 500
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

###함수의 원형 간단 확인 단축키

표준모드에서 [i 키를 누르면 화면 하단에 함수의 원형을 확인할 수 있다.

###지역변수의 선언 부분으로 이동 단축키

표준모드에서 찾고자 하는 지역변수 위에 커서를 위치한 상태에서 gd 키를 입력하면 지역변수가 선언된 위치로 이동한다.

"======================================================================
"gVim settings
"======================================================================
"settings for fonts
if has("gui_running")
set guifont=나눔고딕코딩\ 11
set guifont=monospace\ 10
set guifont=Monaco\ 11
endif
"======================================================================
"Insert here the contents of common.vimrc
"======================================================================
"======================================================================
"WJ Choi <[email protected]>
"======================================================================
"status line in the bottom
set laststatus=2
set statusline=%<%F%h%m%r%h%w%y\ %{strftime(\"%Y/%m/%d-%H:%M\")}%=\ col:%c%V\ ascii:%b\ pos:%o\ lin:%l\,%L\ %P
"use vim's own function, no interoperability with vim
set nocp
"history of commands
set hi=1000
"use backspace
set bs=indent,eol,start
"show position of cursor
set ru
"position start of the line after move actions
set sol
"use user parenthesis finding
set mps+=<:>
"highlight searched word
set hls
"use smart uppercase/lower case
set scs
"open UTF-8, euc-kr hangul text file
if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
set encoding=utf-8
set fileencodings=utf-8,cp949
endif
"show line-number
set nu
"tab settings
set ts=4 "tabstop (size of tab)
set sts=4 "softtabstop (length of tab)
set sw=4 "shiftwidth (width of indentation tab)
"use Syntax Highlighting accroding to file type
syntax on
"indentation
set smartindent
set ai "autoindent
"c language indentation
set cindent
"setting for indentation when paste. toggle by using <Ins> key between paste mode and nopaste mode
set pastetoggle=<Ins>
"vim Colorscheme
set bg=dark
"color desert
"color motus
"======================================================================
"settings for folding
"======================================================================
"determine level of folding when reopen the file
"default is 0 and fold all
set fdl=1
"count of columns that shows the folding range
set fdc=1
"notify matching opening parenthesis as closing parenthesis is typed
set showmatch
"======================================================================
"gVim settings
"======================================================================
"settings for fonts
if has("gui_running")
" set guifont=DejaVu\ Sans\ Mono\ 11
" set guifont=monospace\ 11
" set guifont=DejaVu\ LGC\ Sans\ Mono\ 12
set guifont=Monaco\ 10
set guifont=나눔고딕코딩\ 11
set guifont=monospace\ 10
" set gfn=Fixedsys:h12:cHANGEUL
" set gfn=굴림체:h9:cHANGEUL
endif
"setting for mouse
if has("gui_running")
"mouse model
set mousem=popup
endif
"not display scrollbar
if has("gui_running")
set go-=l
set go-=L
set go-=r
set go-=R
set go-=b
endif
"hide toolbar
if has("gui_running")
set go-=T
endif
"set size of gVim
if has("gui_running")
set lines=50
set co=120
endif
"======================================================================
"Insert here the contents of common.vimrc
"======================================================================
"======================================================================
"Insert here the contents of common.vimrc
"======================================================================

##vim의 여러가지 기능

###파일탐색

  • explorer 플러그인 실행: :20vs . vs는 vertical split이고 20은 창의 크기를 의미한다.
  • NERDTree 플러그인 실행: :NERDTree

###빠른 괄호 이동

여는 괄호 혹은 닫는 괄호에 커서를 놓고 % 키를 누르면 괄호 쌍으로 이동한다.

###빠른 탐색

찾는 단어에 커서를 두고 * 키나 # 키를 누르면 커서에 위치한 단어와 같은 문자열을 찾아서 다음위치 혹은 이전 위치로 이동한다. 이는 /[찾으려는 문자열] 혹은 ?[찾으려는 문자열] 명령과 같은 기능을 한다.

###블록 선택

  • '{}' 영역을 블록 선택: 표준모드에서 {위에 커서를 위치 시킨 후 v]} 키를 누른다.

###폴딩 기능 이용하기

  • 블록을 폴딩하기: 블록으로 영역을 선택을 한 후 zf 키를 누른다.
  • {} 블록을 폴딩하기: 표준모드에서 {위에 커서를 위치 시킨 후 v]}zf 키를 누른다. v]}은 괄호 쌍에 대한 블록을 지정하는 기능이다.
  • 언폴딩: 접힌 부분에서 ?? 혹은 zo를 누른다.

~/.vimrc에서 단축키 설정

"folding
map <F1> v]}zf
"unfolding
map <F2> zo

###함수의 원형 간단 확인 단축키

see ctags 사용하기: ctags.md

###지역변수의 선언 부분으로 이동 단축키

see ctags 사용하기: ctags.md

##(여러 행에) 탭을 일괄적으로 삽입 제거 하기

[N]>>

또는

[N]<<

블록을 지정하고 > 혹은 < 키를 쳐도 위와 같게 작동한다.

##탭 사이즈 조정하기

  • ts(tabstop): 문서 내의 '\t' 문자를 몇 칸으로 보여줄지를 정한다--number of spaces a <Tab> in the text stands for

    :set ts=8

  • sw(shiftwidth): >> 혹은 << 키로 탭 사이즈를 조절할 때, 자동 줄 맞추기시 들여쓰기 시 사용되는 칸 수이다--number of spaces used for each step of (auto)indent

    :set sw=2

  • sts(softtabstop): Tab 키를 눌렀을 때 표시되는 간격을 정한다--if non-zero, number of spaces to insert for a

    :set sts=0

  • et(expandtab): 탭키를 눌렀을 때 스페이스로 확장할 지 아니면 '\t'로 확장할 지를 결정--expand to spaces in Insert mode

    :set noet :set et

###소스 정렬 하기

###블록의 소스 정렬

블록을 지정한 후 = 키를 누른다.

###블록없이 소스 전체를 정렬

gg=G

###^M 문자: Window 시스템의 개행문자

윈도우 시스템의 개행문자(\r\n)를 Unix형식(\n)으로 변경하기

####fileformat변경

:set fileformat==dos

####치환명령

:%s/^M//g

여기서 ^M을 직접입력하는 것이 아니라 <Ctrl>+v<Enter>키를 누른다.

참고:

  • ^[ : <Ctrl>+v<Esc>
  • ^[[11~ : <Ctrl>+v<F1>
  • ^[[12~ : <Ctrl>+v<F2>

###현재 커서 위치의 단어의 삽입

####명령창

  • <Ctrl>+R <Ctrl>+W: 현재 커서 위치의 단어를 명령창에 넣는다. 도움말 :help c_CTRL-R
  • nmap: 단축키 설정 예: nmap ,st :call Sts()<cr>

####내장 스크립트 함수

  • <cword>: 현재 커서 위치의 단어

  • expand("<cword>"): ?

###파일비교

명령창에서

$ vimdiff copying copying.org

혹은 vim내에서 쉘명령 수행을 통해 meld등을 수행한다.

###커서 위치의 파일 열기

우선 vim이 파일을 열수 있는 폴더의 위치를 path 변수에 지정해야한다. .vimrc에 지정한다.

set path=/usr/local/lib/node\_modules:/usr/include:~/projects/example1

vim 내에서 열려는 파일명이 있는 곳에 커서를 위치시킨 후 <Ctrl>+wf를 입력하면 수평 분할이 되면서 파일이 열린다.

###여러가지 변수 값을 출력하기

변수명 뒤에 ? 기호를 붙여서 내용을 출력할 수 있다.

:set filetype?
:set path?
:set tabstop?

###단어자동완성

<Ctrl>+p: 커서의 위치에서 위로 검색, 코딩을 할 때 유요함. <Ctrl>+n: 커서의 위치에서 아래로 검색

###정규표현식의 매칭문자 사용

aaa=YES
aaa=NO

위의 문자열을 가지고 아래와 같게 되도록 하고자 할 때 치환명령을 사용할 수 있다.

ccc=YES, ddd=YES
ccc=NO, ddd=NO

치환명령

:%s/aaa=\(YES\|NO\)/ccc=\1, ddd=\1/gc

위 치환명령은 정규식의 그룹기능을 사용하는 한 예이다.

#vim plugin-내장스크립트

vim 플러그인은 확장자가 *.vim으로 끝난다. vim이 실행될 때 전역 플러그인 디렉토리에 있는 내장 스크립트를 읽어 들인 후, 사용자 홈 디렉토리의 플러그인 디렉토리를 순서대로 읽어서 플러그인을 기능을 준비한다.

  • 전역 플러그인 디렉토리: /usr/share/vim/vim[version]/plugin(페도라)
  • 사용자 홈 디렉토리의 플러그인 디렉토리: ~/.vim/plugin

#참고문헌

  • 유닉스 리눅스 프로그래밍 필수 유틸리티(개정판), 백창우, 한빛미디어
vim 에디터 사용하기
==============
## vim의 여러가지 기능
###파일비교
$ vimdiff copying copying.org
혹은 vim내에서 쉘명령 수행을 통해 meld등을 수행한다.
###커서 위치의 파일 열기
우선 `vim`이 파일을 열수 있는 폴더의 위치를 `path` 변수에 지정해야한다.
`.vimrc`에 지정해 준다.
set
`vim` 내에서 열려는 파일명이 있는 곳에 커서를 위치시킨 후 <Ctrl>+wf를 입력하면 수평 분할이 되면서 파일이 열린다.

vim 에디터 사용하기 ==============

## vim의 여러가지 기능 ###파일비교

명령창에서

$ vimdiff copying copying.org

혹은 vim내에서 쉘명령 수행을 통해 meld등을 수행한다.

###커서 위치의 파일 열기

우선 `vim`이 파일을 열수 있는 폴더의 위치를 `path` 변수에 지정해야한다. `.vimrc`에 지정해 준다.

set path=/usr/local/lib/node\_modules:/usr/include:~/projects/example1

`vim` 내에서 열려는 파일명이 있는 곳에 커서를 위치시킨 후 <Ctrl>+wf를 입력하면 수평 분할이 되면서 파일이 열린다.

##.vimrc 예

TODO: 예제로 사용할 .vimrc 설정 내용 정리

샘플 .vimrc

"===========default setting
set cindent
set smartindent
set autoindent
set nowrap
set ff=unix
set bg=dark
set ruler
"specify folders to be searched
set path=/root/work/include,/usr/include,/usr/local/include,/usr/src/include

"===========key mapping
"folding
map <F1> v]}zf
"unfolding
map <F2> zo
"Taglist plugin
map <F3> :Tlist<cr><C-W><C-W>
"Buffer Explorer plugin
map <F4> :BufExplorer<cr>
map <PageUp> <C-U><C-U>
map <PageDown> <C-D><C-D>

"===========indent on/off
map ,noi :set noai<CR>:set nocindent<CR>:set nosmartindent<CR>
map ,sei :set ai<CR>:set cindent<CR>:set smartindent<CR>

"===========moving between file buffers
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 ,x :bn!<CR>       "next buffer
map ,z :bp!<CR>       "previous buffer
map ,w :bw<CR>        "close current file buffer

"===========ctags setting
set tags=./tags,../tags,../../tags,/usr/include/tags,/usr/local/include/tags

if version >= 500
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

"===========cscope setting

"===========man page setting

"===========executing shell script

##janus

janus는 Vim, GVim, MacVim에 대한 플러그인, 매핑 설정에 대한 배포버전이다.

url: https://github.com/carlhuda/janus

###설치방법

prerequisites

$ sudo apt-get install ack ctags git ruby rake curl

installing

$ curl -Lo- https://bit.ly/janus-bootstrap | bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment