18 You can either put this in a script (`script.vim`) and run it (`:source script.vim`), or you can type the commands individually in normal mode as `:let` and `:echo`.
24 let l:part = strpart(getline('.'),col('.')-2,1)
25 if (l:part =~ '^\W\?$')
32 imap <Tab> <C-R>=SuperTab()<CR>
35 [Here](http://www.vimbits.com/bits/46)'s another example with [functions](#functions), [variables](#variables) and [mapping](#mapping).
50 let g:ack_options = '-s -H' " g: global
51 let s:ack_program = 'ack' " s: local (to script)
52 let l:foo = 'bar' " l: local (to function)
55 The `s:` prefix is also available in function names. See `:help local-variables`
60 let w:foo = 'bar' " w: window
61 let b:state = 'on' " b: buffer
62 let t:state = 'off' " t: tab
63 echo v:var " v: vim special
67 let @/ = '' " @ register (this clears last search pattern)
74 echo 'tabstop is ' . &tabstop
80 Prefix Vim options with `&`
86 'hello ' . name " concat
92 let var .= 'string' " concat
101 let str = "String with \n newline"
103 let literal = 'literal, no \ escaping'
104 let literal = 'that''s enough' " double '' => '
106 echo "result = " . re " concatenation
109 Also see `:help literal-string` and `:help expr-quote`.
110 See: [Strings](http://learnvimscriptthehardway.stevelosh.com/chapters/26.html)
117 strchars(str) " character length
119 split("one two three") "=> ['one', 'two', 'three']
120 split("one.two.three", '.') "=> ['one', 'two', 'three']
122 join(['a', 'b'], ',') "=> 'a,b'
128 Also see `:help functions`
129 See: [String functions](http://learnvimscriptthehardway.stevelosh.com/chapters/27.html)
138 " prefix with s: for local script-only functions
139 function! s:Initialize(cmd, args)
140 " a: prefix for arguments
141 echo "Command: " . a:cmd
147 See: [Functions](http://learnvimscriptthehardway.stevelosh.com/chapters/23.html)
152 function! myplugin#hello()
155 ### Calling functions
159 call s:Initialize("hello")
162 ### Consuming return values
165 echo "Result: " . s:Initialize()
171 function! myfunction() abort
175 Aborts when an error occurs.
180 function! infect(...)
185 for s in a:000 " a list
190 infect('jake', 'bella')
193 See `:help function-argument`. See: [Var arguments](http://learnvimscriptthehardway.stevelosh.com/chapters/24.html)
201 continue " jump to start of loop
202 break " breaks out of a loop
218 command! Save :set fo=want tw=80 nowrap
221 Custom commands start with uppercase letters. The `!` redefines a command if it already exists.
223 ### Commands calling functions
226 command! Save call <SID>foo()
236 ### Commands with arguments
239 command! -nargs=? Save call script#foo(<args>)
245 | `-nargs=0` | 0 arguments, default |
246 | `-nargs=1` | 1 argument, includes spaces |
247 | `-nargs=?` | 0 or 1 argument |
248 | `-nargs=*` | 0+ arguments, space separated |
249 | `-nargs=+` | 1+ arguments, space reparated |
258 if char == "\<LeftMouse>"
260 elseif char == "\<RightMouse>"
270 if 1 | echo "true" | endif
271 if 0 | echo "false" | endif
278 if "456" "=> 1 (true)
279 if "xfz" "=> 0 (false)
282 No booleans. `0` is false, `1` is true.
283 See: [Truthiness](http://learnvimscriptthehardway.stevelosh.com/chapters/21.html)
290 if (a && b) || (c && d)
294 See `:help expression-syntax`.
295 See: [Operators](http://learnvimscriptthehardway.stevelosh.com/chapters/22.html)
300 if name ==# 'John' " case-sensitive
301 if name ==? 'John' " case-insensitive
302 if name == 'John' " depends on :set ignorecase
304 " also: is#, is?, >=#, >=?, and so on
307 ### Identity operators
314 Checks if it's the same instance object.
324 `\v` enables "extended" regex mode which allows word boundary (`<>`), `+`, and more.
329 if empty(a:path) | return [] | endif
333 Use `|` to join lines together.
338 if g:use_dispatch && s:has_dispatch
349 let mylist = [1, two, 3, "four"]
351 let first = mylist[0]
352 let last = mylist[-1]
355 let second = get(mylist, 1)
356 let second = get(mylist, 1, "NONE")
366 let sortedlist = sort(copy(list))
368 split('hello there world', ' ')
374 let longlist = mylist + [5, 6]
381 let shortlist = mylist[2:-1]
382 let shortlist = mylist[2:] " same
384 let shortlist = mylist[2:2] " one item
390 let alist = [1, 2, 3]
391 let alist = add(alist, 4)
397 call map(files, "bufname(v:val)") " use v:val for value
398 call filter(files, 'v:val != ""')
413 echo get(colors, "apple") " suppress error
418 ### Using dictionaries
421 remove(colors, "apple")
426 if has_key(dict, 'foo')
443 map(dict, '<>> " . v:val')
449 for key in keys(mydict)
450 echo key . ': ' . mydict(key)
460 Prefixes (`s:`, `g:`, `l:`, etc) are actually dictionaries.
465 " Extending with more
466 let extend(s:fruits, { ... })
487 let int = 0755 " octal
491 See: [Numbers](http://learnvimscriptthehardway.stevelosh.com/chapters/25.html)
505 3 / 2 "=> 1, integer division
526 ### Execute a command
530 execute "e " . fnameescape(filename)
533 Runs an ex command you typically run with `:`. Also see `:help execute`.
534 See: [Execute a command](http://learnvimscriptthehardway.stevelosh.com/chapters/28.html)
536 ### Running keystrokes
540 normal! G " skips key mappings
542 execute "normal! gg/foo\<cr>dd"
545 Use `:normal` to execute keystrokes as if you're typing them in normal mode. Combine with `:execute` for special keystrokes.
546 See: [Running keystrokes](http://learnvimscriptthehardway.stevelosh.com/chapters/29.html)
548 ### Getting filenames
551 echo expand("%") " path/file.txt
552 echo expand("%:t") " file.txt
553 echo expand("%:p:h") " /home/you/path/file.txt
554 echo expand("%:r") " path/file
555 echo expand("%:e") " txt
566 Suppresses output. See `:help silent`
571 echoerr 'oh it failed'
572 echomsg 'hello there'
575 echohl WarningMsg | echomsg "=> " . a:msg | echohl None
592 let result = confirm("Sure?")
599 has("feature") " :h feature-list
601 globpath(&rtp, "syntax/c.vim")
637 └ normal, visual, insert,
638 eX mode, select, operator-pending
644 | `<buffer>` | only in current buffer |
645 | `<silent>` | no echo |
661 ### Filetype detection
664 augroup filetypedetect
665 au! BufNewFile,BufRead *.json setf javascript
668 au Filetype markdown setlocal spell
675 syn match newLine "<br>" conceal cchar=}
676 hi newLine guifg=green
682 syn region inBold concealends matchgroup=bTag start="<b>" end="</b>"
690 syn match :name ":regex" :flags
692 syn region Comment start="/\*" end="\*/"
693 syn region String start=+"+ end=+"+ skip=+\\"+
695 syn cluster :name contains=:n1,:n2,:n3...
704 hi def link markdownH1 htmlH1
710 if exists('g:loaded_myplugin')
716 let g:loaded_myplugin = 1