• Stars
    star
    110
  • Rank 305,693 (Top 7 %)
  • Language
  • License
    MIT License
  • Created almost 7 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

vimscript cheatsheet

vimscript-cheatsheet

vimscript cheatsheet

:help script - If you want to know more about vimscript

Hello, World!

echo 'Hello, World!'
  • echomsg is same with echo. but echomsg saves message history.
  • You can see the message history with :message.
echomsg 'Hello, Vimscript!'

variables: Number, Float, String

" Number is integer
let number = 42     " Defines Number
let number += 1     " 43

echomsg number      " print 43

unlet number        " Undefines number

echo number         " error
let pi = 3.141592

" parseInt
let pi_int = float2nr(pi)   " 3

" toString
let pi_str = string(pi)     " '3.141592'
let name = 'John'               " 'John'
let size = strchars(name)       " 4

" concat string
let full_name = name . ' Grib'  " 'John Grib'

" substring
echo strcharpart(full_name, 5, 1)   " 'G'

namespaces

  • g: - Global.

  • l: - Local to a function.

  • s: - Local to a script file.

  • a: - Function argument (only inside a function).

  • v: - Global, predefined by Vim.

  • b: - Local to the current buffer.

  • w: - Local to the current window.

  • t: - Local to the current tab page.

let g:number = 42       " global
let s:number = 67       " script scope

function s:foo(number)  " function : script scope

    echo g:number . ' is global.'
    echo a:number . ' is argument.'

    let l:number = 23

    echo l:number . ' is local.'

endfunction

call s:foo(s:number)
    " 42 is global.
    " 67 is argument.
    " 23 is local.

List

  • List basic
let list = []
let list = ['one', 'two', 'three', 'four']

echo list[0]             " 'one'
echo list[-1]            " 'four'
echo list[9]             " error

echo get(list, 0)        " 'one'
echo get(list, -1)       " 'four'
echo get(list, 9, 'ten') " 'ten'

let size = len(s:list)   " size is 4

echo list[:1]       " ['one', 'two']
echo list[:2]       " ['one', 'two', 'three']

echo list[1:]       " ['two', 'three', 'four']
echo list[2:]       " ['three', 'four']

echo list[0:1]      " ['one', 'two']
echo list[1:2]      " ['two', 'three']
echo list[1:-1]     " ['two', 'three', 'four']
echo list[1:-2]     " ['two', 'three']

let list += ['five', 'six']                 " ['one', 'two', 'three', 'four', 'five', 'six']
let long_list = list + ['seven', 'eight']

call add(long_list, 'nine')

let copied_list = copy(list)
let recursively_copied_list = deepcopy(list)

let list[3:4] = [4, 5]      " ['one', 'two', 'three', 4, 5, 'six']
  • List unpack
let list = [1, 2, 3]

let [var1, var2, var3] = list

echo var1   " 1
echo var2   " 2
echo var3   " 3
let list = [1, 2, 3, 4]

let [var1, var2; var3] = list   " use semicolon

echo var1   " 1
echo var2   " 2
echo var3   " [3, 4]
  • List functions
" length
echo len([1, 2, 3])     " 3

" check empty
echo empty([1, 2, 3])   " 0
echo empty([])          " 1

" remove
let list = [1, 2, 3]
call remove(list, 1)                " [1, 3]
let removed_item = remove(list, 0)  " removed_item is 1, list is [3]

" filter
let list = [1, 2, 3]
call filter(list, {idx, val -> val > 1})    " use lambda
echo list   " [2, 3]

" map
let list = [1, 2, 3]
call map(list, {idx, val -> val * 10})
echo list   " [10, 20, 30]

" sort, reverse, uniq, max, min
call sort(list)
call reverse(list)
call uniq(list)
let max = max(list)
let min = min(list)

" count, index
let list = [1, 2, 3, 4, 5, 5]
echo count(list, 5)             " 2
echo index(list, 2)             " 1

" split, join
let list = split('1 2 3', '\s') " ['1', '2', '3']
let str = join(list, '-')       " '1-2-3'

