시작하기
모드와 기본 이동
Vim은 모달 편집기입니다. 모드마다 다른 목적이 있습니다. Esc는 다른 모드에서 노멀 모드로 돌아갑니다. hjkl 키는 손을 홈 로우에 두게 합니다. w/b는 단어 단위, 0/$는 줄 경계, gg/G는 파일 시작/끝, :N은 N번 줄로 이동합니다.
# Vim Modes
# Normal mode (Esc) - default, for navigation
# Insert mode (i) - for typing text
# Visual mode (v) - for selecting text
# Command mode (:) - for Ex commands
# Basic movement (Normal mode)
h # left
j # down
k # up
l # right
w # next word start
b # previous word start
0 # beginning of line
$ # end of line
gg # first line
G # last line
:42 # go to line 42종료와 저장
ZZ와 ZQ는 콜론까지 손을 뻗지 않아도 되는 가장 빠른 종료 방법입니다. :x는 파일이 변경된 경우에만 기록하므로(mtime 보존) 항상 쓰는 :wq와 다릅니다. :w!는 파일 시스템 권한이 허용하면 읽기 전용 플래그를 무시합니다. 수정된 버퍼가 여러 개일 때는 :qa!로 빠져나가세요.
:w # write (save) file
:w! # force write (override read-only)
:q # quit
:q! # force quit (discard changes)
:wq # write and quit
:x # write only if changed, then quit
ZZ # same as :x (write if changed, quit)
ZQ # quit without saving (like :q!)
:w newfile.txt # save as new file
:sav newfile.txt # save as new file and switch to it
:wa # write all buffers
:xa # write all changed buffers and exit
:qa! # force quit all (discard everything)도움말 시스템
Vim의 도움말은 방대하고 하이퍼링크로 연결됩니다. Ctrl-]는 태그(링크)를 따라가고 Ctrl-T는 돌아옵니다. 표기법: i_CTRL-N은 삽입 모드의 Ctrl-N, 'number'(따옴표 포함)는 옵션을 뜻합니다. :helpgrep은 모든 도움말 파일을 한 번에 검색 하며 :cn/:cp로 이동합니다. K는 커서 아래 키워드의 man 페이지를 엽니다('keywordprg'로 설정 가능).
:help # main help
:help subject # help on subject (e.g. :help insert)
:help i_CTRL-N # help on Ctrl-N in insert mode
:help 'number' # help on the 'number' option (quotes)
:helpgrep pattern # search all help files for pattern
:cn / :cp # next/prev helpgrep match
K # man page for word under cursor (Normal mode)
Ctrl-] # jump to tag under cursor (follow link)
Ctrl-T # jump back from tag (pop tag stack)
:helptags ~/.vim/doc # regenerate help tags for a doc directory단어와 문자 이동
소문자 모션(w/b/e)은 구두점을 단어 구분자로 취급해 코드에 유용합니다. 대문자 모션(W/B/E)은 공백만 구분자로 취급해 산문에 유용합니다. e/E는 다음 단어 끝으로, ge/gE는 이전 단어 끝으로 이동합니다(드물지만 유용).
# Word motions (punctuation-aware)
w # next word start
b # previous word start
e # end of next word
ge # end of previous word
# WORD motions (whitespace-only delimiters, uppercase)
W # next WORD start
B # previous WORD start
E # end of next WORD
gE # end of previous WORD
# Word boundaries:
# word: sequence of word-characters OR sequence of punctuation
# WORD: anything separated by whitespace
# Examples (cursor on 'a' in "foo.bar baz"):
# w -> 'bar' (punctuation splits words)
# W -> 'baz' (only whitespace splits WORDS)줄 이동
0과 $는 절대 줄 시작/끝(공백 포함)으로 갑니다. ^와 g_는 앞/뒤 공백을 건너뛰므로 보통 원하는 동작입니다. 'wrap'이 켜져 있으면 g0/g$는 화면 줄, 0/$는 논리 줄에서 동작합니다. |는 특정 열로 이동합니다.
0 # first column (absolute start)
^ # first non-blank character
$ # end of line (last character)
g_ # last non-blank character
g0 # start of screen line (with wrap)
g$ # end of screen line (with wrap)
gm # middle of screen line
gM # middle of text line
| # column N (e.g. 10| = column 10)
+ # first non-blank of next line
- # first non-blank of previous line
_ # first non-blank of current line [count down]파일과 화면 이동
H/M/L은 보이는 화면 안에서 이동합니다. Ctrl-d/u는 반 페이지 스크롤(전체 페이지 Ctrl-f/b보다 덜 혼란스러움). zz/zt/zb는 커서를 움직이지 않고 현재 줄을 화면 중앙/위/아래로 스크롤하므로 편집 중 문맥 유지에 매우 유용합니다. N%는 파일의 백분율 위치로 이동합니다.
gg # go to first line
G # go to last line
Ngg / NG # go to line N (e.g. 50gg or 50G)
:N # go to line N (Ex command)
H # top of screen (High)
M # middle of screen
L # bottom of screen (Low)
N% # jump to N% of the file
Ctrl-f # scroll forward (full page)
Ctrl-b # scroll backward (full page)
Ctrl-d # scroll down (half page)
Ctrl-u # scroll up (half page)
zz # scroll current line to middle
zt # scroll current line to top
zb # scroll current line to bottom텍스트 삽입
기본 삽입(i, I)
i/I는 커서 앞/줄 시작에 삽입합니다. a/A는 커서 뒤/줄 끝에 삽입합니다. gi는 마지막 삽입 위치(마크 '^)로 돌아가므로 Esc로 다른 일을 한 뒤 계속 입력할 때 편리합니다. gI는 앞 공백을 무시하고 절대 1열로 갑니다.
i # insert before cursor
I # insert at first non-blank (same as ^i)
gi # insert at last insert position (also sets mark)
gI # insert at column 1 (same as 0i)
a # insert after cursor
A # insert at end of line (same as $a)
# Examples:
# foo|bar + i -> foo|bar (cursor stays, type before)
# foo|bar + I -> |foobar (cursor at first non-blank)
# foo|bar + A -> foobar| (cursor at end)
# gi returns to where you last left insert mode —
# very useful after escaping to run a Normal-mode command.새 줄 열기(o, O)
o와 O는 커서 아래/위에 새 줄을 열고 삽입 모드로 들어갑니다. 줄을 추가하는 가장 빠른 방법이라 커서를 줄 끝에 둘 필요가 없습니다. 들여쓰기는 주변 줄에서 자동 복사됩니다('autoindent'로 제어).
o # open new line BELOW cursor, enter insert
O # open new line ABOVE cursor, enter insert
# Before (cursor on middle line):
# line one
# li|ne two
# line three
# After 'o':
# line one
# line two
# | <- new blank line, insert mode
# line three
# After 'O':
# line one
# | <- new blank line, insert mode
# line two
# line three변경 명령(c, C, s, S)
c{motion}은 모션 대상을 삭제하고 삽입 모드로 들어가는 가장 유연한 편집 명령입니다. C는 c$(줄 끝까지 변경), S는 cc(줄 전체 변경)입니다. s는 문자 하나를 삭제하고 삽입하며 3s처럼 횟수와 결합할 수 있습니다. ciw/caw는 텍스트 객체를 사용합니다(전용 섹션 참고).
cw / ce # change to end of word
cb # change backward to word start
c$ / C # change to end of line
cc / S # change entire line
c0 # change to beginning of line
c^ # change to first non-blank
ct, # change up to (but not including) next ','
cfx # change up to and including next 'x'
ciw # change inner word (no surrounding whitespace)
caw # change a word (with surrounding whitespace)
cap # change around paragraph
s # substitute: delete char under cursor, insert
S # substitute: delete entire line, insert (like cc)바꾸기 모드(r, R, gr)
r은 문자 하나를 바꾸고 즉시 노멀 모드로 돌아갑니다(모드 전환 없음). R은 입력이 문자를 덮어쓰는 바꾸기 모드로 들어갑니다. Backspace는 문자를 복원하며, 단순 삭제인 삽입 모드와 다릅니다. 탭 작업에는 gr/gR(가상 바꾸기)을 쓰세요. 탭 정렬을 유지하고 텍스트를 밀지 않습니다.
r{x} # replace single char under cursor with x
gr{x} # virtual replace (doesn't shift tabs)
R # enter Replace mode (overwrite until Esc)
gR # virtual Replace mode (tabs stay aligned)
3r{x} # replace 3 chars with x (single char only)
# In Replace mode, typing overwrites existing text
# but Backspace restores the original characters.
# Tabs are preserved with virtual replace (gr/gR).
# Examples:
# abc|def + rx -> abc|xef (cursor advances)
# abc|def + R -> enter Replace mode
# type 'XYZ' -> abcXYZ|f
# Backspace -> abcXY|def (original restored)특수 삽입
:r !cmd는 셸 명령 출력을 버퍼에 삽입합니다. 명령 출력, 날짜, 파일 목록 붙여넣기에 좋습니다. 삽입 모드에서 Ctrl-r 다음 레지스터 이름은 해당 레지스터를 삽입하고 Ctrl-r =은 Vimscript 표현식을 평가해 결과를 삽입합니다. Ctrl-k는 ASCII 문자 두 개를 입력해 다이그래프(특수 문자)를 삽입합니다.
:r file.txt # insert contents of file below cursor
:r !command # insert output of shell command
:0r !date # insert date at top of file
:r !ls -la # insert directory listing
# Insert with special registers
Ctrl-r = # expression register: evaluate, insert result
Ctrl-r " # insert unnamed register
Ctrl-r % # insert current filename
# Insert from current file path
:i! !pwd # insert pwd output (no autoindent)
# Non-ASCII / digraphs (in insert mode)
Ctrl-k {ch1}{ch2} # digraph: e.g. Ctrl-k e' produces é
:digraphs # list available digraphs