├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 이종립 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vimscript-cheatsheet 2 | vimscript cheatsheet 3 | 4 | `:help script` - If you want to know more about vimscript 5 | 6 | ## Hello, World! 7 | 8 | ```viml 9 | echo 'Hello, World!' 10 | ``` 11 | 12 | * `echomsg` is same with `echo`. but `echomsg` saves message history. 13 | * You can see the message history with `:message`. 14 | 15 | ```viml 16 | echomsg 'Hello, Vimscript!' 17 | ``` 18 | 19 | ## variables: Number, Float, String 20 | 21 | ```viml 22 | " Number is integer 23 | let number = 42 " Defines Number 24 | let number += 1 " 43 25 | 26 | echomsg number " print 43 27 | 28 | unlet number " Undefines number 29 | 30 | echo number " error 31 | ``` 32 | 33 | ```viml 34 | let pi = 3.141592 35 | 36 | " parseInt 37 | let pi_int = float2nr(pi) " 3 38 | 39 | " toString 40 | let pi_str = string(pi) " '3.141592' 41 | ``` 42 | 43 | ```viml 44 | let name = 'John' " 'John' 45 | let size = strchars(name) " 4 46 | 47 | " concat string 48 | let full_name = name . ' Grib' " 'John Grib' 49 | 50 | " substring 51 | echo strcharpart(full_name, 5, 1) " 'G' 52 | ``` 53 | 54 | ## namespaces 55 | 56 | * `g:` - Global. 57 | 58 | * `l:` - Local to a function. 59 | * `s:` - Local to a script file. 60 | * `a:` - Function argument (only inside a function). 61 | 62 | * `v:` - Global, predefined by Vim. 63 | 64 | * `b:` - Local to the current buffer. 65 | * `w:` - Local to the current window. 66 | * `t:` - Local to the current tab page. 67 | 68 | ```viml 69 | let g:number = 42 " global 70 | let s:number = 67 " script scope 71 | 72 | function s:foo(number) " function : script scope 73 | 74 | echo g:number . ' is global.' 75 | echo a:number . ' is argument.' 76 | 77 | let l:number = 23 78 | 79 | echo l:number . ' is local.' 80 | 81 | endfunction 82 | 83 | call s:foo(s:number) 84 | " 42 is global. 85 | " 67 is argument. 86 | " 23 is local. 87 | ``` 88 | 89 | ## List 90 | 91 | * List basic 92 | 93 | ```viml 94 | let list = [] 95 | let list = ['one', 'two', 'three', 'four'] 96 | 97 | echo list[0] " 'one' 98 | echo list[-1] " 'four' 99 | echo list[9] " error 100 | 101 | echo get(list, 0) " 'one' 102 | echo get(list, -1) " 'four' 103 | echo get(list, 9, 'ten') " 'ten' 104 | 105 | let size = len(s:list) " size is 4 106 | 107 | echo list[:1] " ['one', 'two'] 108 | echo list[:2] " ['one', 'two', 'three'] 109 | 110 | echo list[1:] " ['two', 'three', 'four'] 111 | echo list[2:] " ['three', 'four'] 112 | 113 | echo list[0:1] " ['one', 'two'] 114 | echo list[1:2] " ['two', 'three'] 115 | echo list[1:-1] " ['two', 'three', 'four'] 116 | echo list[1:-2] " ['two', 'three'] 117 | 118 | let list += ['five', 'six'] " ['one', 'two', 'three', 'four', 'five', 'six'] 119 | let long_list = list + ['seven', 'eight'] 120 | 121 | call add(long_list, 'nine') 122 | 123 | let copied_list = copy(list) 124 | let recursively_copied_list = deepcopy(list) 125 | 126 | let list[3:4] = [4, 5] " ['one', 'two', 'three', 4, 5, 'six'] 127 | ``` 128 | 129 | * List unpack 130 | 131 | ```viml 132 | let list = [1, 2, 3] 133 | 134 | let [var1, var2, var3] = list 135 | 136 | echo var1 " 1 137 | echo var2 " 2 138 | echo var3 " 3 139 | ``` 140 | 141 | ```viml 142 | let list = [1, 2, 3, 4] 143 | 144 | let [var1, var2; var3] = list " use semicolon 145 | 146 | echo var1 " 1 147 | echo var2 " 2 148 | echo var3 " [3, 4] 149 | ``` 150 | 151 | * List functions 152 | 153 | ```viml 154 | 155 | " length 156 | echo len([1, 2, 3]) " 3 157 | 158 | " check empty 159 | echo empty([1, 2, 3]) " 0 160 | echo empty([]) " 1 161 | 162 | " remove 163 | let list = [1, 2, 3] 164 | call remove(list, 1) " [1, 3] 165 | let removed_item = remove(list, 0) " removed_item is 1, list is [3] 166 | 167 | " filter 168 | let list = [1, 2, 3] 169 | call filter(list, {idx, val -> val > 1}) " use lambda 170 | echo list " [2, 3] 171 | 172 | " map 173 | let list = [1, 2, 3] 174 | call map(list, {idx, val -> val * 10}) 175 | echo list " [10, 20, 30] 176 | 177 | " sort, reverse, uniq, max, min 178 | call sort(list) 179 | call reverse(list) 180 | call uniq(list) 181 | let max = max(list) 182 | let min = min(list) 183 | 184 | " count, index 185 | let list = [1, 2, 3, 4, 5, 5] 186 | echo count(list, 5) " 2 187 | echo index(list, 2) " 1 188 | 189 | " split, join 190 | let list = split('1 2 3', '\s') " ['1', '2', '3'] 191 | let str = join(list, '-') " '1-2-3' 192 | ``` 193 | 194 | ## Dictionary 195 | 196 | * add new entry 197 | 198 | ```viml 199 | let obj = { 'name': 'John', 'number': 24 } 200 | let obj['like'] = ['vim', 'game', 'orange'] 201 | let obj['address'] = 'Seoul, Korea' 202 | let obj.test = 1 203 | ``` 204 | 205 | * delete entry 206 | 207 | ```viml 208 | let obj = { 'name': 'John', 'number': 24, 'test': 1 } 209 | let removed = remove(obj, 'name') 210 | 211 | echo removed " 'John' 212 | 213 | unlet obj.number 214 | unlet obj['test'] 215 | ``` 216 | 217 | * to List 218 | 219 | ```viml 220 | for key in keys(mydict) 221 | echo key . ': ' . mydict[key] 222 | endfor 223 | 224 | for key in sort(keys(mydict)) 225 | echo key . ': ' . mydict[key] 226 | endfor 227 | 228 | for v in values(mydict) 229 | echo "value: " . v 230 | endfor 231 | 232 | for [key, value] in items(mydict) 233 | echo key . ': ' . value 234 | endfor 235 | ``` 236 | 237 | * dictionary functions 238 | 239 | ```viml 240 | echo has_key(dict, 'foo') 241 | echo empty(dict) 242 | ``` 243 | 244 | ## flow control 245 | 246 | * `if` 247 | 248 | ```viml 249 | if 1 250 | echo 'true' 251 | endif 252 | 253 | if 0 254 | echo 'impossibe' 255 | endif 256 | 257 | let name = 'foo' 258 | 259 | if name == 'test' 260 | echo 'test name' 261 | elseif name == 'foo' " else if (x), elseif (o) 262 | echo name 263 | endif 264 | ``` 265 | 266 | * `for` 267 | 268 | ```viml 269 | for item in [1, 2, 3] 270 | echo item 271 | endif 272 | 273 | for item in [1, 2, 3] 274 | if item < 2 275 | continue 276 | else 277 | break 278 | endif 279 | endfor 280 | ``` 281 | 282 | * `while` 283 | 284 | ```viml 285 | let sum = 0 286 | while sum < 100 287 | let sum += 1 288 | call do_something() 289 | endwhile 290 | ``` 291 | 292 | ## function 293 | 294 | * The name must be made of alphanumeric characters and `_` 295 | * The name must start with a capital or `s:` 296 | * `:b` or `:g` is not allowed. 297 | 298 | ```viml 299 | function Foo(value) " global function 300 | echo a:value 301 | endfunction 302 | 303 | function s:foo(value) " script private function 304 | echo a:value 305 | endfunction 306 | ``` 307 | 308 | * `function!` : override a function with the same name. 309 | 310 | ```viml 311 | function Foo(value) 312 | echo a:value 313 | endfunction 314 | 315 | function! Foo(value) 316 | echo a:value * 10 317 | endfunction 318 | 319 | call Foo(7) " 70 320 | ``` 321 | 322 | * Dictionary function (method) with `dict` keyword 323 | 324 | ```viml 325 | let obj = { 'msg': 'hello' } 326 | 327 | function obj.getMsg() dict 328 | return self.msg 329 | endfunction 330 | 331 | let msg = obj.getMsg() 332 | echo msg " 'hello' 333 | ``` 334 | 335 | ```viml 336 | function GetMsg() dict 337 | return self.msg 338 | endfunction 339 | 340 | let obj1 = { 'msg': 'hello', 'getMsg': funcref('GetMsg') } 341 | let obj2 = { 'msg': 'hi', 'getMsg': function('GetMsg') } 342 | 343 | echo obj1.getMsg() " 'hello' 344 | echo obj2.getMsg() " 'hi' 345 | ``` 346 | 347 | * apply with `function()` 348 | 349 | ```viml 350 | function Add(num1, num2) 351 | return a:num1 + num2 352 | endfunction 353 | 354 | let Add3 = function('Add', [3]) 355 | echo Add3(7) " 10 356 | echo Add3(2) " 5 357 | ``` 358 | 359 | * arguments 360 | 361 | `:help function-argument` 362 | 363 | 364 | ```viml 365 | function Test(...) 366 | echo 'number of args : ' . a:0 367 | echo 'first arg : ' . a:1 368 | echo 'second arg : ' . a:2 369 | 370 | echo '' 371 | 372 | for s in a:000 373 | echon ' ' . s 374 | endfor 375 | endfunction 376 | 377 | call Test('dog', 'cat') 378 | " number of args : 2 379 | " first arg : dog 380 | " second arg : cat 381 | " dog cat 382 | ``` 383 | 384 | * public static function 385 | * naming rule : {directory_name}#{file_name}#{function_name} 386 | 387 | ``` 388 | - .vim 389 | ┕ plugin 390 | ┕ test-vim-plugin # project root 391 | ┕ syntax 392 | ... 393 | ┕ plugin 394 | ... 395 | ┕ autoload 396 | ┕ TestVimPlugin 397 | ┕ screen.vim # sample file 398 | ``` 399 | 400 | ```viml 401 | # ~/.vim/plugin/test-vim-plugin/autoload/TestVimPlugin/screen.vim 402 | 403 | function! TestVimPlugin#screen#getScreenNumber() 404 | return 1 405 | endfunction 406 | ``` 407 | --------------------------------------------------------------------------------