Vim 내장 스크립트 함수 및 데이터 구조에 대한 참조 가이드입니다.
데이터 구조
딕셔너리 (Dictionaries)
let colors = {
\ "apple": "red",
\ "banana": "yellow"
}
echo colors["apple"]
echo get(colors, "apple") " 키가 없을 때 에러 방지
call remove(colors, "apple")
" 검사 및 확인
if has_key(dict, 'foo')
if empty(dict)
keys(dict)
len(dict)
max(dict)
min(dict)
count(dict, 'x')
string(dict)
map(dict, 'v:val . "!"')
extend(s:fruits, { "cherry": "red" })
for key in keys(mydict)
echo key . ': ' . mydict[key]
endfor
리스트 (Lists)
let mylist = [1, "two", 3, "four"]
let first = mylist[0]
let last = mylist[-1]
" 기본값을 지정한 안전한 조회
let second = get(mylist, 1)
let fallback = get(mylist, 10, "NONE")
버퍼 및 커서
버퍼 검사
line('.') " 현재 줄 번호
col('.') " 현재 열 번호
col('$') " 현재 줄의 마지막 열 번호
getline('.') " 현재 줄의 문자열 내용
getline(1) " 1번 줄의 문자열 내용
getline(1, 5) " 1~5번 줄을 문자열 리스트로 가져오기
search('^$') " 다음 빈 줄 번호
search('^$', 'n') " 커서 이동 없이 다음 빈 줄 찾기
getcurpos() " [bufnum, lnum, col, off, curswant]
getpos('.') " [bufnum, lnum, col, off]
nextnonblank(1) " 1번 줄 이후의 첫 비어있지 않은 줄 번호
prevnonblank(10)
마크 (Marks)
getpos("'a") " 마크 'a'의 위치
setpos("'a", [0, 10, 5, 0])
getpos("'<") " 비주얼 선택 시작 위치
getpos("'>") " 비주얼 선택 끝 위치
커서 위치 지정
call cursor(line, col)
call cursor(line, col, off, curswant)
파일명 확장
expand('<cword>') " 커서 아래의 단어
expand('<cfile>') " 커서 아래의 파일 경로
expand('%') " 현재 파일명
" 수식자:
" :p 전체 경로 (/Users/name/project/file.txt)
" :h 디렉터리 (Head) (/Users/name/project)
" :t 파일명 (Tail) (file.txt)
" :r 확장자 없는 기본 파일명 (file)
" :e 확장자 (txt)
" 예시: expand('%:p:h')
파일 및 경로
fnameescape('string')
fnamemodify('main.c', ':p:h')
fnamemodify(fname, ':e')
filereadable(fname)
getfsize('file.txt')
getcwd()
globpath(&rtp, "plugin/commentary.vim")
수학 및 타입
수학 연산
fmod(9, 2) " 나머지: 1.0
abs(-0.5) " 절댓값
sqrt(9) " 제곱근
trunc(1.84) " 1.0
floor(1.84) " 1.0
ceil(1.84) " 2.0
float2nr(3.14) " 실수를 정수로 변환: 3
타입 변환
str2float('0.2')
str2nr('240')
str2nr('ff', 16) " 16진수 파싱
string(0.3) " 문자열로 변환
타입 검사
type(var) == type(0) " 숫자
type(var) == type("") " 문자열
type(var) == type(function("tr")) " 함수 참조
type(var) == type([]) " 리스트
type(var) == type({}) " 딕셔너리
type(var) == type(0.0) " 실수 (Float)
문자열 및 정규 표현식
문자열 조작
if a =~ '\s*'
let clean = substitute(str, '\s\+', ' ', 'g')
strpart("abcdef", 3, 2) " == "de" (부분 문자열: 오프셋, 길이)
strpart("abcdef", 3) " == "def"
stridx("abcdef", "e") " == 4 (부분 문자열)
strridx("abcdef", "d") " 역방향 인덱스 검색
matchstr('testing', 'test') " == 'test' (일치하는 텍스트)
match('testing', 'test') " == 0 (일치 시작 인덱스)
matchend('testing', 'test') " == 4 (일치 종료 인덱스)
match('testing', '\ctest') " 대소문자 무시 (\c)
split(str, '\zs') " 문자열을 문자 배열로 분할
strlen(str) " 바이트 길이
strchars(str) " 글자 수 (다국어/멀티바이트 지원)
strdisplaywidth(str) " 화면 표시 열 너비
toupper(str)
tolower(str)
tr('foo', '_-', ' ') " 문자 치환
비교 연산
if name ==# 'John' " 대소문자 구분 일치
if name ==? 'John' " 대소문자 구분 없는 일치
if name == 'John' " 'ignorecase' 옵션 설정에 따름
if "hello" =~ '.*' " 패턴과 일치
if "hello" !~ '.*' " 패턴과 일치하지 않음
명령 및 실행
명령 실행
normal! ddahello
execute 'normal! ' . a:keys
wincmd J " 창 이동 명령
셸 명령
let output = system('ls ' . shellescape(expand('%:h')))
레지스터
getreg('*') " 클립보드 내용 가져오기
getregtype('*') " 레지스터 종류: 'v' (문자), 'V' (줄), '<ctrl-v>' (블록)
setreg('*', 'new text')