Dictionary

  • add new entry
let obj = { 'name': 'John', 'number': 24 }
let obj['like'] = ['vim', 'game', 'orange']
let obj['address'] = 'Seoul, Korea'
let obj.test = 1
  • delete entry
let obj = { 'name': 'John', 'number': 24, 'test': 1 }
let removed = remove(obj, 'name')

echo removed    " 'John'

unlet obj.number
unlet obj['test']
  • to List
for key in keys(mydict)
   echo key . ': ' . mydict[key]
endfor

for key in sort(keys(mydict))
   echo key . ': ' . mydict[key]
endfor

for v in values(mydict)
   echo "value: " . v
endfor

for [key, value] in items(mydict)
   echo key . ': ' . value
endfor
  • dictionary functions
echo has_key(dict, 'foo')
echo empty(dict)

flow control

  • if
if 1
    echo 'true'
endif

if 0
    echo 'impossibe'
endif

let name = 'foo'

if name == 'test'
    echo 'test name'
elseif name == 'foo'    " else if (x), elseif (o)
    echo name
endif
  • for
for item in [1, 2, 3]
    echo item
endif

for item in [1, 2, 3]
    if item < 2
        continue
    else
        break
    endif
endfor
  • while
let sum = 0
while sum < 100
    let sum += 1
    call do_something()
endwhile

function

  • The name must be made of alphanumeric characters and _
  • The name must start with a capital or s:
    • :b or :g is not allowed.
function Foo(value)     " global function
    echo a:value
endfunction

function s:foo(value)   " script private function
    echo a:value
endfunction
  • function! : override a function with the same name.
function Foo(value)
    echo a:value
endfunction

function! Foo(value)
    echo a:value * 10
endfunction

call Foo(7)     " 70
  • Dictionary function (method) with dict keyword
let obj = { 'msg': 'hello' }

function obj.getMsg() dict
    return self.msg
endfunction

let msg = obj.getMsg()
echo msg    " 'hello'
function GetMsg() dict
    return self.msg
endfunction

let obj1 = { 'msg': 'hello', 'getMsg': funcref('GetMsg') }
let obj2 = { 'msg': 'hi', 'getMsg': function('GetMsg') }

echo obj1.getMsg()  " 'hello'
echo obj2.getMsg()  " 'hi'
  • apply with function()
function Add(num1, num2)
    return a:num1 + num2
endfunction

let Add3 = function('Add', [3])
echo Add3(7)    " 10
echo Add3(2)    " 5
  • arguments

:help function-argument

function Test(...)
    echo 'number of args : ' . a:0
    echo 'first arg : ' . a:1
    echo 'second arg : ' . a:2

    echo ''

    for s in a:000
        echon ' ' . s
    endfor
endfunction

call Test('dog', 'cat')
" number of args : 2
" first arg : dog
" second arg : cat
" dog cat
  • public static function
    • naming rule : {directory_name}#{file_name}#{function_name}
- .vim
    β”• plugin
        β”• test-vim-plugin           # project root
            β”• syntax
                ...
            β”• plugin
                ...
            β”• autoload
                β”• TestVimPlugin
                    β”• screen.vim    # sample file
# ~/.vim/plugin/test-vim-plugin/autoload/TestVimPlugin/screen.vim

function! TestVimPlugin#screen#getScreenNumber()
    return 1
endfunction

More Repositories

1

vim-game-code-break

Block-breaking game in vim 8.0
Vim Script
1,633
star
2

simple_vim_guide

simple vim guide
505
star
3

vim-game-snake

Vim Game : Snake
Vim Script
132
star
4

vim-f-hangul

vimμ—μ„œ f둜 ν•œκΈ€μ„ κ²€μƒ‰ν•˜μž
Vim Script
92
star
5

dotfiles

My dotfiles.
Shell
91
star
6

study-objects

쑰영호 λ‹˜μ˜ μ±… [였브젝트]λ₯Ό ν•™μŠ΅ν•˜λŠ” μ €μž₯μ†Œ.
Java
85
star
7

johngrib.github.io

my wiki
HTML
72
star
8

johngrib-jekyll-skeleton

my github.io jekyll blog skeleton
HTML
70
star
9

fav-dir

Jump to your favorite directories in bash using fzf.
Shell
56
star
10

vim-git-msg-wheel

Displays recent commit messages as an auto-complete list.
Vim Script
52
star
11

hammerspoon-config

my hammerspoon config
Lua
45
star
12

droller

A command-line tool that randomly recommends one of the URIs you haven't read forever.
Shell
20
star
13

vim-mac-dictionary

Vim Script
17
star
14

html-presentation

HTML
12
star
15

AhkVimLike

Autohotkey VIM like navigation (for ms windows)
AutoHotkey
8
star
16

example-junit5

Java
8
star
17

study-springrunner-c101

SpringRunner c101 μ›Œν¬μƒ΅
Java
7
star
18

vim-aheui

Vim Script
7
star
19

johngrib-wiki-lsp

An implementation of Language Server Protocol for JohnGrib's wiki.
JavaScript
5
star
20

cljstman

Free HTTP client for Clojure users
Clojure
3
star
21

random-git-flight-rules

You can get one of the git flight rules at random.
Shell
3
star
22

footloosegrid

Simple javascript grid.
JavaScript
3
star
23

log

μ•„λ¬΄κ±°λ‚˜ κΈ°λ‘ν•œλ‹€
3
star
24

hammerspoon_winmove

Lua
3
star
25

study-realworld-http

μ±… "λ¦¬μ–Όμ›”λ“œ HTTP"의 예제λ₯Ό κ³΅λΆ€ν•˜λŠ” μ €μž₯μ†Œ
Go
3
star
26

wwlunch

command line μ—μ„œ μš°μ•„ν•œ ν˜•μ œλ“€ 점심 메뉴λ₯Ό μ‘°νšŒν•˜μž!
JavaScript
3
star
27

study

Python
2
star
28

my-text-editor

C
2
star
29

kata

Practice programming
JavaScript
1
star
30

jenkinsTest

JavaScript
1
star
31

leetcode

My leetcode solutions.
Go
1
star
32

hammerspoon_caffein

Lua
1
star
33

LATEX-mathjax-cheatsheet

1
star
34

git-examples

1
star
35

kotlin-playground

Kotlin
1
star
36

hosu

Vim Script
1
star
37

archived-algorithm-study

Java
1
star
38

ruberdriver

Simple web browser automation tool
Java
1
star
39

brainfuck-clojure

Clojure
1
star
40

gitsis

the git helper
Go
1
star
41

farm-timer

Swift
1
star
42

clojure-study-on-java

Java
1
star
43

hammerspoon_clipboard

Lua
1
star
44

example-clojure-sqlite

Examples for clojure + sqlite
Clojure
1
star
45

study-learn-lacinia

https://github.com/learnuidev/learn-lacinia λ₯Ό κ³΅λΆ€ν•˜λŠ” μ €μž₯μ†Œ
Clojure
1
star
46

TIS-100-solutions

1
star
47

study-learn-c-the-hardway

μ±… 'κΉκΉν•˜κ²Œ λ°°μš°λŠ” C'λ₯Ό κ³΅λΆ€ν•˜λŠ” μ €μž₯μ†Œ
C
1
star
48

intellij-idea-playground

IntelliJ ν”ŒλŸ¬κ·ΈμΈμ„ μ‹€ν—˜ν•˜λ©° λ†‰λ‹ˆλ‹€.
Java
1
star
49

AhkClipboardReg

Autohotkey clipboard register
AutoHotkey
1
star
50

study-java-hashmap

Java
1
star
51

homebrew-johngrib

Ruby
1
star
52

clojure-aoc

Clojure
1
star
53

go-example-http-mongodb

Go
1
star
54

jcd

1
star
55

git-training-for-my-team

1
star
56

git-education

git ꡐ윑자료 예제
1
star
57

new-repository

1
star
58

study-golang

Go
1
star
59

think-bayes-study

JavaScript
1
star
60

ssedemo

Java
1
star